ansible playbook介绍

时间:2022-05-11
本文章向大家介绍ansible playbook介绍,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

ansible playbook介绍

  • 例子
 ---- hosts: web  remote_user: root  tasks:    - name: test_playbook      shell: touch /tmp/qqq.txt
 [root@node01 ~]# ansible-playbook test.yml PLAY [web] ************************************************************************TASK [Gathering Facts] ************************************************************ok: [192.168.6.72]ok: [192.168.6.71]TASK [test_playbook] ************************************************************** [WARNING]: Consider using file module with state=touch rather than running touchchanged: [192.168.6.72]changed: [192.168.6.71]PLAY RECAP ************************************************************************192.168.6.71               : ok=2    changed=1    unreachable=0    failed=0   192.168.6.72               : ok=2    changed=1    unreachable=0    failed=0   
  • 创建一个用户
 ---- name: create_user  hosts: web  user: root  gather_facts: false  vars:    - user: "test"  tasks:    - name: create user      user: name="{{ user }}"
 [root@node01 ~]# ansible-playbook test.yml PLAY [create_user] ****************************************************************TASK [create user] ****************************************************************changed: [192.168.6.72]changed: [192.168.6.71]PLAY RECAP ************************************************************************192.168.6.71               : ok=1    changed=1    unreachable=0    failed=0   192.168.6.72               : ok=1    changed=1    unreachable=0    failed=0   
  • ansible playbook循环
 ---- name: xunhuan  hosts: web  user: root  tasks:    - name: change mod for file      file: path=/tmp/{{ item }} mode=600 owner=root group=root      with_items:        - 1.txt        - 2.txt
 [root@node01 ~]# ansible-playbook xunhuan.yml PLAY [xunhuan] ********************************************************************TASK [Gathering Facts] ************************************************************ok: [192.168.6.72]ok: [192.168.6.71]TASK [change mod for file] ********************************************************changed: [192.168.6.72] => (item=1.txt)changed: [192.168.6.71] => (item=1.txt)changed: [192.168.6.72] => (item=2.txt)changed: [192.168.6.71] => (item=2.txt)PLAY RECAP ************************************************************************192.168.6.71               : ok=2    changed=1    unreachable=0    failed=0   192.168.6.72               : ok=2    changed=1    unreachable=0    failed=0   
  • ansible playbook条件判断
 ---- hosts: web  remote_user: root  gather_facts: True  tasks:    - name: use when      shell: touch /tmp/when.txt      when: ansible_eth0.ipv4.address == "192.168.6.71"
 [root@node01 ~]# ansible-playbook panduan.yml PLAY [web] ************************************************************************TASK [Gathering Facts] ************************************************************ok: [192.168.6.71]ok: [192.168.6.72]TASK [use when] *******************************************************************skipping: [192.168.6.72] [WARNING]: Consider using file module with state=touch rather than running touchchanged: [192.168.6.71]PLAY RECAP ************************************************************************192.168.6.71               : ok=2    changed=1    unreachable=0    failed=0   192.168.6.72               : ok=1    changed=0    unreachable=0    failed=0   
  • handlers
 ---- name: handlers test  hosts: web  user: root  tasks:    - name: copy file      copy: src=/etc/passwd dest=/tmp/aa.txt      notify: test handlers  handlers:    - name: test handlers      shell: echo "1111111" >> /tmp/aa.txt
 [root@node01 ~]# ansible-playbook handlers.ymlPLAY [handlers test] **************************************************************TASK [Gathering Facts] ************************************************************ok: [192.168.6.72]ok: [192.168.6.71]TASK [copy file] ******************************************************************changed: [192.168.6.72]changed: [192.168.6.71]RUNNING HANDLER [test handlers] ***************************************************changed: [192.168.6.72]changed: [192.168.6.71]PLAY RECAP ************************************************************************192.168.6.71               : ok=3    changed=2    unreachable=0    failed=0   192.168.6.72               : ok=3    changed=2    unreachable=0    failed=0