实时警报通知:微信告警通知的重要性解析
820
2023-03-11
ansible系列playbook教程实例
上一篇文章我们讲了ansible的基础用法和安装,不会的请去查看传送门,
这一篇,我们总结一下他的playbook的yml用法,以及基础概念。
一、playbook的简单使用
1、创建文件实例
(1)编辑配置文件
[root@tiejiangSRC1 ~]# cd /etc/ansible/[root@tiejiangSRC1 ansible]# vim test.yml //固定后缀为yml,一定要注意空格 --- - hosts: testhost user: root tasks: - name: playbook_test shell: touch /tmp/playbook.txt注意:hosts参数指定了对哪些主机进行参作;user参数指定了使用什么用户登录远程主机操作;tasks指定了一个任务,其下面的name参数同样是对任务的描述,在执行过程中会打印出来。
[root@SRC1 ~]# cd /etc/ansible/[root@SRC1 ansible]# vim test.yml //固定后缀为yml,一定要注意空格 --- - hosts: testhost user: root tasks: - name: playbook_test shell: touch /tmp/playbook.txt注意:hosts参数指定了对哪些主机进行参作;user参数指定了使用什么用户登录远程主机操作;tasks指定了一个任务,其下面的name参数同样是对任务的描述,在执行过程中会打印出来。
(2)执行创建playbook文件文件
[root@tiejiangSRC1 ansible]# ansible-playbook test.yml PLAY [testhost] **************************************************************** TASK [setup] ******************************************************************* ok: [192.168.2.71] ok: [192.168.2.73] ok: [192.168.2.72] TASK [playbook_test] *********************************************************** changed: [192.168.2.71] changed: [192.168.2.73] changed: [192.168.2.72] PLAY RECAP ********************************************************************* 192.168.2.71 : ok=2 changed=1 unreachable=0 failed=0 192.168.2.72 : ok=2 changed=1 unreachable=0 failed=0 192.168.2.73 : ok=2 changed=1 unreachable=0 failed=0
[root@SRC1 ansible]# ansible-playbook test.yml PLAY [testhost] **************************************************************** TASK [setup] ******************************************************************* ok: [192.168.2.71] ok: [192.168.2.73] ok: [192.168.2.72] TASK [playbook_test] *********************************************************** changed: [192.168.2.71] changed: [192.168.2.73] changed: [192.168.2.72] PLAY RECAP ********************************************************************* 192.168.2.71 : ok=2 changed=1 unreachable=0 failed=0 192.168.2.72 : ok=2 changed=1 unreachable=0 failed=0 192.168.2.73 : ok=2 changed=1 unreachable=0 failed=0
(3)现在来查看是否批量创建成功playbook.txt文件。
(4)给创建的playbook批量导入内容,并查看导入的结果
2、创建用户实例
1、编辑配置文件
[root@tiejiangSRC1 yml]# vim create_user.yml --- - name: create_user hosts: testhost user: root gather_facts: false vars: - user: "tiejiang" tasks: - name: create user user: name="{{ user }}" name参数对该playbook实现的功能做一个概述,后面执行过程中,会打印 name变量的值 ,可以省略;gather_facts参数指定了在以下任务部分执行前,是否先执行setup模块获取主机相关信息,这在后面的task会使用到setup获取的信息时用到;vars参数指定了变量,这里指字一个user变量,其值为test ,需要注意的是,变量值一定要用引号引住;user提定了调用user模块,name是user模块里的一个参数,而增加的用户名字调用了上面user变量的值。
[root@SRC1 yml]# vim create_user.yml --- - name: create_user hosts: testhost user: root gather_facts: false vars: - user: "" tasks: - name: create user user: name="{{ user }}" name参数对该playbook实现的功能做一个概述,后面执行过程中,会打印 name变量的值 ,可以省略;gather_facts参数指定了在以下任务部分执行前,是否先执行setup模块获取主机相关信息,这在后面的task会使用到setup获取的信息时用到;vars参数指定了变量,这里指字一个user变量,其值为test ,需要注意的是,变量值一定要用引号引住;user提定了调用user模块,name是user模块里的一个参数,而增加的用户名字调用了上面user变量的值。
2、执行配置文件
[root@tiejiangSRC1 ansible]# ansible-playbook create_user.yml PLAY [create_user] *************************************************************TASK [create user] *************************************************************changed: [192.168.2.73]changed: [192.168.2.71]changed: [192.168.2.72]PLAY RECAP *********************************************************************192.168.2.71 : ok=1 changed=1 unreachable=0 failed=0 192.168.2.72 : ok=1 changed=1 unreachable=0 failed=0 192.168.2.73 : ok=1 changed=1 unreachable=0 failed=0
[root@SRC1 ansible]# ansible-playbook create_user.yml PLAY [create_user] *************************************************************TASK [create user] *************************************************************changed: [192.168.2.73]changed: [192.168.2.71]changed: [192.168.2.72]PLAY RECAP *********************************************************************192.168.2.71 : ok=1 changed=1 unreachable=0 failed=0 192.168.2.72 : ok=1 changed=1 unreachable=0 failed=0 192.168.2.73 : ok=1 changed=1 unreachable=0 failed=0
3、查看远程机器的passwd文件,是否创建出来了用户
二、playbook循环
实例:修改/tmp目录下的1.txthe 2.txt文件属性
1、去新建实验文件
[root@tiejiangSRC1 yml]# cat touch1and2.yml ---- hosts: testhost user: root tasks: - name: 创建实验文件 shell: touch /tmp/{1.txt,2.txt} [root@tiejiangSRC1 yml]# ansible-playbook touch1and2.yml PLAY [testhost] **************************************************************** TASK [setup] ******************************************************************* ok: [192.168.2.73] ok: [192.168.2.71] ok: [192.168.2.72] TASK [创建实验文件] ****************************************************************** changed: [192.168.2.73] [WARNING]: Consider using file module with state=touch rather than running touch changed: [192.168.2.71] changed: [192.168.2.72] PLAY RECAP ********************************************************************* 192.168.2.71 : ok=2 changed=1 unreachable=0 failed=0 192.168.2.72 : ok=2 changed=1 unreachable=0 failed=0 192.168.2.73 : ok=2 changed=1 unreachable=0 failed=0
[root@SRC1 yml]# cat touch1and2.yml ---- hosts: testhost user: root tasks: - name: 创建实验文件 shell: touch /tmp/{1.txt,2.txt} [root@SRC1 yml]# ansible-playbook touch1and2.yml PLAY [testhost] **************************************************************** TASK [setup] ******************************************************************* ok: [192.168.2.73] ok: [192.168.2.71] ok: [192.168.2.72] TASK [创建实验文件] ****************************************************************** changed: [192.168.2.73] [WARNING]: Consider using file module with state=touch rather than running touch changed: [192.168.2.71] changed: [192.168.2.72] PLAY RECAP ********************************************************************* 192.168.2.71 : ok=2 changed=1 unreachable=0 failed=0 192.168.2.72 : ok=2 changed=1 unreachable=0 failed=0 192.168.2.73 : ok=2 changed=1 unreachable=0 failed=0
2、编辑配置文件
[root@tiejiangSRC1 yml]# cat loop.yml --- - hosts: testhost user: root tasks: - name: change mode for files file: path=/tmp/{{ item }} mode=600 owner=root group=root with_items: - 1.txt - 2.txt
[root@SRC1 yml]# cat loop.yml --- - hosts: testhost user: root tasks: - name: change mode for files file: path=/tmp/{{ item }} mode=600 owner=root group=root with_items: - 1.txt - 2.txt
3、执行配置文件
[root@tiejiangSRC1 yml]# ansible-playbook loop.yml PLAY [testhost] **************************************************************** TASK [setup] ******************************************************************* ok: [192.168.2.71] ok: [192.168.2.72] ok: [192.168.2.73] TASK [change mode for files] *************************************************** changed: [192.168.2.73] => (item=1.txt) changed: [192.168.2.71] => (item=1.txt) changed: [192.168.2.72] => (item=1.txt) changed: [192.168.2.73] => (item=2.txt) changed: [192.168.2.71] => (item=2.txt) changed: [192.168.2.72] => (item=2.txt) PLAY RECAP ********************************************************************* 192.168.2.71 : ok=2 changed=1 unreachable=0 failed=0 192.168.2.72 : ok=2 changed=1 unreachable=0 failed=0 192.168.2.73 : ok=2 changed=1 unreachable=0 failed=0
[root@SRC1 yml]# ansible-playbook loop.yml PLAY [testhost] **************************************************************** TASK [setup] ******************************************************************* ok: [192.168.2.71] ok: [192.168.2.72] ok: [192.168.2.73] TASK [change mode for files] *************************************************** changed: [192.168.2.73] => (item=1.txt) changed: [192.168.2.71] => (item=1.txt) changed: [192.168.2.72] => (item=1.txt) changed: [192.168.2.73] => (item=2.txt) changed: [192.168.2.71] => (item=2.txt) changed: [192.168.2.72] => (item=2.txt) PLAY RECAP ********************************************************************* 192.168.2.71 : ok=2 changed=1 unreachable=0 failed=0 192.168.2.72 : ok=2 changed=1 unreachable=0 failed=0 192.168.2.73 : ok=2 changed=1 unreachable=0 failed=0
三、playbook条件判断
条件判断一般用于针对不同版本的系统,比如对centos、ubuntu 等系统进行不同的操作命令。
1、编辑配置文件
[root@tiejiangSRC1 yml]# vim when.yml --- - hosts: testhost user: root gather_facts: True tasks: - name: use when shell: touch /tmp/when.txt when: ansible_default_ipv4.address == "192.168.2.73"
[root@SRC1 yml]# vim when.yml --- - hosts: testhost user: root gather_facts: True tasks: - name: use when shell: touch /tmp/when.txt when: ansible_default_ipv4.address == "192.168.2.73"
2、执行配置文件
[root@tiejiangSRC1 yml]# ansible-playbook when.yml PLAY [testhost] **************************************************************** TASK [setup] ******************************************************************* ok: [192.168.2.71] ok: [192.168.2.73] ok: [192.168.2.72] TASK [use when] **************************************************************** skipping: [192.168.2.71] skipping: [192.168.2.72] changed: [192.168.2.73] [WARNING]: Consider using file module with state=touch rather than running touch PLAY RECAP ********************************************************************* 192.168.2.71 : ok=1 changed=0 unreachable=0 failed=0 192.168.2.72 : ok=1 changed=0 unreachable=0 failed=0 192.168.2.73 : ok=2 changed=1 unreachable=0 failed=0
[root@SRC1 yml]# ansible-playbook when.yml PLAY [testhost] **************************************************************** TASK [setup] ******************************************************************* ok: [192.168.2.71] ok: [192.168.2.73] ok: [192.168.2.72] TASK [use when] **************************************************************** skipping: [192.168.2.71] skipping: [192.168.2.72] changed: [192.168.2.73] [WARNING]: Consider using file module with state=touch rather than running touch PLAY RECAP ********************************************************************* 192.168.2.71 : ok=1 changed=0 unreachable=0 failed=0 192.168.2.72 : ok=1 changed=0 unreachable=0 failed=0 192.168.2.73 : ok=2 changed=1 unreachable=0 failed=0
四、playbook handlers
当我们执行 tasks 后,服务器发生变化之后我们要执行一些操作。比如我们修改了某个服务的配置文件,需要重启下服务。实例如下:
1、编辑配置文件
说明:只有 copy 模块真正执行后,才会去调用下面的 handlers 相关的操作,追加内容。也就是说如果 src 和 dest 内容是一样的,并不会去执行 handlers 里面的 shell 相关命令。所以这种比较适合配置文件发生更改后,需要重启服务的操作。
2、执行配置文件
[root@tiejiangSRC1 yml]# ansible-playbook handlers.yml PLAY [handlers test] *********************************************************** TASK [setup] ******************************************************************* ok: [192.168.2.73] ok: [192.168.2.71] ok: [192.168.2.72] TASK [test copy] *************************************************************** changed: [192.168.2.71] changed: [192.168.2.73] changed: [192.168.2.72] RUNNING HANDLER [test handlers] ************************************************ changed: [192.168.2.71] changed: [192.168.2.73] changed: [192.168.2.72] PLAY RECAP ********************************************************************* 192.168.2.71 : ok=3 changed=2 unreachable=0 failed=0 192.168.2.72 : ok=3 changed=2 unreachable=0 failed=0 192.168.2.73 : ok=3 changed=2 unreachable=0 failed=0
[root@SRC1 yml]# ansible-playbook handlers.yml PLAY [handlers test] *********************************************************** TASK [setup] ******************************************************************* ok: [192.168.2.73] ok: [192.168.2.71] ok: [192.168.2.72] TASK [test copy] *************************************************************** changed: [192.168.2.71] changed: [192.168.2.73] changed: [192.168.2.72] RUNNING HANDLER [test handlers] ************************************************ changed: [192.168.2.71] changed: [192.168.2.73] changed: [192.168.2.72] PLAY RECAP ********************************************************************* 192.168.2.71 : ok=3 changed=2 unreachable=0 failed=0 192.168.2.72 : ok=3 changed=2 unreachable=0 failed=0 192.168.2.73 : ok=3 changed=2 unreachable=0 failed=0
3、查看执行结果
可查看到 copy 文件成功,同时也执行了 handlers 的相关命令,追加了新的信息。
发表评论
暂时没有评论,来抢沙发吧~