第1章 ansible剧本编写介绍
1.1 ansible剧本编写格式说明
# 表示注释信息 ### 剧本的开头,可以不写 - hosts:空格all ### 处理所有服务器,找到所有服务器 tasks: - command: echo hello test linux.
1.2 ansible剧本编写注意事项
- 需要识别空格字符
- tab=1个空格 编写ansible-playbook,一定要忘记有tab
- - : 在剧本中使用时,后面要接一个空格
- 剧本编写有等级划分,用两个空格(缩进)表示不同等级
1.3 ansible剧本编写步骤
1.3.1 编写剧本
[root@m01 ansible-playbook]# vim cron.yml - hosts: all tasks: - cron: name=test03 minute=*/5 job='/bin/sh /server/scripts/test.sh &>/dev/null'
1.3.2 检查剧本语法
[root@m01 ansible-playbook]# ansible-playbook --syntax-check cron.yml playbook: cron.yml
1.3.3 剧本预演
[root@m01 ansible-playbook]# ansible-playbook -C cron.yml PLAY [all] *********************************************************************************** TASK [Gathering Facts] *********************************************************************** ok: [172.16.1.8] ok: [172.16.1.41] ok: [172.16.1.31] TASK [cron] ********************************************************************************** changed: [172.16.1.41] changed: [172.16.1.8] 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.8 : ok=2 changed=1 unreachable=0 failed=0
1.3.4 剧本执行
[root@m01 ansible-playbook]# ansible-playbook cron.yml PLAY [all] ********************************************************************************** TASK [Gathering Facts] ********************************************************************** ok: [172.16.1.8] ok: [172.16.1.41] ok: [172.16.1.31] TASK [cron] ********************************************************************************* 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.8 : ok=2 changed=1 unreachable=0 failed=0
1.4 ansible剧本编写多个任务
可以根据不同主机或主机组,完成不同的ansible任务。
[root@m01 ansible-playbook]# cat cron.yml - hosts: test tasks: - name: cron04:test04 00 03 cron: name=test04 minute=00 hour=03 job='/bin/sh /server/scripts/test.sh &>/dev/null' - name: cron-test05 00 04 cron: name=test05 minute=00 hour=04 job='/bin/sh /server/scripts/test.sh &>/dev/null' - hosts: web01 tasks: - name: stop cron server service: name=crond state=stopped enabled=no
第二章 附录:密钥分发脚本示例
#!/bin/bash ############################################## #FileName:ansible_distribution_key.sh # #Author:Leon # #CTime:2017.09.08 # #Function:Distribution of the key # ############################################## user="root" userPassword="123456" encryption="dsa" publicKey="id_${encryption}.pub" privateKey="id_${encryption}" #install sshpass if [ `rpm -qa sshpass | wc -l` -eq 0 ];then echo "Starting install sshpass..." yum install -y sshpass -q if [ $ != 0 ];then echo "Can not install sshpass" exit 1 fi echo -e "Install sshpass successful...\n" fi #create key pair ssh-keygen -t ${encryption} -f /${user}/.ssh/${privateKey} -P "" -q if [ $ != 0 ];then echo "Can not create dsa key pair" exit 1 fi #Distribution of the key echo "Starting distribute the key..." for ip in 31 41 do sshpass -p ${userPassword} ssh-copy-id -i /${user}/.ssh/${publicKey} "-o StrictHostKeyChecking=no ${user}@172.16.1.${ip}" &>/dev/null if [ $ != 0 ];then echo "Can not distribut key to 172.16.1.${ip}" exit 1 fi done echo -e "Distribute the key successful...\n"

我的微信
如果有技术上的问题可以扫一扫我的微信