ansible之Playbook中tags使用

示例:httpd.yml
– hosts: websrvs
remote_user: root
tasks:
– name: Install httpd
yum: name=httpd state=present

– name: Install configure file
copy: src=files/httpd.conf dest=/etc/httpd/conf/
tags: conf

– name: start httpd service
tags: service
service: name=httpd state=started enabled=yes

ansible-playbook –t conf httpd.yml

如果想单独的执行某一个动作,例如只是复制配置文件但是不重启服务
tags就是给任务打个标签,将来只运行标签就可以,不要加空格
长格式 -tags 标签名
短格式 -t 标签名

[root@ansible ~]# vim httpd.conf
#Listen 12.34.56.78:80
Listen 82

修改httpd.yml
[root@ansible ~/ansible]# cat httpd.yml

– hosts: db
remote_user: root

tasks:
– name: install httpd
yum: name=httpd
– name: copy config file
copy: src=/root/httpd.conf dest=/etc/httpd/conf/ backup=yes
notify: restart httpd
tags: copyconf
– name: start httpd
service: name=httpd state=started enabled=yes
handlers:
– name: restart httpd
service: name=httpd state=restarted
[root@ansible ~/ansible]#

测试
[root@ansible ~/ansible]# ansible-playbook -t copyfile httpd.yml -C

[root@ansible ~/ansible]# ansible-playbook –tags copyconf -C httpd.yml

PLAY [db] **********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [172.18.103.28]
ok: [172.18.103.29]

TASK [copy config file] ********************************************************
changed: [172.18.103.29]
ok: [172.18.103.28]

RUNNING HANDLER [restart httpd] ************************************************
changed: [172.18.103.29]

PLAY RECAP *********************************************************************
172.18.103.28 : ok=2 changed=0 unreachable=0 failed=0
172.18.103.29 : ok=3 changed=2 unreachable=0 failed=0

[root@ansible ~/ansible]#

执行
[root@ansible ~/ansible]# ansible-playbook –tags copyconf httpd.yml
PLAY RECAP *********************************************************************
172.18.103.28 : ok=2 changed=0 unreachable=0 failed=0
172.18.103.29 : ok=3 changed=2 unreachable=0 failed=0

查看执行结果
[root@ansible ~/ansible]# ansible 172.18.103.29 -m shell -a “cat /etc/httpd/conf/httpd.conf | grep ‘Listen 8′”
172.18.103.29 | SUCCESS | rc=0 >>
Listen 82

为方便使用可以添加多个标签,支持下划线
[root@ansible ~/ansible]# cat httpd.yml

– hosts: db
remote_user: root

tasks:
– name: install httpd
yum: name=httpd
tags: install
– name: copy config file
copy: src=/root/httpd.conf dest=/etc/httpd/conf/ backup=yes
tags: copyconf
notify: restart httpd
– name: start httpd
tags: start_httpd
service: name=httpd state=started enabled=yes
handlers:
– name: restart httpd
service: name=httpd state=restarted
[root@ansible ~/ansible]#

停止服务,使用tags测试
[root@ansible ~/ansible]# ansible db -m shell -a “systemctl stop httpd”
172.18.103.28 | SUCCESS | rc=0 >>
172.18.103.29 | SUCCESS | rc=0 >>

测试
[root@ansible ~/ansible]# ansible-playbook -t start_httpd httpd.yml

PLAY [db] ************************************************************************

TASK [Gathering Facts] **********************************************************
ok: [172.18.103.29]
ok: [172.18.103.28]

TASK [start httpd] ***************************************************************
ok: [172.18.103.29]
changed: [172.18.103.28]

PLAY RECAP *********************************************************************
172.18.103.28 : ok=2 changed=1 unreachable=0 failed=0
172.18.103.29 : ok=2 changed=0 unreachable=0 failed=0

[root@ansible ~/ansible]#

查看端口
[root@ansible ~/ansible]# ansible db -m shell -a “ss -ntl | grep 82”
172.18.103.29 | SUCCESS | rc=0 >>
LISTEN 0 128 :::82 :::*

172.18.103.28 | SUCCESS | rc=0 >>
LISTEN 0 128 :::82 :::*

停止服务
[root@ansible ~/ansible]# ansible db -m service -a ‘name=httpd state=stopped’

修改端口
[root@ansible ~]# vim httpd.conf
Listen 83

执行多个标签,复制文件,启动服务,顺序不重要,在httpd.yml已经写好了顺序
[root@ansible ~/ansible]# ansible-playbook -t start_httpd,copyconf httpd.yml

查看端口
[root@ansible ~/ansible]# ansible db -m shell -a “ss -ntl | grep 83”
172.18.103.29 | SUCCESS | rc=0 >>
LISTEN 0 128 :::83 :::*

172.18.103.28 | SUCCESS | rc=0 >>
LISTEN 0 128 :::83 :::*

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/91106

(6)
无言胜千言无言胜千言
上一篇 2018-01-14
下一篇 2018-01-14

相关推荐

  • 安装Redmine

    Redmine是一个开源的、基于Web的项目管理和缺陷跟踪工具。它用日历和甘特图辅助项目及进度可视化显示。同时它又支持多项目管理。Redmine是一个自由开放源码软件解决方案,它提供集成的项目管理功能,问题跟踪,并为多个版本控制选项的支持。虽说像IBM Rational Team Concert的商业项目调查工具已经很强大了,但想坚持一个自由和开放源码的解决…

    Linux干货 2016-12-01
  • Linux 磁盘、文件系统管理

    Linux 磁盘、文件系统管理                                               &nb…

    Linux干货 2016-09-01
  • 8月2日作业

            1、在/data/testdir里创建的新文件自动属于g1组,组g2的成员如:alice能对这些新文件有读写权限,组g3的成员如:tom只能对新文件有读权限,其它用户(不属于g1,g2,g3)不能访问这个文件夹。 [root@localhost test…

    Linux干货 2016-08-05
  • keepalived(主主) 结合lvs(dr工作 模式) 实现高可用性。

    keepalived(主主) 结合lvs(dr工作 模式) 实现高可用性。

    2018-03-11
  • SRE管理职责简介

    读书笔记 摘要 SRE是Site Reliability Engineer的简称,从名字可以看出Google的SRE不只是做Operation方面的工作,更多是保障整个Google服务的稳定性。 SRE管理职责简介 监控系统 警报 工单 日志 应急事件处理 变更管理 预测需求和规划容量 资源部署 小结 监控系统 监控系统是 SRE 团队监控服务质量…

    Linux干货 2017-04-03
  • MySQL高可用架构之Galera Cluster

    MySQL高可用架构之Galera Cluster 1、实验准备及拓扑 至少需要三个节点 node1 192.168.150.137 node2 192.168.150.138 node3 192.168.150.139 mariadb版本为mariadb的支持galera cluster的分支版本 MariaDB-Galera-server-5.5.46 …

    Linux干货 2017-03-31