zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Linux Ansible playbook解析

2023-02-18 16:38:31 时间

文章目录

实施playbook

vimrc
[devops@workstation ansible]$ vim ~/.vimrc
set number ts=2 sw=2 et
批量缩进
1.按ctrl v进入可视化块模式
2.按↑↓方向键选中你要缩进的行
3.按I(大)进入输入模式
4.按tab键缩进
5.按esc(两)

帮助文档

[devops@workstation ansible]$ ansible-doc -l | grep yum
[devops@workstation ansible]$ ansible-doc  yum
/EX

playbook

playbook格式
---     //固定开头格式
  2 - name: 安装软件包      //注释说明
  3   hosts: qq           //对哪个主机或主机组进行操作
  4   tasks:			  //具体任务
ad-hoc
[devops@workstation ansible]$ ansible qq -m user -a "name=bob comment=testbob uid=1050 group=root"
servera | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "comment": "testbob",
    "create_home": true,
    "group": 0,
    "home": "/home/bob",
    "name": "bob",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1050
}
[devops@workstation ansible]$ ansible qq -a "id bob"
servera | CHANGED | rc=0 >>
uid=1050(bob) gid=0(root) groups=0(root)
playbook
[devops@workstation ansible]$ vim user.yml
  1 ---
  2 - name: useradd user
  3   hosts: qq
  4   tasks:
  5   - name: Add the user 'johnd'
  6     user:
  7       name: johnd
  8       comment: John Doe
  9       uid: 1040
 10       group: root
[devops@workstation ansible]$ ansible-playbook user.yml
验证
[devops@workstation ansible]$ ansible qq -a "id johnd"
servera | CHANGED | rc=0 >>
uid=1040(johnd) gid=0(root) groups=0(root)

检查语法

[devops@workstation ansible]$ ansible-playbook user.yml -C  //空运行
[devops@workstation ansible]$ ansible-playbook user.yml -vvv //执行playbook时查看详细信息
[devops@workstation ansible]$ ansible-playbook user.yml --syntax-check  //检查语法

清单文件的密码和临时命令密码做优先级

 [devops@workstation ansible]$ vim inventory 
 1 [all:vars]
 2 ansible_password=redhat1

[devops@workstation ansible]$ ansible qq -a "id"
servera | UNREACHABLE! => {
    "changed": false,
    "msg": "Invalid/incorrect password: Permission denied, please try again.",
    "unreachable": true
}

原因:密码错误

[devops@workstation ansible]$ ansible qq -a "id" -e ansible_password=redhat
servera | CHANGED | rc=0 >>
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

没有问题,说明变量以临时命令优先
第三题:安装软件
[devops@workstation ansible]$ vim /home/devops/ansible/packages.yml
  1 ---
  2 - name: install
  3   hosts: dev,test,prod
  4   tasks:
  5   - name: ensure a list of packages installed
  6     yum:
  7       name: "{{ packages }}"
  8     vars:
  9       packages:
 10       - php
 11       - mariadb
 12 - name: install
 13   hosts: dev
 14   tasks:
 15   - name: install the 'Development tools' package group
 16     yum:
 17       name: "@RPM Development Tools"
 18       state: present
 19   - name: upgrade all packages
 20     yum:
 21       name: '*'
 22       state: latest
[devops@workstation ansible]$ ansible-playbook /home/greg/ansible/packages.yml
第一题梳理:主机清单
ssh greg@control
sudo yum install -y ansible
mkdir -p  /home/greg/ansible/roles
cd ansible
cp /etc/ansible/ansible.cfg .
ansible --version
vim ansible.cfg
	inventory = /home/greg/ansible/inventory
	 69 #roles_path    = /etc/ansible/roles
     70 roles_path    = /home/greg/ansible/roles

vim inventory
第二题:脚本
[devops@workstation ansible]$ vim /home/devops/ansible/adhoc.sh
  1 #!/bin/bash
  2 
  3 ansible all -m yum_repository -a "name=EX294_BASE description='EX294 base software' baseurl=http://co    ntent/rhel8.4/x86_64/dvd/BaseOS gpgcheck=yes gpgkey=http://content/rhel8.4/x86_64/dvd/RPM-GPG-KEY-red    hat-release enabled=yes"
  4 ansible all -m yum_repository -a "name=EX294_STREAM description='EX294 stream software' baseurl=http:    //content/rhel8.4/x86_64/dvd/AppStream gpgcheck=yes gpgkey=http://content/rhel8.4/x86_64/dvd/RPM-GPG-    KEY-redhat-release enabled=yes"
[devops@workstation ansible]$ chmod +x /home/devops/ansible/adhoc.sh
[devops@workstation ansible]$ /home/devops/ansible/adhoc.sh