yxsailing 发表于 2018-7-30 11:46:34

ansible模块使用

#查看客户端的文件  
# ansible web -m shell -a 'ls -l /tmp |grep test'
  
192.168.3.45 | success | rc=0 >>
  
-rw-r--r-- 1 root root   7 Jul3 13:29 test-server-1
  
-rw-r--r-- 1 root root   7 Jul3 11:07 test-server-2
  
-rw-r--r-- 1 root root   7 Jul3 11:07 test-server-3
  

  
192.168.3.46 | success | rc=0 >>
  
-rw-r--r-- 1 root root   7 Jul3 13:29 test-server-1
  
-rw-r--r-- 1 root root   7 Jul3 11:07 test-server-2
  
-rw-r--r-- 1 root root   7 Jul3 11:07 test-server-3
  

  
#编写带有tag的playbook
  
# cat /etc/ansible/delete_tags.yml
  
- hosts: "{{host}]"
  
remote_user: "`user`"
  
gather_facts: "`gather`"
  
tasks:
  
    - name: if system is centos,then rm /tmp/test-server-1
  
      shell: rm -rf /tmp/test-server-1
  
      tags: server-1
  
    - name: if system is centos,them rm /tmp/test-server-2
  
      shell: rm -rf /tmp/test-server-2
  
      tags: server-2
  

  
#执行playbooks
  
# ansible-playbook /etc/ansible/delete_tags.yml --extra "host=web user=root gather=True",未指定tags
  

  
PLAY ********************************************************************
  

  
GATHERING FACTS ***************************************************************
  
ok:
  
ok:
  

  
TASK: ************************
  
changed:
  
changed:
  

  
TASK: ************************
  
changed:
  
changed:
  

  
PLAY RECAP ********************************************************************
  
192.168.3.45               : ok=3    changed=2    unreachable=0    failed=0
  
192.168.3.46               : ok=3    changed=2    unreachable=0    failed=0
  

  
#查看结果
  
#从下面的结果中我们能看出,在不指定tags的情况先客户端将2个任务都执行了,即在客户端上删除了/tmp/test-server-1和/tmp/test-server-2这2个文件
  
# ansible web -m shell -a 'ls -l /tmp |grep test'
  
192.168.3.46 | success | rc=0 >>
  
-rw-r--r-- 1 root root   7 Jul3 11:07 test-server-3
  

  
192.168.3.45 | success | rc=0 >>
  
-rw-r--r-- 1 root root   7 Jul3 11:07 test-server-3
  

  
#我们将客户端的文件恢复
  
# ansible web -m shell -a 'ls -l /tmp |grep test'
  
192.168.3.45 | success | rc=0 >>
  
-rw-r--r-- 1 root root   7 Jul3 14:06 test-server-1
  
-rw-r--r-- 1 root root   7 Jul3 14:06 test-server-2
  
-rw-r--r-- 1 root root   7 Jul3 11:07 test-server-3
  

  
192.168.3.46 | success | rc=0 >>
  
-rw-r--r-- 1 root root   7 Jul3 14:06 test-server-1
  
-rw-r--r-- 1 root root   7 Jul3 14:06 test-server-2
  
-rw-r--r-- 1 root root   7 Jul3 11:07 test-server-3
  

  
#执行playbook,指定运行tags:server-2
  
#正常情况下是只删除/tmp/test-server-2这个文件,/tmp/test-server-1这个文件是不会删除的
  
# ansible-playbook /etc/ansible/delete_tags.yml --extra "host=web user=root gather=True" --tags server-2
  

  
PLAY ********************************************************************
  

  
GATHERING FACTS ***************************************************************
  
ok:
  
ok:
  

  
TASK: ************************
  
changed:
  
changed:
  

  
PLAY RECAP ********************************************************************
  
192.168.3.45               : ok=2    changed=1    unreachable=0    failed=0
  
192.168.3.46               : ok=2    changed=1    unreachable=0    failed=0
  

  
# ansible web -m shell -a 'ls -l /tmp |grep test'
  
192.168.3.46 | success | rc=0 >>
  
-rw-r--r-- 1 root root   7 Jul3 14:06 test-server-1
  
-rw-r--r-- 1 root root   7 Jul3 11:07 test-server-3
  

  
192.168.3.45 | success | rc=0 >>
  
-rw-r--r-- 1 root root   7 Jul3 14:06 test-server-1      #该文件还存在
  
-rw-r--r-- 1 root root   7 Jul3 11:07 test-server-3
  
#总结,如果palybooks带有tags,不指定任何tags,默认会执行所有的任务。如果指定了tags,只执行指定的tags任务,其余的tags任务不会执行
页: [1]
查看完整版本: ansible模块使用