ansible命令之palybook应用实践

2019-04-27  本文已影响0人  慕男

playbook

1、什么是playbook?

把所有的操作按照ansible编程语法,放到文件里执行就是playbook。

2、ansible剧本编写格式说明

ansible剧本遵循PYyaml语法规则进行编写,yaml文件基本编写规则如下说明:

规则一:缩进
yaml使用一个固定的缩进风格表示数据层结构关系,需要每个缩进级别由两个空格组成。切记一定不能使用tab键进行缩进。
规则二:冒号
每个冒号后面一定要有一个空格(以冒号结尾不需要空格,表示文件路径的模版可以不需要空格)
规则三:短横线
想要表示列表项,使用一个短横杠加一个空格。多个项使用同样的缩进级别作为同一个列表的一部分

3、playbook替换文案

playbook替换文案1:

[root@m01 /server/scripts]# cat one_key.sh 
ansible r1 -m yum -a "name=rsync state=installed" &&\
ansible r1 -m shell -a "useradd rsync"&&\
ansible r1 -m file -a "path=/backup state=directory owner=rsync group=rsync" &&\
ansible r1 -m copy -a "src=/etc/rsyncd.conf dest=/etc/rsyncd.conf" &&\
ansible n2 -m shell -a "echo rsync_backup:oldboy >/etc/rsync.password" &&\

playbook替换文案2:

4、ansible的实践:

测试执行:(出现绿色,不出现红色;故测试成功。)
[root@m01 /etc/ansible/yaml]# ansible-playbook -C /etc/ansible/yaml/p1.yaml 

PLAY [r1] **********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [172.16.1.31]
ok: [172.16.1.41]

TASK [munan] *******************************************************************
skipping: [172.16.1.41]
skipping: [172.16.1.31]

PLAY RECAP *********************************************************************
172.16.1.31                : ok=1    changed=0    unreachable=0    failed=0   
172.16.1.41                : ok=1    changed=0    unreachable=0    failed=0   

真正执行:(命令提示出现黄色;故执行成功。)

[root@m01 /etc/ansible/yaml]# ansible-playbook  /etc/ansible/yaml/p1.yaml 

PLAY [r1] **********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [172.16.1.41]
ok: [172.16.1.31]

TASK [munan] *******************************************************************
changed: [172.16.1.41]
changed: [172.16.1.31]

PLAY RECAP *********************************************************************
172.16.1.31                : ok=2    changed=1    unreachable=0    failed=0   
172.16.1.41                : ok=2    changed=1    unreachable=0    failed=0   
[root@m01 /etc/ansible/yaml]# cat p2.yaml 
- hosts: r1
  remote_user: root
  tasks:
    - name: munan
      file: name=/tmp/munan state=touch

黄色提示表示成功

[root@m01 /etc/ansible/yaml]# ansible-playbook   /etc/ansible/yaml/p2.yaml 

PLAY [r1] **********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [172.16.1.31]
ok: [172.16.1.41]

TASK [munan] *******************************************************************
changed: [172.16.1.41]
changed: [172.16.1.31]

PLAY RECAP *********************************************************************
172.16.1.31                : ok=2    changed=1    unreachable=0    failed=0   
172.16.1.41                : ok=2    changed=1    unreachable=0    failed=0  

定时任务剧本编写:

[root@m01 /etc/ansible/yaml]# vim p3.yaml
- hosts: r1
  tasks:
    - name: munan
      cron: name='sync time' minute=*/10 job='/usr/sbin/ntpdate ntp3.aliyun.com >/dev/null 2>&1'

注明:

minute:    # Minute when the job should run ( 0-59, *, */2, etc )
 hour:     # Hour when the job should run ( 0-23, *, */2, etc )
day:      # Day of the month the job should run ( 1-31, *, */2, etc )
month:     # Month of the year the job should run ( 1-12, *, */2, etc )
weekday:   # Day of the week that the job should run ( 0-6 for Sunday-Saturday, *, etc )
 job:      # The command to execute or, if env is set, the value of  environment variable. The
command should not contain line   breaks. Required if    state=present.

拷贝文件剧本编写:

[root@m01 /etc/ansible/yaml]# vim p4.yaml 
- hosts: r1
  tasks:
    - copy: src=/etc/rsyncd.conf dest=/etc/rsyncd.conf backup=yes
    - copy: content='rsync_backup:oldboy' dest=/etc/rsync.password backup=yes mode=0600
上一篇 下一篇

猜你喜欢

热点阅读