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

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

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

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

2 案例2:变量练习

2.1 问题

本案例要求熟悉playbook进阶:

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

2.2 步骤

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

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

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

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

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

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

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

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

  1. [root@ansible ansible]# vim user6.yml
  2. – hosts: cache
  3. remote_user: root
  4. vars:
  5. user: bb
  6. tasks:
  7. – shell: setenforce 0
  8. ignore_errors: True
  9. – user:
  10. name: “{{user}}”
  11. group: root
  12. password: “{{‘123456‘ | password_hash(‘sha512‘)}}”
  13. – shell: chage -d 0 “{{user}}”
  14. [root@ansible ansible]# ansible-playbook user6.yml
  15. PLAY [cache] ******************************************************************
  16. TASK [Gathering Facts] ********************************************************
  17. ok: [cache]
  18. TASK [command] ****************************************************************
  19. 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”: []}
  20. …ignoring
  21. TASK [user] *******************************************************************
  22. changed: [cache]
  23. TASK [command] ****************************************************************
  24. changed: [cache]
  25. PLAY RECAP ********************************************************************
  26. cache : ok=4 changed=3 unreachable=0 failed=0

步骤二: handlers

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

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

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

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

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

4 案例4:编写playbook

4.1 问题

本案例要求:

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

4.2 步骤

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

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

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

步骤二:when条件判断

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

  1. [root@ansible ansible]# vim when.yml
  2. – hosts: cache
  3. remote_user: root
  4. tasks:
  5. – shell: uptime | awk ‘{printf(“%.2f”,$(NF-2))}‘
  6. register: result
  7. – service:
  8. name: httpd
  9. state: stopped
  10. when: result.stdout|float > 0.7
  11. [root@ansible ansible]# ansible-playbook when.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

步骤三:with_items标准循环

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

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

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

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

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

1)tags 样例

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

2)调用方式

  1. [root@ansible 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. tasks:
  2. – include: tasks/setup.yml
  3. – include: tasks/users.yml user=plj
  4. //users.yml 中可以通过{{ user }}来使用这些变量
  5. handlers:
  6. – include: handlers/handlers.yml

步骤五:debug检测

  1. [root@ansible ansible]# ansible-playbook –syntax-check http.yml //检测语法
  2. playbook: http.yml
  3. [root@ansible ansible]# ansible-playbook -C http.yml //测试运行
  4. [root@ansible 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. [root@ansible ansible]# vim debug.yml
  15. – hosts: cache
  16. remote_user: root
  17. tasks:
  18. – shell: uptime |awk ‘{printf(“%f\n“,$(NF-2))}‘
  19. register: result
  20. – shell: touch /tmp/isreboot
  21. when: result.stdout|float > 0.5
  22. – name: Show debug info
  23. debug: var=result
  24. [root@ansible ansible]# ansible-playbook debug.yml         //运行
  25. PLAY [cache] ************************************************************************************************************
  26. TASK [Gathering Facts] **************************************************************************************************
  27. ok: [cache]
  28. TASK [command] **********************************************************************************************************
  29. changed: [cache]
  30. TASK [command] **********************************************************************************************************
  31. skipping: [cache]
  32. TASK [Show debug info] **************************************************************************************************
  33. ok: [cache] => {
  34. “result”: {
  35. “changed”: true,
  36. “cmd”: “uptime |awk ‘{printf(\”%f\\n\”,$(NF-2))}‘”,
  37. “delta”: “0:00:00.005905”,
  38. “end”: “2018-09-07 12:57:51.371013”,
  39. “failed”: false,
  40. “rc”: 0,
  41. “start”: “2018-09-07 12:57:51.365108”,
  42. “stderr”: “”,
  43. “stderr_lines”: [],
  44. “stdout”: “0.000000”,
  45. “stdout_lines”: [
  46. “0.000000”
  47. ]
  48. }
  49. }
  50. PLAY RECAP **************************************************************************************************************
  51. cache : ok=3 changed=1 unreachable=0 failed=0