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 11:42
下一篇 2018-01-14 14:33

相关推荐

  • 马哥教育网络班20期+第2周课程练习

    1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示 文件管理命令:ls、cd、pwd、mkdir、rmdir、cp、rm.、mv、touch、cat、more、less、head、tail 1)ls命令:显示目录文件   -a 显示所有文件,包括隐藏文件  ~]# ls -a /etc.  &n…

    Linux干货 2016-06-23
  • yum详解

        yum(全称为 Yellow dog Updater, Modified)yum相对于rpm来说:rpm为基础包管理器,yum则是rpm的前端工具。能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖性关系,并且一次安装所有依赖的软体包,无须繁琐地一次次下载、安装。,yum无法独立存在,系统不允许有两个…

    Linux干货 2016-08-24
  • shell脚本基础

    1、编程基础     程序:指令+数据   程序编程风格:                         过程式:以指令为中心,数据服务于指令         &nbs…

    2017-04-20
  • N28-第一周

    1. 描述計算機的組成及其功能。
    2. 按系列羅列Linux的發行版,並描述不同發行版之間的聯繫與區別。
    3. 描述Linux的哲學思想,並按照自己的理解對其進行解釋性描述。
    4.說明Linux系統上命令的使用格式;詳細介紹ifconfig、echo、tty、startx、export、pwd、history、shutdown、poweroff、reboot、hwclock、date命令的使用,並配合相應的示例來闡述。
    5.如何在Linux系統上獲取命令的幫助信息,請詳細列出,並描述man文檔的章節是如何劃分的?
    6.請羅列Linux發行版的基礎目錄名稱命名法則及功能規定。

    Linux干货 2017-12-04
  • 编程真难啊

    上周,在Sun的Java论坛上出现了一个这样的帖子,这个贴子的链接如下:http://forums.sun.com/thread.jspa?threadID=5404590&start=0&tstart=0 LZ的贴子翻译如下: 大家好,我是一个Java的新手,我有一个简单的问题:请问我怎么才能反转一个整数的符号啊。比如把-12转成+12。是…

    Linux干货 2015-04-03
  • Keepalive高可用Nginx服务测试

    环境   系统版本:CentOS 7.2  节点1地址:10.1.8.81  节点2地址:10.1.8.83  虚拟IP地址:10.1.8.248 安装软件     yum -y install nginx     yum -y …

    Linux干货 2017-02-16