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

相关推荐

  • N26-第四周作业

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 [root@localhost ~]# cp -rfp /etc/skel/ /home/tuser1[root@localhost ~]# chmod 700 /home/tuser1 2、编辑/etc/group文件,添…

    Linux干货 2017-02-25
  • iptables

    一、前言 什么是iptables?当我们启动iptables时,使用service命令可以启动iptables。但是并非使用service启动的iptables就能说明其是一个服务。Iptables是一个便以我们写规则的工具,真正起作用的是内核中的netfilter一个框架。Netfilter内置了5个hook函数,当一个数据包交由此机器时,经过这5个hoo…

    Linux干货 2015-10-27
  • RAID常见级别及特性

    磁盘阵列(Redundant Arrays of Independent Disks,RAID),通过软件或者硬件的手段将多个磁盘整合成一个磁盘来使用,以此来提高磁盘的性能,并提供数据冗余的功能。 目前常见的RAID等级: raid0,被称为条带卷。 条带卷,我们可以通过名字来想象:RAID0通过把文件切割之后把数据像一条带子一样平铺在每个磁盘之上。 由于文…

    Linux干货 2016-01-05
  • linux下zip包的压缩与解压

    linux zip 命令详解  功能说明:压缩文件。  语 法:zip [-AcdDfFghjJKlLmoqrSTuvVwXyz$][-b <工作目录>][-ll][-n <字尾字符串>][-t <日期时间>][-<压缩效率>][压缩文件][文件…][-i <范本样式&gt…

    Linux干货 2017-04-10
  • Linux Basics

    计算机的组成及其功能:             CPU:运算器、控制器、寄存器、缓存             存储器:内存,RAM(Random Access Memory)   …

    Linux干货 2016-09-17
  • Nginx 基础 (IO模型、编译安装、几大块配置文件详解)

    Nginx基础 前言 apache在设计的时候已经考虑了并发访问模型,select()机制可以响应1024个访问,但是当数量再大,千万级别的时候http就响应不过来了。这个时候,nginx的出现解决了这一个问题。nginx是一个安装简单、配置文件简单、占用内存少、稳定性高、处理并发能力非常强、灵活好用等有点聚集于一身的轻量级服务器。在Linux操作系统中,N…

    Linux干货 2016-12-24