网站建设与服务技能实训心得体会,展馆公司,seo关键词优化公司推荐,wordpress自动添加视频播放器本章注意介绍如何在ansible中写脚本
playbook的语法在写playbook时如何进行错误处理
ansible的许多模块都是在命令行中执行的#xff0c;每次只能执行一个模块。如果需要执行多个模块#xff0c;且要写判断语句#xff0c;判断模块是否执行成功了#xff0c;如果没成功会…本章注意介绍如何在ansible中写脚本
playbook的语法在写playbook时如何进行错误处理
ansible的许多模块都是在命令行中执行的每次只能执行一个模块。如果需要执行多个模块且要写判断语句判断模块是否执行成功了如果没成功会怎么处理等。这时就需要写脚本了ansible中的脚本叫作 playbook每个 playbook中可以包含多个play
1.1 playbook的写法
playbook是以yaml或yml作为后缀每个play都可以使用两种格式来写
1参数写在模块后面
- name: play的名称
hosts主机组1主机组2.... //列出主机
tasks
- name提示信息1
模块1argx1vx1 argz2vx2 //这种写法“”两边不要有空格
- name: 提示信息x
模块x: rgx1vx1 argx2vx2
一个 play 中可以包含多个 tasks每个 task调用一个模块
2参数分行写一行一个参数
‐ hosts:主机组1主机组2,... #‐‐列出主机组
tasks:
‐ name:描述语句1
模块1:
argxl: vxl #这里指定模块的参数,注意冒号后面的空格
argx2: vx2
‐ name:描述语句2
模块x:
argxl:vx1
argx2:vx2需要注意的是YAML文件对缩进有极严格的要求,每个缩进都是两个空格不要按【Tab】键
一个完整的playbook中至少要包含一个 play下面是一个包含两个play的playbook
‐‐‐
‐ name: 第一个play的名称
hosts: 主机组1主机组2...#‐‐列出主机组
tasks:
‐ name:提示信息1
模块1:argxl‐vxl argx2vx2
‐ name:提示信息×
模块x:rgxlvxl argx2vx2
‐ name: 第二个play的名称
hosts: 主机组3,主机组4,...#‐‐列出主机组
gather facts: false
tasks:
‐ name: 提示信息1
模块1: argxlvxl argx2vx2
‐ name: 提示信息×
模块x: rgxlvxl argx2Vx2
在写playbook时一定要先写好框架然后往框架中写内容。如果在多个主机组上做的是相同的操作可以把它们放在同一个play中。如果在不同的主机组上做的是不同的操作可以通过不同的play分别来实现
这里第二个play中加了一句gather_facts: false意思是在执行此play时不需要通过setup获取主机组的信息。所以如果在tasks中没有使用到fact变量建议加上这句可以提升执行的速度
写好之后运行playbook的方法是ansible playbook文件
本章实验都在/home/lduan/demol下操作先把demo1目录创建出来并把 ansible.cfg 和hosts拷贝进去
[jinrhel801 ~]$ mkdir demo1
[jinrhel801 ~]$ cp ansible.cfg hosts demo1/
[jinrhel801 ~]$ cd demo1/
[jinrhel801 demo1]$ ls
ansible.cfg hosts
[jinrhel801 demo1]$
练习1写一个playbook文件test1.yaml在rhel802和rhel803上打印主机名和IP
分析因为在server2和 server3上做的是相同的操作所以只要一个play即可。这个play中包含两个task: 一个用于打印主机名另一个用于打印IP
[jinrhel801 demo1]$ cat test1.yml
---
- hosts: rhel802,rhel803tasks:- name: 打印主机名debug: msg{{ansible_fqdn}}- name: 打印IP地址debug: msg{{ansible_default_ipv4.address}}
[jinrhel801 demo1]$
运行此playbook
[jinrhel801 demo1]$ ansible-playbook test1.yml PLAY [rhel802,rhel803] *********************************************************TASK [Gathering Facts] *********************************************************
ok: [rhel802]
ok: [rhel803]TASK [打印主机名] *******************************************************************
ok: [rhel802] {msg: rhel802
}
ok: [rhel803] {msg: rhel803
}TASK [打印IP地址] ******************************************************************
ok: [rhel802] {msg: 192.168.161.17
}
ok: [rhel803] {msg: 192.168.161.18
}PLAY RECAP *********************************************************************
rhel802 : ok3 changed0 unreachable0 failed0 skipped0 rescued0 ignored0
rhel803 : ok3 changed0 unreachable0 failed0 skipped0 rescued0 ignored0 [jinrhel801 demo1]$
练习2写一个playbook文件test2.yaml在rhel802上打印主机名在rhel803上打印 IP。
分析:因为在rhel802和 rhel803上做的是不同的操作所以这里写两个play一个play在 rhel802上执行,另一个play在rhel803上执行。每个play中只要包含一个task即可
[jinrhel801 demo1]$ cat test2.yml
---
- name: 在rhel802上的操作hosts: rhel802tasks: - name: 打印主机名debug: msg{{ansible_fqdn}}- name: 在rhel803上的操作hosts: rhel803tasks: - name: 打印IP地址debug: msg{{ansible_default_ipv4.address}}
[jinrhel801 demo1]$
运行此playbook
[jinrhel801 demo1]$ ansible-playbook test2.yml PLAY [在rhel802上的操作] ************************************************************TASK [Gathering Facts] *********************************************************
ok: [rhel802]TASK [打印主机名] *******************************************************************
ok: [rhel802] {msg: rhel802
}PLAY [在rhel803上的操作] ************************************************************TASK [Gathering Facts] *********************************************************
ok: [rhel803]TASK [打印IP地址] ******************************************************************
ok: [rhel803] {msg: 192.168.161.18
}PLAY RECAP *********************************************************************
rhel802 : ok2 changed0 unreachable0 failed0 skipped0 rescued0 ignored0
rhel803 : ok2 changed0 unreachable0 failed0 skipped0 rescued0 ignored0 [jinrhel801 demo1]$
练习3写一个playbook 文件 test3.yaml要求如下
1在rhel802上安装vsftpd启动并开机自动启动vsftpd,设置防火墙开放ftp服务
2在rhel803上安装 httpd启动并开机自动启动httpd设置防火墙开放http服务
分析因为在server2和 server3上做的是不同的操作所以这里写两个play
第一个play在rhel80r2上执行包含3个task分别用于安装、服务管理、防火墙设置
第二个 play在rhel803上执行包含3个task分别用于安装、服务管理、防火墙设置
[jinrhel801 demo1]$ cat test3.yml
---
- name: 在rhel802上要做的操作----安装vsftpd启动服务开启防火墙hosts: rhel802tasks: - name: 第一个操作安装vsftpdyum: namevsftpd stateinstalled- name: 第二个操作启动服务service: namevsftpd statestarted enabledyes- name: 第三个操作开启防火墙firewalld: serviceftp stateenabled immediateyes permanentyes- name: 在rhel803上要做的操作----安装httpd启动服务开启防火墙hosts: rhel803tasks:- name: 第一个操作安装httpdyum: namehttpd stateinstalled- name: 第二个操作启动服务service: namehttpd statestarted enabledyes- name: 第三个操作开启防火墙firewalld: servicehttp stateenabled immediateyes permanentyes
[jinrhel801 demo1]$
运行此playbook
[jinrhel801 demo1]$ ansible-playbook test3.yml PLAY [在rhel802上要做的操作----安装vsftpd启动服务开启防火墙] ***********************************TASK [Gathering Facts] *********************************************************
ok: [rhel802]TASK [第一个操作安装vsftpd] ***********************************************************
ok: [rhel802]TASK [第二个操作启动服务] ***************************************************************
ok: [rhel802]TASK [第三个操作开启防火墙] **************************************************************
ok: [rhel802]PLAY [在rhel803上要做的操作----安装httpd启动服务开启防火墙] ************************************TASK [Gathering Facts] *********************************************************
ok: [rhel803]TASK [第一个操作安装httpd] ************************************************************
changed: [rhel803]TASK [第二个操作启动服务] ***************************************************************
changed: [rhel803]TASK [第三个操作开启防火墙] **************************************************************
changed: [rhel803]PLAY RECAP *********************************************************************
rhel802 : ok4 changed0 unreachable0 failed0 skipped0 rescued0 ignored0
rhel803 : ok4 changed3 unreachable0 failed0 skipped0 rescued0 ignored0 [jinrhel801 demo1]$
1.2 错误处理
在写playbook时会遇到各种各样的问题例如命令出错了,或者引用的变量不存在等。playbook具备一定的错误处理能力
1.2.1 ignore_errors
执行一个playbook时如果其中的某个task出错则后续的task就不再继续执行了。看 下面的例子编写test4.yaml的内容如下
[jinrhel801 demo1]$ cat test4.yml
---
- hosts: rhel802gather_facts: falsetasks:- name: aadebug: msg{{default_aaa}}- name: bbdebug: msg3333
[jinrhel801 demo1]$ 这里写了两个task一个是aa另一个是bbaa这个 task 中引用了一个不存在的变量 default_aaa所以导致aa这个task报错。如果某个task出错,则后续的task就不再继续执行了所以bb这个 task不会继续执行了
[jinrhel801 demo1]$ ansible-playbook test4.yml PLAY [rhel802] *****************************************************************TASK [aa] **********************************************************************
fatal: [rhel802]: FAILED! {msg: The task includes an option with an undefined variable. The error was: default_aaa is undefined\n\nThe error appears to be in /home/jin/demo1/test4.yml: line 5, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: aa\n ^ here\n}PLAY RECAP *********************************************************************
rhel802 : ok0 changed0 unreachable0 failed1 skipped0 rescued0 ignored0 [jinrhel801 demo1]$ 如果想让task aa出错时不影响后续task的执行那么可以在task aa中添加 ignore_errors:true来忽略这个报错继续往下执行 这里添加了ignore_errorstrue忽略报错信息。下面运行test4.yaml查看结果
[jinrhel801 demo1]$ ansible-playbook test4.yml PLAY [rhel802] *****************************************************************TASK [aa] **********************************************************************
fatal: [rhel802]: FAILED! {msg: The task includes an option with an undefined variable. The error was: default_aaa is undefined\n\nThe error appears to be in /home/jin/demo1/test4.yml: line 5, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: aa\n ^ here\n}
...ignoringTASK [bb] **********************************************************************
ok: [rhel802] {msg: 3333
}PLAY RECAP *********************************************************************
rhel802 : ok2 changed0 unreachable0 failed0 skipped0 rescued0 ignored1 [jinrhel801 demo1]$
可以看到即使task aa出错了但是后续的 task bb仍然继续执行
1.2.2 fail语句
fail模块和debug模块一样都是用来打印信息的区别在于debug执行完成之后会继续进行后续模块的操作而fail打印完报错信息之后会退出整个playbook。编写test5.yaml的内容如下
[jinrhel801 demo1]$ cat test5.yml
---
- hosts: rhel802gather_facts: falsetasks:- name: aadebug: msg111- name: bbfail: msg222- name: ccdebug: msg333[jinrhel801 demo1]$
这里写了3个task其中task aa和 task cc使用debug打印信息, task bb使用fail打印信息。下面运行此playbook查看结果,如下所示
[jinrhel801 demo1]$ ansible-playbook test5.yml PLAY [rhel802] *****************************************************************TASK [aa] **********************************************************************
ok: [rhel802] {msg: 111
}TASK [bb] **********************************************************************
fatal: [rhel802]: FAILED! {changed: false, msg: 222}PLAY RECAP *********************************************************************
rhel802 : ok1 changed0 unreachable0 failed1 skipped0 rescued0 ignored0 [jinrhel801 demo1]$
可以看到task aa正确执行之后继续执行task bb。因为 task bb用的是fail来打印信, 所以执行完成之后就退出 playbook了task cc并没有执行