playbooks配置文件:
[root@ansible ~]# vim /etc/ansible/hosts
[test01] 192.168.200.114 [test02] 192.168.200.115
[root@ansible ~]# vim /etc/ansible/test.yaml --- - hosts: test01 remote_user: root tasks: - name: adduser user: name=user2 state=present tage: - testaaa - name: addgroup group: name=root system=yes tags: - testbbb - hosts: test02 remote_user: root tasks: - name: xxx copy: src=/etc/passwd dest=/home tags: - testccc
用法:
playbook文件定义的任务需要通过nasible-playbook命令调用并执行,ansible-playbook命令用法如下:
用法:ansible-playbook[option]/PATH/TO/PLAYBOOK.yaml
实验案例:
1:语法检查:
[root@ansible ~]# ansible-playbook --Syntax-check /etc/ansible/test.yml playbook: /etc/ansible/test.yml
2:预测试:
[root@ansible ~]# ansible-playbook -C /etc/ansible/test.yml PLAY [test01] *************************************************************************************************************************** TASK [Gathering Facts] ****************************************************************************************************************** ok: [192.168.200.114] TASK [adduser] ************************************************************************************************************************** changed: [192.168.200.114] TASK [addgroup] ************************************************************************************************************************* ok: [192.168.200.114] PLAY [test02] *************************************************************************************************************************** TASK [Gathering Facts] ****************************************************************************************************************** ok: [192.168.200.115] TASK [xxx] ****************************************************************************************************************************** changed: [192.168.200.115] PLAY RECAP ****************************************************************************************************************************** 192.168.200.114 : ok=3 changed=1 unreachable=0 Failed=0 192.168.200.115 : ok=2 changed=1 unreachable=0 Failed=0
3:列出主机:
[root@ansible ~]# ansible-playbook --list-hosts /etc/ansible/test.yml playbook: /etc/ansible/test.yml play #1 (test01): test01 TAGS: [] pattern: [u'test01'] hosts (1): 192.168.200.114 play #2 (test02): test02 TAGS: [] pattern: [u'test02'] hosts (1): 192.168.200.115
4:列出任务:
[root@ansible ~]# ansible-playbook --list-tasks /etc/ansible/test.yml playbook: /etc/ansible/test.yml play #1 (test01): test01 TAGS: [] tasks: adduser TAGS: [testaaa] addgroup TAGS: [testbbb] play #2 (test02): test02 TAGS: [] tasks: xxx TAGS: [testccc]
5:列出标签:
[root@ansible ~]# ansible-playbook --list-tags /etc/ansible/test.yml playbook: /etc/ansible/test.yml play #1 (test01): test01 TAGS: [] TASK TAGS: [testaaa, testbbb] play #2 (test02): test02 TAGS: [] TASK TAGS: [testccc]
6:执行任务:
[root@ansible ~]# ansible-playbook /etc/ansible/test.yml PLAY [test01] *************************************************************************************************************************** TASK [Gathering Facts] ****************************************************************************************************************** ok: [192.168.200.114] TASK [adduser] ************************************************************************************************************************** ok: [192.168.200.114] TASK [addgroup] ************************************************************************************************************************* ok: [192.168.200.114] PLAY [test02] *************************************************************************************************************************** TASK [Gathering Facts] ****************************************************************************************************************** ok: [192.168.200.115] TASK [xxx] ****************************************************************************************************************************** ok: [192.168.200.115] PLAY RECAP ****************************************************************************************************************************** 192.168.200.114 : ok=3 changed=0 unreachable=0 Failed=0 192.168.200.115 : ok=2 changed=0 unreachable=0 Failed=0
7:测试查看:
[root@ansible ~]# ansible test01 -m shell -a "tail -l /etc/passwd" 192.168.200.114 | SUCCESS | rc=0 >> user2:x:1001:1001::/home/user2:/bin/bash
[root@ansible ~]# ansible test02 -m command -a "ls -l /home"
192.168.200.115 | SUCCESS | rc=0 >>
总用量 4
drwx------. 3 crushlinx crushlinx 78 10月 28 09:53 crushlinx
-rw-r--r--. 1 root root 2304 10月 28 16:57 passwd
drwx------. 3 room room 78 4月 11 2018 room
8:执行输出:
我们在用playbook进行ansible模块操作的时候,并没有命令的执行结果输出,默认被隐藏了,可以通过refister模块追加命令输出的结果
[root@ansible ~]# vim /etc/ansible/test.yml --- - hosts: test01 remote_user: root tasks: - name: adduser user: name=user2 state=present register: print_result tags: - testaaa - debug: var=print_result - name: addgroup group: name=root system=yes tags: - testbbb - debug: var=print_result - hosts: test02 remote_user: root tasks: - name: xxx copy: src=/etc/passwd dest=/home tags: - testccc ...
[root@ansible ~]# ansible-playbook /etc/ansible/test.yml PLAY [test01] *************************************************************************************************************************** TASK [Gathering Facts] ****************************************************************************************************************** ok: [192.168.200.114] TASK [adduser] ************************************************************************************************************************** ok: [192.168.200.114] TASK [debug] **************************************************************************************************************************** ok: [192.168.200.114] => { "print_result": { "append": false, "changed": false, "comment": "", "Failed": false, "group": 1001, "home": "/home/user2", "move_home": false, "name": "user2", "shell": "/bin/bash", "state": "present", "uid": 1001 } } TASK [addgroup] ************************************************************************************************************************* ok: [192.168.200.114] TASK [debug] **************************************************************************************************************************** ok: [192.168.200.114] => { "print_result": { "append": false, "changed": false, "comment": "", "Failed": false, "group": 1001, "home": "/home/user2", "move_home": false, "name": "user2", "shell": "/bin/bash", "state": "present", "uid": 1001 } } PLAY [test02] *************************************************************************************************************************** TASK [Gathering Facts] ****************************************************************************************************************** ok: [192.168.200.115] TASK [xxx] ****************************************************************************************************************************** ok: [192.168.200.115] PLAY RECAP ****************************************************************************************************************************** 192.168.200.114 : ok=5 changed=0 unreachable=0 Failed=0 192.168.200.115 : ok=2 changed=0 unreachable=0 Failed=0
2:触发器:
hanglers触发器的使用示例如下:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。