playbook由YMAL语言编写,playbook常用到的YMAL格式:
1、文件的第一行应该以 “- - -” (三个连字符)开始,表明YMAL文件的开始。
2、在同一行中,#之后的内容表示注释,类似于shell,python和ruby。
3、YMAL中的列表元素以”-”开头然后紧跟着一个空格,后面为元素内容。
4、同一个列表中的元素应该保持相同的缩进。否则会被当做错误处理。
5、play中hosts,variables,roles,tasks等对象的表示方法都是键值中间
以":“分隔表示,”:"后面还要增加一个空格。
举例:
---
- hosts: node1remote_user: roottasks:- name: install mysql-server package
解析:
文件名称应该以.yml结尾
①host部分:使用 hosts 指示使用哪个主机或主机组来运行下面的 tasks 每个 playbook
都必须指定 hosts ,hosts也可以使用通配符格式。 ②remote_user:指定远端主机中的哪个用户来登录远端系统,在远端系统执行
task 的用户,可以任意指定,也可以使用 sudo,但是用户必须要有执行相应 task 的权限。
③tasks:指定远端主机将要执行的一系列动作。tasks 的核心为 ansible 的模块,前面已经提到模块的用法。tasks 包含
name 和要执行的模块,name 是可选的,只是为了便于用户阅读,不过还是建议加上去,模块是必须的,同时也要给予模块相应的参数。
输出信息:
使用ansible-playbook运行playbook文件,输出内容为JSON格式。并且由不同颜色组成,便于识别,执行有三个步骤:1、收集facts
2、执行tasks 3、报告结果。一般而言 | 绿色代表执行成功,系统保持原样 | 黄色代表系统代表系统状态发生改变 |
红色代表执行失败,显示错误输出
模块写入到配置文件里面
[root@ansible-01 ~]# vi /etc/ansible/test.yml---
- hosts: 192.168.200.23remote_user: roottasks:- name: test_playbookshell: touch /tmp/ansible_test.txt
运行:
[root@ansible-01 ~]# ansible-playbook /etc/ansible/test.yml
在23端检测一下:
[root@ansible-02 ~]# ls /tmp
创建用户的例子:
[root@ansible-01 ~]# vi /etc/ansible/create_user.yml---
- name: create_userhosts: 192.168.200.23user: rootgather_facts: falsevars:- user: "test"tasks:- name: create useruser: name="{{ user }}"
运行:
[root@ansible-01 ~]# ansible-playbook /etc/ansible/create_user.ymlPLAY [create_user] *************************************************************TASK [create user] *************************************************************
changed: [192.168.200.23]PLAY RECAP *********************************************************************
192.168.200.23 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
说明:
name参数对该playbook实现的功能做一个概述,后面执行过程中,会打印 name变量的值
,可以省略;gather_facts参数指定了在以下任务部分执行前,是否先执行setup模块获取主机相关信息,这在后面的task会使用到setup获取的信息时用到;vars参数,指定了变量,这里指字一个user变量,其值为test
,需要注意的是,变量值一定要用引号引住;user提定了调用user模块,name是user模块里的一个参数,而增加的用户名字调用了上面user变量的值。
创建文件(权限是644):
[root@ansible-01 tmp]# touch 1.txt 2.txt 3.txt
[root@ansible-01 tmp]# ll
total 12
-rw-r--r--. 1 root root 1040 May 25 05:35 123
-rw-r--r--. 1 root root 0 May 25 09:58 1.txt
-rw-r--r--. 1 root root 0 May 25 09:58 2.txt
-rw-r--r--. 1 root root 0 May 25 09:58 3.txt
编写ansible循环(创建while.yml文件):
[root@ansible-01 ~]# vi /etc/ansible/while.yml---
- hosts: testhostuser: roottasks:- name: change mode for filesfile: path=/tmp/{{ item }} mode=600with_items: //循环对象- 1.txt- 2.txt- 3.txt
执行while.yml 文件
[root@ansible-01 ~]# ansible-playbook /etc/ansible/while.yml PLAY [testhost] ****************************************************************TASK [Gathering Facts] *********************************************************
ok: [127.0.0.1]
ok: [192.168.200.23]TASK [change mode for files] ***************************************************
changed: [127.0.0.1] => (item=1.txt)
changed: [192.168.200.23] => (item=1.txt)
changed: [127.0.0.1] => (item=2.txt)
changed: [192.168.200.23] => (item=2.txt)
changed: [127.0.0.1] => (item=3.txt)
changed: [192.168.200.23] => (item=3.txt)PLAY RECAP *********************************************************************
127.0.0.1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.200.23 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
查看文件的权限(是否发生变化,已改为600)
[root@ansible-01 ~]# cd /tmp/
[root@ansible-01 tmp]# ll
total 12
-rw-r--r--. 1 root root 1040 May 25 05:35 123
-rw-------. 1 root root 0 May 25 09:58 1.txt
-rw-------. 1 root root 0 May 25 09:58 2.txt
-rw-------. 1 root root 0 May 25 09:58 3.txt
[root@ansible-01 ~]# vi /etc/ansible/when.yml---
- hosts: testhostuser: rootgather_facts: Truetasks:- name: use whenshell: touch /tmp/when.txtwhen: ansible_eno16777728.ipv4.address == "192.168.200.23"
//设置了ip=23的时候才会执行这个任务,所以运行的时候直接跳过了127.0.0.1,所以23是符合的修改是成功的,这里面when是个判断,当条件符合的时候执行。不符合的时候跳过。
运行:
[root@ansible-01 ~]# ansible-playbook /etc/ansible/when.ymlPLAY [testhost] ****************************************************************TASK [Gathering Facts] *********************************************************
ok: [127.0.0.1]
ok: [192.168.200.23]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.200.23]PLAY RECAP *********************************************************************
127.0.0.1 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
192.168.200.23 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
检查23的机器上是否有文件存在:
[root@ansible-02 ~]# ls /tmp/
123
1.txt
2.txt
3.txt
ansible_test.txt
systemd-private-9b6d1b03780948f9b1163f85f4a1c1a3-httpd.service-KQyVLW
test1
test1.txt
test2.txt
test.sh``
when.txt
``
检查127.0.0.1的机器上是否有文件存在(没存在):
[root@ansible-01 ~]# ls /tmp/
123
1.txt
2.txt
3.txt
ansible_test.txt
systemd-private-850eb47ffcd94252a078dab7a7c7c112-httpd.service-TD9PFV
test.sh
查看到ansible-02所有的facter(主机)信息 (有很多信息没复制全)
[root@ansible-01 ~]# ansible 192.168.200.23 -m setup
192.168.200.23 | SUCCESS => {"ansible_facts": {"ansible_all_ipv4_addresses": ["192.168.200.23"], "ansible_all_ipv6_addresses": ["fe80::20c:29ff:fe93:a98a"], "ansible_apparmor": {"status": "disabled"
。。。
执行task之后,服务器发生变化之后要执行的一些操作,比如我们修改了配置文件后,需要重启一下服务,创建handlers.yml文件加入如下内容
[root@ansible-01 ~]# vi /etc/ansible/handlers.yml (先执行完复制在执行追加)---
- name: handlers testhosts: 192.168.200.23user: roottasks:- name: copy filecopy: src=/etc/passwd dest=/tmp/aaa.txtnotify: test handlershandlers:- name: test handlersshell: echo "11111" >> /tmp/aaa.txt
说明:
只有copy模块真正执行后,才会去调用下面的handlers相关的操作。也就是说如果1.txt和2.txt内容是一样的,并不会去执行handlers里面的shell相关命令。
这种比较适合配置文件发生更改后,重启服务的操作。
运行:
[root@ansible-01 ~]# ansible-playbook /etc/ansible/handlers.ymlPLAY [handlers test] ***********************************************************TASK [Gathering Facts] *********************************************************
ok: [192.168.200.23]TASK [copy file] ***************************************************************
changed: [192.168.200.23]RUNNING HANDLER [test handlers] ************************************************
changed: [192.168.200.23]PLAY RECAP *********************************************************************
192.168.200.23 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
检查一下23端口(先复制后追加):
[root@ansible-02 ~]# cat /tmp/aaa.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:998:996:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:997:995:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
11111