json yaml playbook语法

Posted tiki

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了json yaml playbook语法相关的知识,希望对你有一定的参考价值。

Top

NSD ARCHITECTURE DAY02

  1. 练习1:playbook练习
  2. 案例2:变量练习
  3. 案例3:handlers练习
  4. 案例4:编写playbook

1 练习1:playbook练习

1.1 问题

本案例要求:

  • 安装Apache并修改监听端口为8080
  • 修改ServerName配置,执行apachectl -t命令不报错
  • 设置默认主页hello world
  • 启动服务并设开机自启

1.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:playbook的ping脚本检测

  1. [[email protected] ansible]# vim ping.yml
  2. ---
  3. - hosts: all
  4. remote_user: root
  5. tasks:
  6. - ping:
  7. [[email protected] ansible]# ansible-playbook ping.yml //输出结果
  8. PLAY [all] *******************************************************************
  9. TASK [Gathering Facts] *******************************************************
  10. ok: [web1]
  11. ok: [web2]
  12. ok: [cache]
  13. ok: [db1]
  14. ok: [db2]
  15. TASK [ping] ******************************************************************
  16. ok: [db1]
  17. ok: [web2]
  18. ok: [cache]
  19. ok: [web1]
  20. ok: [db2]
  21. PLAY RECAP *******************************************************************
  22. cache : ok=2 changed=0 unreachable=0 failed=0
  23. db1 : ok=2 changed=0 unreachable=0 failed=0
  24. db2 : ok=2 changed=0 unreachable=0 failed=0
  25. web1 : ok=2 changed=0 unreachable=0 failed=0
  26. web2 : ok=2 changed=0 unreachable=0 failed=0

注意:如果检测的时候出错,会在当前的目录生成一个新的文件(以.retry结尾),可以去这个文件里面看是哪个主机的错

步骤二:用playbook安装Apache,修改端口,配置ServerName,修改主页,设置开机自启

  1. [[email protected] ansible]# vim http.yml
  2. ---
  3. - hosts: cache
  4. remote_user: root
  5. tasks:
  6. - name: install one specific version of Apache
  7. yum:
  8. name: httpd        //安装Apache
  9. state: installed
  10. - lineinfile:
  11. path: /etc/httpd/conf/httpd.conf
  12. regexp: ‘^Listen ‘
  13. line: ‘Listen 8080‘        //修改端口为8080
  14. - replace:
  15. path: /etc/httpd/conf/httpd.conf
  16. regexp: ‘^#(ServerName).*‘        //配置ServerName
  17. replace: \1 localhost‘
  18. - service:
  19. name: httpd
  20. enabled: yes        //开机自启
  21. state: restarted
  22. - copy:
  23. src: /root/index.html        //修改主页,可以自己写个页面
  24. dest: /var/www/html/index.html
  25. [[email protected] ansible]# curl 192.168.1.56:8080
  26. hello world
  27. [[email protected] ansible]# ssh cache
  28. Last login: Fri Sep 7 09:32:05 2018 from 192.168.1.51
  29. [[email protected] ~]# apachectl -t
  30. Syntax OK

2 案例2:变量练习

2.1 问题

本案例要求熟悉playbook进阶:

  • 练习使用user模块添加用户
  • 练习使用变量简化task,让play通用性更强
  • 练习使用过滤器

2.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:使用user模块添加用户,并修改密码

  1. [[email protected] ansible]# vim user.yml
  2. ---
  3. - hosts: cache
  4. remote_user: root
  5. vars:
  6. username: xiaoming
  7. tasks:
  8. - name: create user "{{username}}"
  9. user: group=wheel uid=1000 name={{username}}
  10. - shell: echo 123456 | passwd --stdin xiaoming
  11. - shell: chage -d 0 {{username}}
  12. [[email protected] ansible]# ansible-playbook user.yml //执行结果
  13. PLAY [cache] ******************************************************************
  14. TASK [Gathering Facts] ********************************************************
  15. ok: [cache]
  16. TASK [create user " xiaoming "] ***********************************************
  17. changed: [cache]
  18. TASK [command] ****************************************************************
  19. changed: [cache]
  20. TASK [command] ****************************************************************
  21. changed: [cache]
  22. PLAY RECAP ********************************************************************
  23. cache : ok=4 changed=3 unreachable=0 failed=0

步骤二:变量过滤器,创建一个用户,设置密码

  1. [[email protected] ansible]# vim user1.yml
  2. ---
  3. - hosts: cache
  4. remote_user: root
  5. tasks:
  6. - user:
  7. name: lisi
  8. group: root
  9. password: "{{‘123456‘ | password_hash(‘sha512‘)}}"
  10. - shell: chage -d 0 lisi
  11. [[email protected] ansible]# ansible-playbook user1.yml
  12. PLAY [cache] ******************************************************************
  13. TASK [Gathering Facts] ********************************************************
  14. ok: [cache]
  15. TASK [user] *******************************************************************
  16. changed: [cache]
  17. TASK [command] ****************************************************************
  18. changed: [cache]
  19. PLAY RECAP ********************************************************************
  20. cache : ok=3 changed=2 unreachable=0 failed=0

步骤三:定义一个变量创建用户

  1. [[email protected] ansible]# vim user2.yml
  2. ---
  3. - hosts: cache
  4. remote_user: root
  5. vars:
  6. user: zhangs
  7. tasks:
  8. - user:
  9. name: "{{user}}"
  10. group: root
  11. password: "{{‘123456‘ | password_hash(‘sha512‘)}}"
  12. - shell: chage -d 0 "{{user}}"
  13. [[email protected] ansible]# ansible-playbook user2.yml
  14. PLAY [cache] ******************************************************************
  15. TASK [Gathering Facts] ********************************************************
  16. ok: [cache]
  17. TASK [user] *******************************************************************
  18. changed: [cache]
  19. TASK [command] ****************************************************************
  20. changed: [cache]
  21. PLAY RECAP ********************************************************************
  22. cache : ok=3 changed=2 unreachable=0 failed=0

3 案例3:handlers练习

3.1 问题

本案例要求:

  • 安装Apache软件
  • 配置文件,重新载入配置文件让服务生效
  • 使用handlers来实现

3.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:error

playbook从上往下顺序执行,若报错,后面的命令不会在执行,若想解决有两种方法:

1)当返回值为假时,显示true: - shell: setenforce 0 || true

  1. [[email protected] ansible]# vim user5.yml
  2. ---
  3. - hosts: cache
  4. remote_user: root
  5. vars:
  6. user: bb
  7. tasks:
  8. - shell: setenforce 0 || true
  9. - user:
  10. name: "{{user}}"
  11. group: root
  12. password: "{{‘123456‘ | password_hash(‘sha512‘)}}"
  13. - shell: chage -d 0 "{{user}}"
  14. [[email protected] ansible]# ansible-playbook user5.yml
  15. PLAY [cache] ******************************************************************
  16. TASK [Gathering Facts] ********************************************************
  17. ok: [cache]
  18. TASK [command] ****************************************************************
  19. changed: [cache]
  20. TASK [user] *******************************************************************
  21. changed: [cache]
  22. TASK [command] ****************************************************************
  23. changed: [cache]
  24. PLAY RECAP ********************************************************************
  25. cache : ok=4 changed=3 unreachable=0 failed=0

2、忽略:ignoring_errors: True(推荐使用这个,会有报错信息,告诉你错误忽略,继续执行下面的命令)

  1. [[email protected] ansible]# vim user6.yml
  2. ---
  3. - hosts: cache
  4. remote_user: root
  5. vars:
  6. user: bb
  7. tasks:
  8. - shell: setenforce 0
  9. ignore_errors: True
  10. - user:
  11. name: "{{user}}"
  12. group: root
  13. password: "{{‘123456‘ | password_hash(‘sha512‘)}}"
  14. - shell: chage -d 0 "{{user}}"
  15. [[email protected] ansible]# ansible-playbook user6.yml
  16. PLAY [cache] ******************************************************************
  17. TASK [Gathering Facts] ********************************************************
  18. ok: [cache]
  19. TASK [command] ****************************************************************
  20. fatal: [cache]: FAILED! => {"changed": true, "cmd": "setenforce 0", "delta": "0:00:00.004198", "end": "2018-09-07 11:08:14.936959", "msg": "non-zero return code", "rc": 1, "start": "2018-09-07 11:08:14.932761", "stderr": "setenforce: SELinux is disabled", "stderr_lines": ["setenforce: SELinux is disabled"], "stdout": "", "stdout_lines": []}
  21. ...ignoring
  22. TASK [user] *******************************************************************
  23. changed: [cache]
  24. TASK [command] ****************************************************************
  25. changed: [cache]
  26. PLAY RECAP ********************************************************************
  27. cache : ok=4 changed=3 unreachable=0 failed=0

步骤二: handlers

关注的资源发生变化时采取的操作

1) 使用handlers来配置文件,重新载入配置文件让服务生效

  1. [[email protected] ansible]# vim adhttp.yml
  2. ---
  3. - hosts: cache
  4. remote_user: root
  5. tasks:
  6. - copy:
  7. src: /root/httpd.conf
  8. dest: /etc/httpd/conf/httpd.conf
  9. owner: root
  10. group: root
  11. mode: 0644
  12. notify:
  13. - restart httpd
  14. handlers:
  15. - name: restart httpd
  16. service: name=httpd state=restarted
  17. [[email protected] ansible]# ansible-playbook adhttp.yml
  18. PLAY [cache] ******************************************************************
  19. TASK [Gathering Facts] ********************************************************
  20. ok: [cache]
  21. TASK [copy] *******************************************************************
  22. ok: [cache]
  23. PLAY RECAP ********************************************************************
  24. cache : ok=2 changed=0 unreachable=0 failed=0
  25. [[email protected] ansible]# ssh cache apachectl -t
  26. Syntax OK
  27. [[email protected] ansible]# curl 192.168.1.56:8080
  28. hello world

2)使用脚本调用变量更改服务

  1. [[email protected] ansible]# vim adhttp2.yml
  2. ---
  3. - hosts: cache
  4. remote_user: root
  5. vars:
  6. server: httpd
  7. tasks:
  8. - copy:
  9. src: /root/httpd.conf
  10. dest: /etc/httpd/conf/httpd.conf
  11. owner: root
  12. group: root
  13. mode: 0644
  14. notify:
  15. - restart "{{server}}"
  16. handlers:
  17. - name: restart "{{server}}"
  18. service: name=httpd state=restarted
  19. [[email protected] ansible]# ansible-playbook adhttp2.yml
  20. PLAY [cache] ************************************************************************************************************
  21. TASK [Gathering Facts] **************************************************************************************************
  22. ok: [cache]
  23. TASK [copy] *************************************************************************************************************
  24. ok: [cache]
  25. PLAY RECAP **************************************************************************************************************
  26. cache : ok=2 changed=0 unreachable=0 failed=0
  27. [[email protected] ansible]#

4 案例4:编写playbook

4.1 问题

本案例要求:

  • 把所有监听端口是8080的Apache服务全部停止

4.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:把监听端口是8080的Apache服务全部停止

  1. [[email protected] ansible]# vim ad.yml
  2. ---
  3. - hosts: cache
  4. remote_user: root
  5. tasks:
  6. - shell: netstat -atunlp | awk ‘{print $4}‘| awk ‘-F:‘ ‘{print $2}‘
  7. register: result
  8. - service:
  9. name: httpd
  10. state: stopped
  11. [[email protected] ansible]# ansible-playbook ad.yml
  12. PLAY [cache] ************************************************************************************************************
  13. TASK [Gathering Facts] **************************************************************************************************
  14. ok: [cache]
  15. TASK [command] **********************************************************************************************************
  16. changed: [cache]
  17. TASK [service] **********************************************************************************************************
  18. changed: [cache]
  19. PLAY RECAP **************************************************************************************************************
  20. cache : ok=3 changed=2 unreachable=0 failed=0

步骤二:when条件判断

1)当系统负载超过0.7时,则关掉httpd

  1. [[email protected] ansible]# vim when.yml
  2. ---
  3. - hosts: cache
  4. remote_user: root
  5. tasks:
  6. - shell: uptime | awk ‘{printf("%.2f",$(NF-2))}‘
  7. register: result
  8. - service:
  9. name: httpd
  10. state: stopped
  11. when: result.stdout|float > 0.7
  12. [[email protected] ansible]# ansible-playbook when.yml
  13. PLAY [cache] ************************************************************************************************************
  14. TASK [Gathering Facts] **************************************************************************************************
  15. ok: [cache]
  16. TASK [command] **********************************************************************************************************
  17. changed: [cache]
  18. TASK [service] **********************************************************************************************************
  19. changed: [cache]
  20. PLAY RECAP **************************************************************************************************************
  21. cache : ok=3 changed=2 unreachable=0 failed=0

步骤三:with_items标准循环

1)为不同用户定义不同组

  1. [[email protected] ansible]# vim add.yml
  2. ---
  3. - hosts: web2
  4. remote_user: root
  5. tasks:
  6. - user:
  7. name: "{{item.name}}"
  8. group: "{{item.group}}"
  9. password: "{{‘123456‘|password_hash(‘sha512‘)}}"
  10. with_items:
  11. - {name: "aa", group: "users"}
  12. - {name: "bb", group: "mail" }
  13. - {name: "cc", group: "wheel"}
  14. - {name: "dd", group: "root" }
  15. [[email protected] ansible]# ansible-playbook add.yml
  16. PLAY [web2] *************************************************************************************************************
  17. TASK [Gathering Facts] **************************************************************************************************
  18. ok: [web2]
  19. TASK [user] *************************************************************************************************************
  20. changed: [web2] => (item={u‘group‘: u‘users‘, u‘name‘: u‘aa‘})
  21. changed: [web2] => (item={u‘group‘: u‘mail‘, u‘name‘: u‘bb‘})
  22. changed: [web2] => (item={u‘group‘: u‘wheel‘, u‘name‘: u‘cc‘})
  23. changed: [web2] => (item={u‘group‘: u‘root‘, u‘name‘: u‘dd‘})
  24. PLAY RECAP **************************************************************************************************************
  25. web2 : ok=2 changed=1 unreachable=0 failed=0

2)嵌套循环,循环添加多用户

  1. [[email protected] ansible]# vim add1.yml
  2. ---
  3. - hosts: web2
  4. remote_user: root
  5. vars:
  6. un: [a, b, c]
  7. id: [1, 2, 3]
  8. tasks:
  9. - name: add users
  10. shell: echo {{item}}
  11. with_nested:
  12. - "{{un}}"
  13. - "{{id}}"
  14. [[email protected] ansible]# ansible-playbook add1.yml
  15. PLAY [web2] *************************************************************************************************************
  16. TASK [Gathering Facts] **************************************************************************************************
  17. ok: [web2]
  18. TASK [add users] ********************************************************************************************************
  19. changed: [web2] => (item=[u‘a‘, 1])
  20. changed: [web2] => (item=[u‘a‘, 2])
  21. changed: [web2] => (item=[u‘a‘, 3])
  22. changed: [web2] => (item=[u‘b‘, 1])
  23. changed: [web2] => (item=[u‘b‘, 2])
  24. changed: [web2] => (item=[u‘b‘, 3])
  25. changed: [web2] => (item=[u‘c‘, 1])
  26. changed: [web2] => (item=[u‘c‘, 2])
  27. changed: [web2] => (item=[u‘c‘, 3])
  28. PLAY RECAP **************************************************************************************************************
  29. web2 : ok=2 changed=1 unreachable=0 failed=0

步骤四:tags给指定的任务定义一个调用标识

1)tags 样例

  1. [[email protected] ansible]# vim adhttp.yml
  2. ---
  3. - hosts: cache
  4. remote_user: root
  5. tasks:
  6. - copy:
  7. src: /root/httpd.conf
  8. dest: /etc/httpd/conf/httpd.conf
  9. owner: root
  10. group: root
  11. mode: 0644
  12. tags: config_httpd
  13. notify:
  14. - restart httpd
  15. handlers:
  16. - name: restart httpd
  17. service: name=httpd state=restarted

2)调用方式

  1. [[email protected] ansible]# ansible-playbook adhttp.yml --tags=config_httpd
  2. PLAY [cache] *****************************************************************
  3. TASK [Gathering Facts] *******************************************************
  4. ok: [cache]
  5. TASK [copy] ******************************************************************
  6. ok: [cache]
  7. PLAY RECAP *******************************************************************
  8. cache : ok=2 changed=0 unreachable=0 failed=0

3)include and roles

在编写playbook的时候随着项目越来越大,playbook越来越复杂。可以把一些play、task 或 handler放到其他文件中,通过包含进来是一个不错的选择

roles像是加强版的include,它可以引入一个项目的文件和目录

一般所需的目录层级有

vars:变量层

tasks:任务层

handlers:触发条件

files:文件

template:模板

default:默认,优先级最低

  1. ...
  2. tasks:
  3. - include: tasks/setup.yml
  4. - include: tasks/users.yml user=plj
  5. //users.yml 中可以通过{{ user }}来使用这些变量
  6. handlers:
  7. - include: handlers/handlers.yml

步骤五:debug检测

  1. [[email protected] ansible]# ansible-playbook --syntax-check http.yml //检测语法
  2. playbook: http.yml
  3. [[email protected] ansible]# ansible-playbook -C http.yml //测试运行
  4. [[email protected] ansible]# ansible-playbook http.yml --list-tasks
  5. //显示要执行的工作
  6. playbook: http.yml
  7. play #1 (cache): cache    TAGS: []
  8. tasks:
  9. install one specific version of Apache    TAGS: []
  10. lineinfile    TAGS: []
  11. replace    TAGS: []
  12. service    TAGS: []
  13. copy    TAGS: []
  14. [[email protected] ansible]# vim debug.yml
  15. ---
  16. - hosts: cache
  17. remote_user: root
  18. tasks:
  19. - shell: uptime |awk ‘{printf("%f\n",$(NF-2))}‘
  20. register: result
  21. - shell: touch /tmp/isreboot
  22. when: result.stdout|float > 0.5
  23. - name: Show debug info
  24. debug: var=result
  25. [[email protected] ansible]# ansible-playbook debug.yml         //运行
  26. PLAY [cache] ************************************************************************************************************
  27. TASK [Gathering Facts] **************************************************************************************************
  28. ok: [cache]
  29. TASK [command] **********************************************************************************************************
  30. changed: [cache]
  31. TASK [command] **********************************************************************************************************
  32. skipping: [cache]
  33. TASK [Show debug info] **************************************************************************************************
  34. ok: [cache] => {
  35. "result": {
  36. "changed": true,
  37. "cmd": "uptime |awk ‘{printf(\"%f\\n\",$(NF-2))}‘",
  38. "delta": "0:00:00.005905",
  39. "end": "2018-09-07 12:57:51.371013",
  40. "failed": false,
  41. "rc": 0,
  42. "start": "2018-09-07 12:57:51.365108",
  43. "stderr": "",
  44. "stderr_lines": [],
  45. "stdout": "0.000000",
  46. "stdout_lines": [
  47. "0.000000"
  48. ]
  49. }
  50. }
  51. PLAY RECAP **************************************************************************************************************
  52. cache : ok=3 changed=1 unreachable=0 failed=0

以上是关于json yaml playbook语法的主要内容,如果未能解决你的问题,请参考以下文章

03.yaml语法和playbook写法

Ansible自动化运维之playbook详解(含yaml介绍)及ansible的加密控制

json yaml playbook语法

ansible核心模块playbook介绍

YAML语法搞定ansible playbook

学习ansible playbook之前先了解下YAML语法