2019-04-25

12.8 playbook

12.8.1 什么是playbook?

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

 12.8.2 ansible剧本编写格式说明

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

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

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

规则三:短横线 想要表示列表项,使用一个短横杠加一个空格。

多个项使用同样的缩进级别作为同一个列表的一部分

1.YAML介绍 YAML是一个可读性高的用来表达资料序列的格式。

YAML参考了其他多种语言,包括:XML、C语言、Python、Perl以及电子邮件格式RFC2822等。

Clark Evans在2001年在首次发表了这种语言,另外Ingy dt Net与Oren Ben-Kiki也是这语言的共同设计

者。

YAML Ain't Markup Language,即YAML不是XML。

不过,在开发的这种语言时,YAML的意思其实是:"Yet Another Markup Language"(仍是一种标记语言)。

其特性: YAML的可读性好 YAML和脚本语言的交互性好 YAML使用实现语言的数据类型 YAML有一个一

致的信息模型 YAML易于实现 YAML可以基于流来处理 YAML表达能力强,扩展性好 更多的内容及规范参

见http://www.yaml.org。

 12.8.3 playbook替代方案 playbook

替代方案1:不同样

 [root@m01 ~]# 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"

12.8.4实践

 ansible oldboy -m shell -a "echo oldboy linux. >/tmp/oldboy.log"

把这行命令写成剧本: 准备:

mkdir -p /etc/ansible/yaml cd /etc/ansible/yaml vim p1.yml

 开始编写:

[root@m01 /etc/ansible/yaml]# cat /etc/ansible/yaml/p1.yml - hosts: oldboy tasks: - name: Create a log

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

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

- hosts: oldboy

   tasks:

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

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

说明:利用ansibl-playbook命令执行剧本,-C参数表示测试剧本任务执行,类似话剧的彩排一样。

PLAY [oldboy] *********************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************** ok: [172.16.1.41] ok: [172.16.1.31] TASK [Create a log file] ************************************************************************************************ 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.yml PLAY [oldboy] *********************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************** ok: [172.16.1.31] ok: [172.16.1.41] TASK [Create a log file] ************************************************************************************************ changed: [172.16.1.31] 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 ansible oldboy -m file -a "dest=/tmp/oldboy1 state=touch"

编写剧本:

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

- hosts: oldboy remote_user: root

tasks:

- name: Create New File

file: name=/tmp/oldboy1 state=touch

定时任务:

ansible oldboy -m cron -a "name='sync time' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'" #crond-id-001:time sync by oldboy */5 * * * * /usr/sbin/ntpdate ntp3.aliyun.com >/dev/null 2>&1

编写剧本:

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

- hosts: oldboy

 tasks:

- name: Cron time sync

cron: name='sync time' minute=*/10 job='/usr/sbin/ntpdate ntp3.aliyun.com >/dev/null 2>&1'

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

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

- hosts: oldboy

tasks:

- name:

Cron

1、Linux命令行操作 #3)创建用户和备份目录

 useradd rsync

 id rsync

mkdir -p /backup

chown -R rsync.rsync /backup/

 ls -ld /backup/

 #4)启动和检查

systemctl start rsyncd

 systemctl enable rsyncd

systemctl status rsyncd

2、ansible命令

ansible oldboy -m systemd -a "name=crond.service enabled=no state=stopped " ansible oldboy -m command -a "systemctl status crond" ansible oldboy -m systemd -a "name=crond.service enabled=yes state=started"

 3、编写剧本:

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

- hosts: backup

 tasks:

- name: copy rsyncd.conf

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

 - name: create rsync.password

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

 - name: create

user 用user模块 替代 command: useradd rsync -s /sbin/nologin -M - name: create dir

用file模块替代 command: mkdir -p /backup - name: shouquan

用file模块替代 command: chown -R rsync.rsync /backup/ - name: startup rsyncd systemd: name='rsyncd.service' state=restarted enabled=yes

执行三部曲: ansible-playbook --syntax-check /etc/ansible/yaml/p6.yml ansible-playbook -C /etc/ansible/yaml/p6.yml ansible-playbook /etc/ansible/yaml/p6.yml

检测结果: rsync -avz /etc/hosts [email protected]::backup --password-file=/etc/rsync.password 今晚25日项目实践一分钟内,

一键完成三个项目的安装: 使用playbook完成如下配置:

1)各一键完成rsync服务端和客户端。 #完成

 2)各一键完成nfs服务端和客户端。

3)各一键完成sersync服务端和客户端。

一个脚本one_key.sh或者一个ansible命令。

完成

- name: This command will change the working directory to somedir/ and will only run when /path/to/database doesn't exist. command: /usr/bin/make_database.sh arg1 arg2 args: chdir: somedir/ creates: /path/to/database

你可能感兴趣的:(2019-04-25)