1.1 Ansible playbook中的使用
相当于把模块写入到配置文件里面,例:
[root@ansible-test1 ~]# cat /etc/ansible/test.yml
---
- hosts: 192.168.20.45
remote_user: root
tasks:
- name: test_playbook
shell: touch /tmp/playbook_test.txt
说明:第一行需要有三个杠,hosts参数指定了对哪些主机进行参作,如果是多台机器可以用逗号作为分隔,也可以使用主机组,在/etc/ansible/hosts里定义,user参数指定了使用什么用户登录远程主机操作,tasks指定了一个任务,其下面的name参数同样是对任务的描述,在执行过程中会打印出来,shell是ansible模块名字。
[root@ansible-test1 ~]# ansible-playbook /etc/ansible/test.yml
PLAY [192.168.20.45] *****************************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [192.168.20.45]
TASK [Create a test file using playbook] *********************************************************************************************************************
[WARNING]: Consider using the file module with state=touch rather than running 'touch'. If you need to use command because file is insufficient you can add
'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
ok: [192.168.20.45]
PLAY RECAP ***************************************************************************************************************************************************
192.168.20.45 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
再来一个创建用户的例子:
[root@ansible-test1 ~]# cat create_user.yml
---
- name: Create User
hosts: 192.168.20.45
remote_user: root
become: yes
tasks:
- name: Ensure the user exists
user:
name: user # 替换为你要创建的用户名
state: present
shell: /bin/bash
groups: user # 可选:添加用户到特定组
说明:name参数对该playbook实现的功能做一个概述,后面执行过程中,会打印 name变量的值 ,可以省略;gather_facts参数指定了在以下任务部分执行前,是否先执行setup模块获取主机相关信息,这在后面的task会使用到setup获取的信息时用到;vars参数,指定了变量,这里指字一个user变量,其值为test ,需要注意的是,变量值一定要用引号引住;user提定了调用user模块,name是user模块里的一个参数,而增加的用户名字调用了上面user变量的值。
[root@ansible-test1 ~]# ansible-playbook create_user.yml
PLAY [Create User] ********************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************
ok: [192.168.20.45]
TASK [Ensure the user exists] *********************************************************************************************************************************
changed: [192.168.20.45]
PLAY RECAP ****************************************************************************************************************************************************
192.168.20.45 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
1.2 Ansible playbook中的循环
创建while.yml文件
[root@ansible-test1 ansible]# cat while.yml
---
- name: Create and change mode for files
hosts: 192.168.20.45
remote_user: root
become: yes
tasks:
- name: Ensure the files exist
file:
path: "/tmp/{{ item }}"
state: touch
loop:
- 1.txt
- 2.txt
- 3.txt
- name: Change mode for files
file:
path: "/tmp/{{ item }}"
mode: '0644'
loop:
- 1.txt
- 2.txt
- 3.txt
说明: with_items为循环的对象、执行while.yml
[root@ansible-test1 ~]# ansible-playbook while.yml
PLAY [Create and change mode for files] ***********************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************
ok: [192.168.20.45]
TASK [Ensure the files exist] *********************************************************************************************************************************
changed: [192.168.20.45] => (item=1.txt)
changed: [192.168.20.45] => (item=2.txt)
changed: [192.168.20.45] => (item=3.txt)
TASK [Change mode for files] **********************************************************************************************************************************
ok: [192.168.20.45] => (item=1.txt)
ok: [192.168.20.45] => (item=2.txt)
ok: [192.168.20.45] => (item=3.txt)
PLAY RECAP ****************************************************************************************************************************************************
192.168.20.45 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
1.3 Ansible playbook 中的条件判断
创建when.yml文件
[root@ansible-test1 ansible]# cat when.yml
---
- hosts: testhost
user: root
gather_facts: True
tasks:
- name: use when
shell: touch /tmp/when.txt
when: ansible_ens33.ipv4.address == "192.168.20.45"
说明:ansible anisble-02 -m setup 可以查看到所有的facter信息、执行when.yml
[root@ansible-test1 ansible]# ansible-playbook when.yml -c local
PLAY [testhost] ***************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************
ok: [127.0.0.1]
ok: [192.168.2.20]
TASK [use when] ***************************************************************************************************
skipping: [127.0.0.1]
[WARNING]: Consider using the file module with state=touch rather than running 'touch'. If you need to use
command because file is insufficient you can add 'warn: false' to this command task or set
'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [192.168.2.20]
PLAY RECAP ********************************************************************************************************
127.0.0.1 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
192.168.2.20 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
1.4 Ansible playbook中的handlers.yml
执行task之后,服务器发生变化之后要执行的一些操作,比如我们修改了配置文件后,需要重启一下服务,创建handlers.yml文件加入如下内容:
[root@ansible-test1 ~]# cat handlers.yml
---
- name: handlers test
hosts: 192.168.20.45
user: root
tasks:
- name: copy file
copy: src=/etc/passwd dest=/tmp/aaa.txt
notify: test handlers
handlers:
- name: test handlers
shell: echo "111111" >> /tmp/aaa.txt
说明,只有copy模块真正执行后,才会去调用下面的handlers相关的操作。也就是说如果1.txt和2.txt内容是一样的,并不会去执行handlers里面的shell相关命令。 这种比较适合配置文件发生更改后,重启服务的操作
[root@ansible-test1 ~]# ansible-playbook handlers.yml
PLAY [handlers test] *****************************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [192.168.20.45]
TASK [copy file] *********************************************************************************************************************************************
changed: [192.168.20.45]
RUNNING HANDLER [test handlers] ******************************************************************************************************************************
changed: [192.168.20.45]
PLAY RECAP ***************************************************************************************************************************************************
192.168.20.45 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0