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

相关推荐

  • linux基础知识之nmcli

           CentOS7才有的功能网络组:将多块网卡聚合在一起的方法,从而实现冗错和提高吞吐量        网络组不同于旧版中的bonding技术,提供了更好的性能和扩展性        网路组由内核驱动和teamd守护进程实现 &n…

    Linux干货 2016-09-07
  • N26-博客作业-week2

    1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。   cp:copy 复制文件和路径 cp [OPTION]… [-T] SOURCE DEST cp [OPTION]… SOURCE… DIRECTORY cp [OPTION]… -t DIRECTORY SOURCE&…

    Linux干货 2017-02-12
  • 马哥教育网络班21期+第五周博客作业

    1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行; [root@C67-X64-A0 ~]# grep "^[[:space:]]\+" /boot/grub/grub.conf      root (hd0,0…

    Linux干货 2016-08-05
  • Linux风水学

    Linux风水学童 有的没的说一说 说一个,到一个。想起哪个说哪个,说起哪个都不错。 对于接触过一点点Linux的风水学童的我来说,还在苦背天地玄黄,宇宙洪荒。虽然不懂是干什么的,但是心里总有个声音告诉我,这些鬼画符肯定有用。总有一天我能成为一个合格的Linux风水师,去某个员外家里施展堪舆之术被奉为上宾。 Linux流派大概   相对于目前的流派…

    2017-07-18
  • 马哥linux0803课堂笔记

    课堂笔记   重定向的几种方式 标准输入 0 (stdin) 标准输出 1 (stdout) 标准错误 2 (stderr)   标准输入的重定向方法 cat < file cat << string (多行重定向) xxx aaa string结束 cat file | cat   标准输出的重定向 ls e…

    Linux干货 2016-08-08
  • Linux基础之—基础权限和特殊权限以及FACL权限管理

    Linux是多用户多任务的操作系统,了解掌握Linux的权限分配机制,也是管理Linux系统安全的基础之一。 文件目录的权限格式如下:   1.其中权限位可以看对象到是一个9个占位符,其实分为三位一组: (1) 第一组:对应的是文件或目录的所有者属主权限。owner (2) 第二组:对应的是文件或目录的所属组权限。group (3) 第三组:对应的…

    Linux干货 2016-08-07