1、playbook
什么是playbook?
把所有操作按照ansible编成语法,放在文件里执行就是playbook。
2、ansible剧本编写格式说明
ansible剧本遵循PYyaml语法规则进行编写,yaml
规则一:缩进
yaml使用一个固定的
会泽二:冒号
规则三:短横线
想要表示列表项,使用一个短横杠加一个空格。多个项使用同样的缩进级别
实践:编写剧本
实践1
ansible oldboy -m shell -a "echo oldboy linux. >/tmp/oldboy.log"
把这行命令写成剧本:
mkdir -p /etc/ansible/yaml
cd /etc/ansible/yaml
vim p1.yml
开始编写:
[root@waiwai /etc/ansible/yaml]# cat p1.yml
- hosts: oldboy
tasks:
- name: Create alog file
shell: echo oldboy linux. >/tmp/oldboy.log
执行
[root@waiwai /etc/ansible/yaml]# ansible-playbook -C /etc/ansible/yaml/p1.yml
PLAY [oldboy] **************************************************************************
TASK [Gathering Facts] *****************************************************************
ok: [172.16.1.41]
ok: [172.16.1.31]
TASK [Create alog file] ****************************************************************
skipping: [172.16.1.31]
skipping: [172.16.1.41]
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
实践2
[root@waiwai /etc/ansible/yaml]# cat p3.yml
- hosts: oldboy
remote_user: root
tasks:
- name: Create New file
file: name=/tmp/oldboy1 state=touch
执行
[root@waiwai /etc/ansible/yaml]# ansible-playbook /etc/ansible/yaml/p3.yml
PLAY [oldboy] **************************************************************************
TASK [Gathering Facts] *****************************************************************
ok: [172.16.1.41]
ok: [172.16.1.31]
TASK [Create New file] *****************************************************************
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
实践3
定时任务编写
[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 [图片上传失败...(image-66d0dc-1556163511329)]
ntp3.aliyun.com >/dev/null 2>&1'
练习题:每周一1-5 上午8:30 /server/scripts/
[root@nfs01 ~]# crontab -l|tail -2
#Ansible: oldboy class
30 08 * * 1-5 /bin/sh /server/scripts/
class.sh>/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@waiwai /etc/ansible/yaml]# ansible-playbook /etc/ansible/yaml/p5.yml
PLAY [oldboy] **************************************************************************
TASK [Gathering Facts] *****************************************************************
ok: [172.16.1.41]
ok: [172.16.1.31]
TASK [Cron time sync] ******************************************************************
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
练习题2:
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@waiwai /etc/ansible/yaml]# cat p6.yml
- hosts: oldboy
tasks:
- name: copy rsync.conf
copy: src=/etc/rsyncd.conf dest=/etc/rsyncd.conf backup=yes
- name: create rsync.password
copy: content='rsync_backup:oldboy' dest=/etc/rsync.password backup=yes mode=0600
执行
[root@waiwai /etc/ansible/yaml]# ansible-playbook /etc/ansible/yaml/p6.yml
PLAY [oldboy] **************************************************************************
TASK [Gathering Facts] *****************************************************************
ok: [172.16.1.31]
ok: [172.16.1.41]
TASK [copy] ****************************************************************************
changed: [172.16.1.31]
changed: [172.16.1.41]
TASK [copy] ****************************************************************************
changed: [172.16.1.41]
changed: [172.16.1.31]
PLAY RECAP *****************************************************************************
172.16.1.31 : ok=3 changed=2 unreachable=0 failed=0
172.16.1.41 : ok=3 changed=2 unreachable=0 failed=0