ansible playbook
一:概述
之前的学习ad-hoc命令行的使用,当我们操作简单的shell的时候,使用ad-hoc来说比较简单。但是处理比较繁琐的任务的时候,使用playbook,逻辑更为清晰,尤其他的幂等性的优势更为明显。
playbook组成:
一个playbook是由多个play组成。一个play是由多个task组成。
play结构:
hosts:主机列表 remote_user:远程主机执行用户。tasks:任务列表。
例子:
1 $ cat check.yml 2 --- 3 - name: lmd 4 hosts: test 5 remote_user: bjliumeide 6 tasks: 7 - name: ensure apache is at the latest version 8 shell: /bin/date||she ll /bin/true
结果输出:
1 ansible-playbook check.yml -k 2 SSH password: 3 4 PLAY [lmd] ******************************************************************** 5 6 GATHERING FACTS *************************************************************** 7 ok: [127.0.0.1] 8 9 TASK: [ensure apache is at the latest version] ******************************** 10 changed: [127.0.0.1] 11 12 PLAY RECAP ******************************************************************** 13 127.0.0.1 : ok=2 changed=1 unreachable=0 failed=0
注意:
- 书写yml文件的时候。每个:冒号之后需要留一个空格包括tasks:空格,否则报错。
- 整体以:---开始。三个横线。
- 在冒号之后。必须存在一个空格。tasks冒号后面可以不加空格已测试!!!
- 在书写key value的时候,不要存在空格。大多数: key=value ;在shell 和command模块,参数直接命令,其他模块是state=started。
- 在roles中的mainl.yml不能使用tasks关键词,主要是因为在目录结构中包含了此关键词,main.yml是存在tasks目录中。
- 上下关系的需要用2个空格缩进表示关系。
- 同级的需要对齐,比如上面hosts和tasks;name和shell。
- 在任务中需要些name这个字段,以方便将输出结果辨认是哪个任务输出的。
如果不遵循上面的规范一般会抛出如下错误:
1 #ansible-playbook check.yml -k 2 /usr/lib64/python2.6/site-packages/cryptography/__init__.py:26: DeprecationWarning: Python 2.6 is no longer supported by the Python core team, please upgrade your Python. A future version of cryptography will drop support for Python 2.6 3 DeprecationWarning 4 SSH password: 5 ERROR! Syntax Error while loading YAML. 6 7 8 The error appears to have been in '/export/bjliumeide/ansible/check.yml': line 5, column 3, but may 9 be elsewhere in the file depending on the exact syntax problem. 10 11 The offending line appears to be: 12 13 remote_user:bjliumeide 14 tasks: 15 ^ here
二:playbook的幂等性:
当我们在执行playbook的时候,如果出现报错的时候,在执行playbook的时候,不会重复执行之前的task,前提是已经满足我们要的"结果",这种情况叫做幂等性。
yml文件:
1 # cat check.yml 2 --- 3 - hosts: test 4 remote_user: root 5 tasks: 6 - name: check http install 7 yum: name=httpd state=latest 8 - name: http status 9 service: name=httpd state=started
第一次执行:
1 # ansible-playbook check.yml -k 2 SSH password: 3 4 PLAY [test] ******************************************************************* 5 6 GATHERING FACTS *************************************************************** 7 ok: [127.0.0.1] 8 9 TASK: [check http install] **************************************************** 10 changed: [127.0.0.1] 11 12 TASK: [http status] *********************************************************** 13 changed: [127.0.0.1] 14 15 PLAY RECAP ******************************************************************** 16 127.0.0.1 : ok=3 changed=2 unreachable=0 failed=0
注意changed 改变2次。
第二次执行:
1 # ansible-playbook check.yml -k 2 SSH password: 3 4 PLAY [test] ******************************************************************* 5 6 GATHERING FACTS *************************************************************** 7 ok: [127.0.0.1] 8 9 TASK: [check http install] **************************************************** 10 ok: [127.0.0.1] 11 12 TASK: [http status] *********************************************************** 13 ok: [127.0.0.1] 14 15 PLAY RECAP ******************************************************************** 16 127.0.0.1 : ok=3 changed=0 unreachable=0 failed=0
changed没出现改变 表示任务没有被执行,http软件符合我们目前的需求。所以只是做了检测。如果我们把httpd 停掉 在看下:
1 [root@MiWiFi-R1CM-srv ansbile]# ansible test -m service -a 'name=httpd state=stopped' -k -u root 2 SSH password: 3 127.0.0.1 | success >> { 4 "changed": true, 5 "name": "httpd", 6 "state": "stopped" 7 } 8 9 [root@MiWiFi-R1CM-srv ansbile]# ansible-playbook check.yml -k 10 SSH password: 11 12 PLAY [test] ******************************************************************* 13 14 GATHERING FACTS *************************************************************** 15 ok: [127.0.0.1] 16 17 TASK: [check http install] **************************************************** 18 ok: [127.0.0.1] 19 20 TASK: [http status] *********************************************************** 21 changed: [127.0.0.1] 22 23 PLAY RECAP ******************************************************************** 24 127.0.0.1 : ok=3 changed=1 unreachable=0 failed=0
所以我们在执行playbook 的时候,之前已经执行成功之后,不会重复执行。这就是ansible的幂等性。
学习是一种态度,坚持是质变的利器!

浙公网安备 33010602011771号