playbook剧本

playbook剧本

示例一、对所有受控主机配置yum仓库
powershell 复制代码
[student@master ansible]$ vim web.yml
[student@master ansible]$ ansible-playbook web.yml

PLAY [repo] ****************************************************************************

TASK [Gathering Facts] *****************************************************************
ok: [node4]
ok: [node3]
ok: [node5]
ok: [node2]
ok: [node1]

TASK [repo1] ***************************************************************************
ok: [node3]
ok: [node4]
ok: [node5]
ok: [node2]
ok: [node1]

TASK [repo2] ***************************************************************************
changed: [node1]
changed: [node5]
changed: [node2]
changed: [node4]
changed: [node3]

PLAY RECAP *****************************************************************************
node1                      : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node2                      : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node3                      : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node4                      : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node5                      : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
示例二:
1、 安装httpd,在node1执行
2、 开机自启
3、 给/var/www/html目录创建软链接/www
4、 从http://ansible.example.com/index.html下载至/www
5、 能够访问该web站点
powershell 复制代码
[root@ansible ~]# cd /var/www/html
[root@ansible html]# ls
ansible-automation-platform  materials  rhel9  roles
[root@ansible html]# vim index.html
[root@ansible html]# cat index.html
welcome
[student@master ansible]$ vim web1.yml
[student@master ansible]$ ansible-playbook web1.yml

PLAY [webstation] **********************************************************************

TASK [Gathering Facts] *****************************************************************
ok: [node1]

TASK [install httpd] *******************************************************************
changed: [node1]

TASK [create link] *********************************************************************
changed: [node1]

TASK [get html] ************************************************************************
changed: [node1]

TASK [restart httpd] *******************************************************************
changed: [node1]

TASK [set firewalld for http] **********************************************************
changed: [node1]

PLAY RECAP *****************************************************************************
node1                      : ok=6    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[student@master ansible]$ curl http://node1
welcome
示例三:
notify handlers用法(当完成....任务时,则进行....任务)
1、创建用户user1
2、创建用户user2
3、在创建用户user2之后执行在/tmp下创建文件stw1(也就是在完成创建user2任务后,触发任务stw)
powershell 复制代码
[student@master ansible]$ vim test.yml
[student@master ansible]$ ansible-playbook test.yml

PLAY [this is a test playbook] *********************************************************

TASK [Gathering Facts] *****************************************************************
ok: [node1]

TASK [create user1] ********************************************************************
changed: [node1]

TASK [create user2] ********************************************************************
changed: [node1]

RUNNING HANDLER [stw] ******************************************************************
changed: [node1]

PLAY RECAP *****************************************************************************
node1                      : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
在执行一遍:发现不执行stw任务(因为创建user2任务状态没有改变,只有创建user2任务状态改变时才会执行stw任务)
powershell 复制代码
[student@master ansible]$ ansible-playbook test.yml

PLAY [this is a test playbook] *********************************************************

TASK [Gathering Facts] *****************************************************************
ok: [node1]

TASK [create user1] ********************************************************************
ok: [node1]

TASK [create user2] ********************************************************************
ok: [node1]

PLAY RECAP *****************************************************************************
node1                      : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
更改剧本:
powershell 复制代码
[student@master ansible]$ ansible-playbook test.yml

PLAY [this is a test playbook] *********************************************************

TASK [Gathering Facts] *****************************************************************
ok: [node1]

TASK [create user1] ********************************************************************
ok: [node1]

TASK [create user2] ********************************************************************
changed: [node1]

RUNNING HANDLER [stw] ******************************************************************
changed: [node1]

PLAY RECAP *****************************************************************************
node1                      : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

只触发了stw任务,没有触发abc任务(abc任务属于handlers的层级,但是我的notify只写了触发stw任务)

如果想要执行abc任务需要把abc任务写在handlers前面(也就是handlers需要写在剧本的最后)

powershell 复制代码
[student@master ansible]$ ansible-playbook test.yml

PLAY [this is a test playbook] *********************************************************

TASK [Gathering Facts] *****************************************************************
ok: [node1]

TASK [create user1] ********************************************************************
ok: [node1]

TASK [create user2] ********************************************************************
changed: [node1]

TASK [abc] *****************************************************************************
ok: [node1] => {
    "msg": 123
}

RUNNING HANDLER [stw] ******************************************************************
changed: [node1]

PLAY RECAP *****************************************************************************
node1                      : ok=5    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

handlers任务永远最后执行

示例四:
1、 写一个剧本galaxy.yml,只对node1操作
2、 创建用户aa,该用户不能用于登录,家目录/www1
3、 在/www1创建一个文件html
4、 每次执行该剧本时,将系统的当前时间输入到html文件中
5、 如果html中的时间发生变化,那么创建/tmp/kk的文件
powershell 复制代码
[student@master ansible]$ vim test1.yml
[student@master ansible]$ ansible-playbook test1.yml

PLAY [test1] ***************************************************************************

TASK [Gathering Facts] *****************************************************************
ok: [node1]

TASK [create user aa] ******************************************************************
changed: [node1]

TASK [create file] *********************************************************************
changed: [node1]

TASK [input html] **********************************************************************
changed: [node1]

RUNNING HANDLER [kk] *******************************************************************
changed: [node1]

PLAY RECAP *****************************************************************************
node1                      : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
再执行一遍也仍旧触发
powershell 复制代码
[student@master ansible]$ ansible-playbook test1.yml

PLAY [test1] ***************************************************************************

TASK [Gathering Facts] *****************************************************************
ok: [node1]

TASK [create user aa] ******************************************************************
ok: [node1]

TASK [create file] *********************************************************************
changed: [node1]

TASK [input html] **********************************************************************
changed: [node1]

RUNNING HANDLER [kk] *******************************************************************
changed: [node1]

PLAY RECAP *****************************************************************************
node1                      : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
因为状态发生了改变,并且不用date,只要使用shell模块,状态都会发生改变
示例五:

tags用法:给任务打标签,一个任务可以有多个标签

ansible-playbook -t 来指定需要指定的任务标签

注意:

其实ansible还预置了5个特殊的tag

always:如果添加此标签,不管是否指定该任务,都执行

never:不执行标签(指定执行时执行此标签任务)

tagged:在执行时使用ansible-playbook playbook.yml -t tagged

​ 只执行带有标签的任务(带never标签的不执行)

untagged:在执行时使用ansible-playbook playbook.yml -t untagged

​ 只执行不带标签的任务,包含always标签也执行

all:所有任务都执行,默认标签

可以同时使用多个标签,需要用","隔开

powershell 复制代码
[student@master ansible]$ vim a.yml
(1)ansible-playbook a.yml -t l2:执行带l2标签的任务,带always标签任务也执行
powershell 复制代码
[student@master ansible]$ vim a.yml
[student@master ansible]$ ansible-playbook a.yml -t l2 

PLAY [test] ****************************************************************************

TASK [Gathering Facts] *****************************************************************
ok: [node2]

TASK [user1] ***************************************************************************
changed: [node2]

TASK [user2] ***************************************************************************
changed: [node2]

TASK [user4] ***************************************************************************
changed: [node2]

PLAY RECAP *****************************************************************************
node2                      : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
(2)ansible-playbook a.yml -t tagged:执行带标签的任务,不执行带never标签的任务
powershell 复制代码
[student@master ansible]$ ansible-playbook a.yml -t tagged

PLAY [test] ****************************************************************************

TASK [Gathering Facts] *****************************************************************
ok: [node2]

TASK [user1] ***************************************************************************
ok: [node2]

TASK [user2] ***************************************************************************
ok: [node2]

TASK [user3] ***************************************************************************
changed: [node2]

TASK [user4] ***************************************************************************
ok: [node2]

PLAY RECAP *****************************************************************************
node2                      : ok=5    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
(3)ansible-playbook a.yml -t untagged:执行不带标签的任务(这里只执行always)
powershell 复制代码
[student@master ansible]$ ansible-playbook a.yml -t untagged

PLAY [test] ****************************************************************************

TASK [Gathering Facts] *****************************************************************
ok: [node2]

TASK [user4] ***************************************************************************
ok: [node2]

PLAY RECAP *****************************************************************************
node2                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
(3)ansible-playbook a.yml -t always:执行带always标签的
powershell 复制代码
[student@master ansible]$ ansible-playbook a.yml -t always

PLAY [test] ****************************************************************************

TASK [Gathering Facts] *****************************************************************
ok: [node2]

TASK [user4] ***************************************************************************
ok: [node2]

PLAY RECAP *****************************************************************************
node2                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
(4)ansible-playbook a.yml -t never:执行带never标签的,并且带always标签的也执行
powershell 复制代码
[student@master ansible]$ ansible-playbook a.yml -t never

PLAY [test] ****************************************************************************

TASK [Gathering Facts] *****************************************************************
ok: [node2]

TASK [user4] ***************************************************************************
ok: [node2]

TASK [user5] ***************************************************************************
changed: [node2]

PLAY RECAP *****************************************************************************
node2                      : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
(5)跳过带l2标签的,不执行带never标签的,其余的都执行
powershell 复制代码
[student@master ansible]$ ansible-playbook a.yml --skip-tags l2

PLAY [test] ****************************************************************************

TASK [Gathering Facts] *****************************************************************
ok: [node2]

TASK [user3] ***************************************************************************
ok: [node2]

TASK [user4] ***************************************************************************
ok: [node2]

PLAY RECAP *****************************************************************************
node2                      : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
(6)ansible-playbook a.yml:不执行带never标签的,其余的都执行
powershell 复制代码
[student@master ansible]$ ansible-playbook a.yml

PLAY [test] ****************************************************************************

TASK [Gathering Facts] *****************************************************************
ok: [node2]

TASK [user1] ***************************************************************************
ok: [node2]

TASK [user2] ***************************************************************************
ok: [node2]

TASK [user3] ***************************************************************************
ok: [node2]

TASK [user4] ***************************************************************************
ok: [node2]

PLAY RECAP *****************************************************************************
node2                      : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
相关推荐
范桂飓1 天前
在 Windows GPU 机器上运行 Linux CUDA
linux·运维·windows
小跌—1 天前
Linux:线程池
linux
深思慎考1 天前
LinuxC++项目开发日志——基于正倒排索引的boost搜索引擎(4——通过jsoncpp库建立搜索模块)
linux·c++·搜索引擎
虚伪的空想家1 天前
生产环境K8S的etcd备份脚本
运维·容器·kubernetes·脚本·备份·etcd
蓝婴天使1 天前
Debian13 钉钉无法打开问题解决
linux·服务器·钉钉
程序员三明治1 天前
Linux安装Kafka(无Zookeeper模式)保姆级教程,云服务器安装部署,Windows内存不够可以看看
linux·zookeeper·kafka
zz-zjx1 天前
Nginx负载均衡算法与IP透传、跨域实战指南
linux·nginx·负载均衡
誰能久伴不乏1 天前
Linux Shell 脚本:从零到进阶的实战笔记
linux·chrome·笔记
宁雨桥1 天前
保姆级教程:windows和linux双系统的电脑如何无副作用,安全删除linux
linux·windows·电脑
孙克旭_1 天前
kind部署K8S集群并将“修仙业务“部署到kind集群
linux·运维·云原生·kubernetes·kind