5.ansible 剧本编写规范

三种规范原则:
空格: 缩进信息 两个空格
PS:编写剧本时,将tab键扣掉
冒号: key-value(键值关系) hosts: 172.16.1.41
冒号后面不需要有空格情况:
01. 以冒号结尾
02. 冒号信息出现在注释信息中
短横线:- 生成列表信息
- hosts: 172.16.1.41
- hosts: 172.16.1.31
1.编写示例

cat test.yaml 
- hosts: 192.168.1.203
  tasks:
    - yum: name=cowsay state=installed
    - copy: src=/etc/hosts dest=/tmp/hosts.bak remote_src=yes

2.语法检查 检查语法有没有错误

ansible-playbook --syntax-check test.yaml 

playbook: test.yaml

3.剧本模拟执行 检查剧本是否能执行

ansible-playbook -C  test.yaml 

PLAY [192.168.1.203] ****************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************
ok: [192.168.1.203]

TASK [yum] **************************************************************************************************************************************************
changed: [192.168.1.203]

TASK [copy] *************************************************************************************************************************************************
ok: [192.168.1.203]

PLAY RECAP **************************************************************************************************************************************************
192.168.1.203              : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

4.剧本执行

ansible-playbook test.yaml 

PLAY [192.168.1.203] ****************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************
ok: [192.168.1.203]

TASK [yum] **************************************************************************************************************************************************
changed: [192.168.1.203]

TASK [copy] *************************************************************************************************************************************************
ok: [192.168.1.203]

PLAY RECAP **************************************************************************************************************************************************
192.168.1.203              : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

rsync 示例

cat rsync.yaml

- hosts: 172.16.1.41
  tasks:
    - name: 01:install rsync software
      yum: name=rsync state=installed
    - name: 02:push client
      copy: src=./rsyncd.conf dest=/etc/
    - name: 03:create user
      user: name=rsync shell=/sbin/nologin create_home=no
    - name: 04:create password file
      copy: content='rsync_backup:oldboy123' dest=/etc/rsync.password mode=600
    - name: 05:create backup dir
      file: path=/backup state=directory owner=rsync group=rsync
    - name: 06:重启rsync服务  
      service: name=rsyncd state=restarted enabled=yes   

- hosts: rsync_client
  tasks:
    - name: 01:ceate password file
      copy: content='oldboy123' dest=/etc/rsync.password mode=600
原文地址:https://www.cnblogs.com/yangtao416/p/14592365.html