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

原文地址:https://www.cnblogs.com/tiki/p/10785531.html

时间: 2024-08-29 12:11:59

json yaml playbook语法的相关文章

Ansible PlayBook语法(4)

title: Ansible PlayBook语法(4) date: 2018-12-02 10:53:24 tags: Ansible categories: Ansible copyright: true --- Ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统配置.批量程序部署.批量运行命令等功能,ansible是基于模块工作的,本身没有批量部署的能力,真正具有批量部署

kbmmw 中XML 操作入门(跨平台,而且可以与JSON,YAML,BSON 直接互相转换)

delphi 很早以前就自带了xml 的操作,最新版里面有三种XML 解释器,一种是MSXML,看名字就知道 这个是微软自带的,这个据delphi 官方称是速度是最快的,但是只能在windows 上使用.另外两个是 OmniXML, ADOM, 这两个都是跨平台的.但是速度比MSXML 慢点.在delphi 10.2 里面,WINDOWS 应用默认使用MSXML, linux 应用默认使用OmniXML. 当然也可以通过强行改变 今天我们讲一下如何使用kbmmw 内置的XML 解释器处理XML

3.1 playbook语法实践

ansible playbook Playbook是使用yaml语言定制的,YAML是一个可读性高的用来表达资料序列的格式   Yml语法: 123456 - host: websrvs                   #定义执行的主机  remote_user: root            #定义执行的用户  tasks:                                #定义任务    - task1                             #定义第一个

YAML基础语法-ansible使用ansible-playbook

YAML 语法 这个页面提供一个正确的 YAML 语法的基本概述, 它被用来描述一个 playbooks(我们的配置管理语言). 我们使用 YAML 是因为它像 XML 或 JSON 是一种利于人们读写的数据格式. 此外在大多数变成语言中有使用 YAML 的库. 基本的 YAML 对于 Ansible, 每一个 YAML 文件都是从一个列表开始. 列表中的每一项都是一个键值对, 通常它们被称为一个 "哈希" 或 "字典". 所以, 我们需要知道如何在 YAML 中编

JSON+YAML初步学习+ciscoconfparse

Git git clone 在github.com右上角点击加号创建新的repository 在Linux或Mac命令行下,找到你想存放这个repository的目录,然后git clone 某个repository的https链接 git clone 除了上面那种方法还可以通过SSH的方式,但是我还没试过,那种是clone一个 .git为结尾的链接好像 cd 到.git 目录下,ll是看不出来的,只能ls -al,看到objects目录下有很多文件,其实每个文件都对应着一个“版本”,git的版

jmeter 不显示 [email protected] -JSON/YAML Path Extractor 处理

出现该问题的原因是,缺少插件,传送门 下载之后, 解压后把对应jar包放置对应的lib和lib/ext目录下,重启Jmeter option(选项)-->plugins manage 会看到如下图 选择 available plugins  ,搜索json,然后安装相关,重启,即可 原文地址:https://www.cnblogs.com/mafy/p/12023469.html

SpringBoot必知必会-yaml基础语法

PS : 一定要注意每一个:(冒号)后面必须有一个半角空格,否则无法识别 # 普通的K :V name: syu #Object student: name: zhangergou age: 3 # Object in one line person: {name: yuanshou,age: 3} #Array myPets: - cat - dog - pig #Array in one line arr: [cat,dog,pig] 原文地址:https://www.cnblogs.com

我关注的一周技术动态 2015.09.06

服务化和资源管理技术 1. Docker容器月刊(2015年8月) http://www.duokan.com/book/95298#rd 要点: 8月份docker 容器技术文章合集. 2. 苹果.彭博.Netflix的Mesos使用经验分享 https://mp.weixin.qq.com/s?__biz=MzA5OTAyNzQ2OA==&mid=207917628&idx=1&sn=36548b857da893fdd8b326803d8d6eff&scene=1&am

03.yaml语法和playbook写法

ansible的playbook采用yaml语法,它简单地实现了json格式的事件描述.yaml之于json就像markdown之于html一样,极度简化了json的书写.在学习ansible playbook之前,很有必要把yaml的语法格式.引用方式做个梳理. 1.1 初步说明 以一个简单的playbook为例,说明yaml的基本语法. --- - hosts: 192.168.100.59,192.168.100.65 remote_user: root pre_tasks: - name