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

相关推荐

  • 利用varnish构建httpd缓存服务器

    varnish如何存储缓存对象:     file: 单个文件;不支持持久机制;     malloc: 缓存在内存中;     persistent:基于文件的持久存储;(此方式不建议使用) vcl:配置缓存系统的缓存机制;【线程中缓存功…

    Linux干货 2016-06-20
  • 每天一个Linux命令

        程序管理:      程序的组成部分:二进制程序文件,库文件,配置文件,帮助文件。                      二进制和库文件可执行,库文件不能独立执行,只能被调用时执…

    Linux干货 2017-04-08
  • 基于mysql的数据库分析系统(rsyslog)

    rsyslog:日志收集和存储系统 1.事件在电脑中的日志记录格式为:     日期时间 主机 进程[pid]:事件内容 2.rsyslog的特性:     多线程;     UDP,TCP,SSL/TLS,RELP; &nbsp…

    Linux干货 2016-10-23
  • GlusterFS测试报告-02(结合samba为windows提供服务)

    在挂载glusterFS的客户机的目录下,使用samba分享给windows机器使用 1、samba服务的安装 [root@client01 ~]# yum -y install samba [root@client01 ~]# /etc/init.d/smb restart S…

    Linux干货 2016-07-22
  • 第一周总结(linux系统初识和简单命令)

    一、linux操作系统初识         了解操作系统之前我们需要先了解一下计算机的硬件组成,因为操作系统都是运行在硬件之上。遵循冯诺依曼体系,计算机是由控制器、运算器、存储器、输入设备、输出设备五部分组成。对应到我们现在使用计算机上硬件分别为:        &nb…

    Linux干货 2016-12-26
  • echo命令使用详解

        echo命令用于在shell中打印shell变量的值,或者直接输出指定的字符串。linux的echo命令,在shell编程中极为常用,在终端下打印变量的时候也是常常用到的,因此有必要了解下echo的用法。 语法: • 语法: echo [-neE][字符串]   命令选项:   &…

    Linux干货 2016-08-22