day 40-集群架构-自动化管理yaml基本的playbook

day 40-集群架构-自动化管理yaml基本的playbook

ansible执行命令后输出信息中:

绿色——表示查询,或者没有发生任何改变

红色——表示命令操作出现异常

屎黄色——对远程主机做了相应改动

粉色——对操作提出建议或忠告

8、ansible的剧本:playbook

1.什么是playbook?

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

2.ansible剧本编写格式说明

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

1.什么是playbook,

playbook翻译过来就是“剧本”,那playbook组成如下

play:定义的是主机的角色

task:定义的是具体执行的任务

playbook:由一个或多个play组成, 一个play可以包含多个task任务简单理解为:使用不同的模块完成一件事情

2.playbook的优势

1) 功能比ad-hoc 更全

2) 能很好的控制先后执行顺序,以及依赖关系

3) 语法展现更加的直观

4) ad-hoc无法持久使用, playbook可以持久使用

3.playbook的配置语法是由yaml语法描述的,扩展名是yaml

规则一:缩进

yaml使用一个固定的缩进风格表示数据层结构关系,需要每个缩进级别由两个空格组成。切记一定不能使用tab键进行缩进。

规则二:冒号

每个冒号后面一定要有一个空格(以冒号结尾不需要空格,表示文件路径的模版可以不需要空格)

规则三:短横线

想要表示列表项,使用一个短横杠加一个空格。多个项使用同样的缩进级别作为同一个列表的一部分

3.playbook替代方案

playbook替代方案1:

[rootem01 ~# cat ansible.sh #判断循环,可以脚本实现。

ansible oldboy-m file -a "dest=/tmp/oldboy file state-touch"

ansible oldboy-m file -a "dest=/tmp/oldboy file state=touch owner=oldboy group-oldboy mode=ugo=rwx"

ansible oldboy -m yum -a "name=nginx state-installed"

ansible oldboy -m service-a "name-crond state=started enabled=yes"

ansible oldboy-m cron-a "name='sync time' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'"

playbook替代方案2:

[root@m01 ~]# cat ~/set.sh

touch /tmp/oldboy_file

chown oldboy.oldboy /tmp/oldboy_file

yum install nginx -y

/etc/init.d/crond start

chkconfig cornd on

echo '#sync time oldboy' >>/var/spool/cron/root

echo '00 00 * * *  /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1' >>/var/spool/cron/root

执行:

ansible oldboy -m script -a "~/set.sh"

4.实践:

[root@m01 /server/scripts]# cd /etc/ansible/

[root@m01 /etc/ansible/yaml]# mkdir -p /etc/ansible/yaml

[root@m01 /etc/ansible]# cd yaml/

[root@m01 /etc/ansible/yaml]# vim p1.yml

- hosts: oldboy

  tasks:

    - name: Create a log file

      shell: echo oldboy linux. >/tmp/oldboy.log

[root@m01 /etc/ansible/yaml]# cat p1.yml

- hosts: oldboy  #--是hosts/下的IP名字

  tasks:        #--要执行的命令

    - name: Create a log file  #---注释(相当于#!/bin/sh)

      shell: echo oldboy linux. >/tmp/oldboy.log 

[root@m01 /etc/ansible/yaml]# ansible-playbook -C /etc/ansible/yaml/p1.yml#--加-C是测试执行 去掉就直接执行

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

PLAY [oldboy] **********************************************************************************************

TASK [Gathering Facts] *************************************************************************************

ok: [172.16.1.41]

ok: [172.16.1.31]

ok: [172.16.1.7]

ok: [172.16.1.8]

TASK [Create a log file] ***********************************************************************************

changed: [172.16.1.31]

changed: [172.16.1.8]

changed: [172.16.1.7]

changed: [172.16.1.41]

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 

172.16.1.7                : ok=2    changed=1    unreachable=0    failed=0 

172.16.1.8                : ok=2    changed=1    unreachable=0    failed=0 

[root@m01 /etc/ansible/yaml]# vim p2.yml

- hosts: oldboy

  remote_user: root

  tasks:

    - name: Create a log file

      file: name=/tmp/oldboy1 state=touch

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

PLAY [oldboy] ******************************************************************************

TASK [Gathering Facts] *********************************************************************

ok: [172.16.1.41]

ok: [172.16.1.8]

ok: [172.16.1.7]

ok: [172.16.1.31]

TASK [Create a log file] *******************************************************************

changed: [172.16.1.7]

changed: [172.16.1.8]

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 

172.16.1.7                : ok=2    changed=1    unreachable=0    failed=0 

172.16.1.8                : ok=2    changed=1    unreachable=0    failed=0

定时任务时间参数:

      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.

minute:分

hour:时

day:日

month:月

weekday:周  

job: (job='/usr/sbin/ntpdat')意思就是加上全路径

[root@m01 /etc/ansible/yaml]# vim p3.yml

- hosts: oldboy

  tasks:

    - name: Cron time sync

      cron: name='sync time' minute=*/5 job='/usr/sbin/ntpdat'

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

_______________

< PLAY [oldboy] >

---------------

        \  ^__^

        \  (oo)\_______

            (__)\      )\/\

                ||----w |

                ||    ||

________________________

< TASK [Gathering Facts] >

------------------------

        \  ^__^

        \  (oo)\_______

            (__)\      )\/\

                ||----w |

                ||    ||

ok: [172.16.1.7]

ok: [172.16.1.31]

ok: [172.16.1.8]

ok: [172.16.1.41]

_______________________

< TASK [Cron time sync] >

-----------------------

        \  ^__^

        \  (oo)\_______

            (__)\      )\/\

                ||----w |

                ||    ||

changed: [172.16.1.8]

changed: [172.16.1.41]

changed: [172.16.1.7]

changed: [172.16.1.31]

____________

< PLAY RECAP >

------------

        \  ^__^

        \  (oo)\_______

            (__)\      )\/\

                ||----w |

                ||    ||

172.16.1.31                : ok=2    changed=1    unreachable=0    failed=0 

172.16.1.41                : ok=2    changed=1    unreachable=0    failed=0 

172.16.1.7                : ok=2    changed=1    unreachable=0    failed=0 

172.16.1.8                : ok=2    changed=1    unreachable=0    failed=0

练习题:每周一1-5 上午8:30 /server/scripts/class.sh

[root@m01 /etc/ansible/yaml]# vim p4.yml

- hosts: oldboy

  tasks:

    - name: Cron time sync

      cron: name='qushangke' minute=30 hour=08 weekday=1-5 job='/bin/sh /server/scripts/class.sh >/dev/null 2>&1'

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

练习题:

ansible oldboy -m copy -a "src=/etc/rsyncd.conf dest=/etc/rsyncd.conf backup=yes"

ansible oldboy -m copy -a "content='rsync_backup:oldboy' dest=/etc/rsync.password backup=yes mode=0600"

编写剧本:

[root@m01 /etc/ansible/yaml]# vim p5.yml

- hosts: backup

  tasks:

    - name: copy rsyncd.conf

      copy: src=/etc/rsyncd.conf dest=/etc/rsyncd.conf mode=0600 backup=yes

      name: copy rsyncd.password

      copy: content='rsync_backup:oldboy' dest=/etc/rsync.password mode=0600

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

你可能感兴趣的:(day 40-集群架构-自动化管理yaml基本的playbook)