自动化运维专题(二):Ansible批量自动化管理工具进阶
七,ansible的script模块批量运行脚本
ansible的script模块能够实现远程服务器批量运行本地的shell脚本。
#操作示例-->远程批量分发并自动部署nginx#所有被管理端需要挂载光盘,并创建本地yum配置文件[root@ansible scripts]# pwd/service/scripts[root@ansible scripts]# ls | xargs -n1auto_nginx.sh #自动安装nginx脚本fenfa.sh #批量分发脚本nginx-1.10.2.tar.gz #nginx源码包```[root@ansible scripts]# cat auto_nginx.sh #nginx安装脚本#!/bin/sh#nginx install shell scriptstest -d /media/cdrom || mkdir -p /media/cdrommount /dev/sr0 /media/cdrom &>/dev/nullyum -y install gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel &>/dev/nulltest -d /service/scripts || exit 3cd /service/scripts/tar xf nginx-1.10.2.tar.gz -C /usr/src/cd /usr/src/nginx-1.10.2/./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module &>/dev/nullmake &>/dev/nullmake install &>/dev/nullexit 0```[root@ansible scripts]# cat fenfa.sh #源码包和安装脚本的批量分发脚本#!/bin/sh#批量分发脚本Group=$1ansible $Group -m copy -a "src=/service/scripts/ dest=/service/scripts/"ansible $Group -m script -a "/service/scripts/auto_nginx.sh"[root@ansible scripts]# sh fenfa.sh all #激活脚本
此脚本只是个演示示例,工作中需要写的尽量严谨一些。
八,ansible-playbook的初步使用
playbook的使用,playbook可以把ansible的模块进行组合
8.1 playbook的简单shell模块的使用
[root@ansible scripts]# cat test_shell.yaml #playbook的执行模板--- #开头三个小-开头- hosts: webBtasks:- name: testshell: echo "welcome to yunjisaun" >> /tmp/username- name: test2shell: echo "welcome to yunjisuan" >> /tmp/username模板说明:--- #开头必须有三个小-,顶格写- hosts: #正文配置代码的第一级,必须有两个空格(-占一个空格位)- host: webB #webB是host参数的值,值和hosts:之间要有一个空格tasks: #tasks:表示接下来要执行的具体任务- name: #相对于tasks再多缩进两个格(-占一个空格位),表示属于tasks的下一级- name: test #test只是要执行的具体命令的名字可以随便写。name:后还是有一个空格要注意shell: #表示调用shell模块执行命令相对于tasks仍旧要多缩进两个空格shell: echo "xxx" >> xxx #shell:后边还是要有个空格,需要注意。
执行playbook配置文件
[root@ansible scripts]# ansible-playbook test_shell.yaml #执行playbook配置文件PLAY [webB] ********************************************************************************************************TASK [Gathering Facts] *********************************************************************************************ok: [webB]TASK [test] ********************************************************************************************************changed: [webB]TASK [test2] *******************************************************************************************************changed: [webB]PLAY RECAP *********************************************************************************************************webB : ok=3 changed=2 unreachable=0 failed=0
8.2 playbook的简单copy模块的使用
[root@ansible scripts]# echo "welcom to yunjisuan" >> /tmp/test_copy[root@ansible scripts]# cat test_copy.yaml---- hosts: alltasks:- name: test copycopy: src=/tmp/copy_test dest=/tmp/[root@ansible scripts]# ansible-playbook /service/scripts/test_copy.yamlPLAY [all] *********************************************************************************************************TASK [Gathering Facts] *********************************************************************************************ok: [webA]ok: [webB]TASK [test copy] ***************************************************************************************************changed: [webA]changed: [webB]PLAY RECAP *********************************************************************************************************webA : ok=2 changed=1 unreachable=0 failed=0webB : ok=2 changed=1 unreachable=0 failed=0
8.3 playbook使用register输出命令运行结果
我们在用playbook进行ansible模块操作的时候,并没有命令的执行结果输出,默认被隐藏了。
我们可以通过register模块最加输出命令的执行结果
[root@ansible scripts]# cat test_register.yaml---- hosts: alltasks:- name: test registershell: echo "welcome to yunjisuan"register: print_result #将之前命令的输出结果保存在变量print_result里- debug: var=print_result #将变量的值作为debug输出出来。[root@ansible scripts]# ansible-playbook test_register.yamlPLAY [all] *********************************************************************************************************TASK [Gathering Facts] *********************************************************************************************ok: [webA]ok: [webB]TASK [test register] ***********************************************************************************************changed: [webA]changed: [webB]TASK [debug] *******************************************************************************************************ok: [webA] => { #命令的执行结果有输出了"print_result": {"changed": true,"cmd": "echo \"welcome to yunjisuan\"","delta": "0:00:00.002269","end": "2018-06-15 10:28:14.693883","failed": false,"rc": 0,"start": "2018-06-15 10:28:14.691614","stderr": "","stderr_lines": [],"stdout": "welcome to yunjisuan","stdout_lines": ["welcome to yunjisuan"]}}ok: [webB] => {"print_result": {"changed": true,"cmd": "echo \"welcome to yunjisuan\"","delta": "0:00:00.002633","end": "2018-06-15 10:28:14.710242","failed": false,"rc": 0,"start": "2018-06-15 10:28:14.707609","stderr": "","stderr_lines": [],"stdout": "welcome to yunjisuan","stdout_lines": ["welcome to yunjisuan"]}}PLAY RECAP *********************************************************************************************************webA : ok=3 changed=1 unreachable=0 failed=0webB : ok=3 changed=1 unreachable=0 failed=0
8.4 nginx配置下发并检测
[root@ansible scripts]# cat test_nginx_conf.yaml---- hosts: alltasks:- name: copy nginx.confcopy: src=/tmp/nginx.conf dest=/usr/local/nginx/conf/ backup=yes- name:shell: /usr/local/nginx/sbin/nginx -tregister: nginx_result- debug: var=nginx_result
九,playbook的自定义变量和内置变量
9.1 在Playbook中使用自定义变量
[root@localhost scripts]# cat test_vars.yaml---- hosts: allvars: #定义变量- name: "yunjisuan" #第一个name变量age: "3" #第二个age变量tasks:- name: "{{ name }}" #{{}}两对大括号引用变量,变量名两头空格shell: echo "myname {{ name }},myage {{ age }}"register: var_result- debug: var=var_result特别提示:引用变量需要在双引号中引用。[root@localhost scripts]# ansible-playbook /service/scripts/test_vars.yaml[WARNING]: Found variable using reserved name: name #这里提示,name是一个保留的内置变量,我们在自定义时不能用PLAY [all] *********************************************************************************************************TASK [Gathering Facts] *********************************************************************************************ok: [webA]ok: [webB]TASK [yunjisuan] ***************************************************************************************************changed: [webA]changed: [webB]TASK [debug] *******************************************************************************************************ok: [webA] => {"var_result": {"changed": true,"cmd": "echo \"myname yunjisuan,myage 3\"","delta": "0:00:00.002320","end": "2018-06-19 10:45:16.175728","failed": false,"rc": 0,"start": "2018-06-19 10:45:16.173408","stderr": "","stderr_lines": [],"stdout": "myname yunjisuan,myage 3","stdout_lines": ["myname yunjisuan,myage 3"]}}ok: [webB] => {"var_result": {"changed": true,"cmd": "echo \"myname yunjisuan,myage 3\"","delta": "0:00:00.002518","end": "2018-06-19 10:45:10.552331","failed": false,"rc": 0,"start": "2018-06-19 10:45:10.549813","stderr": "","stderr_lines": [],"stdout": "myname yunjisuan,myage 3","stdout_lines": ["myname yunjisuan,myage 3"]}}PLAY RECAP *********************************************************************************************************webA : ok=3 changed=1 unreachable=0 failed=0webB : ok=3 changed=1 unreachable=0 failed=0#我们修改一下name这个变量再发送,就不会出警告了[root@localhost scripts]# cat test_vars.yaml---- hosts: allvars:- names: "yunjisuan"age: "3"tasks:- name: "{{ names }}"shell: echo "myname {{ names }},myage {{ age }}"register: var_result- debug: var=var_result
在使用自定义变量时,我们要特别注意不要和系统的内置保留变量同名,容易引发问题。
9.2 在playbook中使用ansible内置变量
我们可以使用ansible all -m setup | less查看ansible内置变量
[root@localhost scripts]# cat test_setupvars.yaml---- hosts: allgather_facts: True #使用ansible内置变量tasks:- name: setup varshell: echo "ip {{ ansible_all_ipv4_addresses[0] }} cpu {{ ansible_processor_count }}"register: var_result- debug: var=var_result[root@localhost scripts]#[root@localhost scripts]# ansible-playbook test_setupvars.yamlPLAY [all] *********************************************************************************************************TASK [Gathering Facts] *********************************************************************************************ok: [webA]ok: [webB]TASK [setup var] ***************************************************************************************************changed: [webA]changed: [webB]TASK [debug] *******************************************************************************************************ok: [webA] => {"var_result": {"changed": true,"cmd": "echo \"ip 192.168.200.132 cpu 1\"","delta": "0:00:00.002408","end": "2018-06-19 11:32:44.540658","failed": false,"rc": 0,"start": "2018-06-19 11:32:44.538250","stderr": "","stderr_lines": [],"stdout": "ip 192.168.200.132 cpu 1","stdout_lines": ["ip 192.168.200.132 cpu 1"]}}ok: [webB] => {"var_result": {"changed": true,"cmd": "echo \"ip 192.168.200.138 cpu 1\"","delta": "0:00:00.002102","end": "2018-06-19 11:32:44.526875","failed": false,"rc": 0,"start": "2018-06-19 11:32:44.524773","stderr": "","stderr_lines": [],"stdout": "ip 192.168.200.138 cpu 1","stdout_lines": ["ip 192.168.200.138 cpu 1"]}}PLAY RECAP *********************************************************************************************************webA : ok=3 changed=1 unreachable=0 failed=0webB : ok=3 changed=1 unreachable=0 failed=0
简单演示一下ansible内置变量的取用方法ansible all -m setup | less
[root@localhost scripts]# cat test_setupvars.yaml---- hosts: allgather_facts: Truetasks:- name: setup varshell: echo "ip {{ ansible_all_ipv4_addresses[0] }} cpu {{ ansible_processor_count }}" >> /tmp/test- name: setup var2shell: echo "time {{ ansible_date_time["date"] }}" >> /tmp/testregister: var_result- debug: var=var_result
十,Playbook下发可变配置文件
配置文件如果使用copy模块去下发的话,那配置都是一样的;
如果下发的配置文件里有可变的配置,需要用到template模块。
10.1 利用template模块下发可变的配置文件
[root@localhost scripts]# cat /tmp/testmy name is {{ myname }} #自定义变量my name is {{ ansible_all_ipv4_addresses[0] }} #系统变量[root@localhost scripts]# cat test_filevars.yaml---- hosts: allgather_facts: True #开启系统变量vars:- myname: "yunjisuan" #自定义变量tasks:- name: template testtemplate: src=/tmp/test dest=/root/test #使用template下发可变配置文件[root@localhost scripts]# ansible-playbook test_filevars.yaml
10.2 下发配置文件里面使用判断语法
[root@localhost scripts]# cat /tmp/if.j2{% if PORT %} #if PORT存在ip=0.0.0.0:{{ PORT }}{% else %} #否则的话ip=0.0.0.0:80{% endif %} #结尾[root@localhost scripts]# cat test_ifvars.yaml---- hosts: allgather_facts: True #开启系统内置变量vars:- PORT: 90 #自定义变量tasks:- name: jinja2 if testtemplate: src=/tmp/if.j2 dest=/root/test[root@localhost scripts]# ansible-playbook test_ifvars.yaml
如果我们将变量PORT值为空的话,就会是另外的结果
[root@localhost scripts]# cat test_ifvars.yaml---- hosts: allgather_facts: Truevars:- PORT: #置空tasks:- name: jinja2 if testtemplate: src=/tmp/if.j2 dest=/root/test[root@localhost scripts]# ansible-playbook test_ifvars.yaml
十一,Playbook的notify通知和下发nginx配置
#实战下发可执行动作的可变的nginx配置文件[root@localhost scripts]# head -1 /tmp/nginx.j2worker_processes {{ ansible_processor_count }}; #可变的参数[root@localhost scripts]# cat test_nginxvars.yaml---- hosts: allgather_facts: True #开启系统内置变量tasks:- name: nginx conftemplate: src=/tmp/nginx.j2 dest=/usr/local/nginx/conf/nginx.confnotify:- reload nginx #下发通知给handlers模块执行名字叫做reload nginx的动作handlers: #定义动作- name: reload nginx #动作的名字shell: /usr/local/nginx/sbin/nginx -s reload[root@localhost scripts]# ansible-playbook test_nginxvars.yaml
十二,使用roles标准化Playbook
roles功能可以用来规范playbook的编写
12.1 创建所需要的roles原型目录结构
#创建roles基本原型的目录结构[root@ansible myroles]# tree /myroles//myroles/├── nginx.yaml #入口触发配置文件└── roles #playbook的原型配置目录└── nginx #nginx相关模组配置目录├── files #copy模块和script模块的参数src默认会从这个文件夹查找├── handlers #用来存放notify的├── tasks #用来存放ansible模块任务的├── templates #用来存放j2的└── vars #用来存放变量的7 directories, 1 file#入口触发配置文件[root@ansible myroles]# cat /myroles/nginx.yaml---- hosts: all #执行的主机范围gather_facts: True #开启系统内置变量roles: #启用roles原型配置- nginx #执行nginx原型模组
12.2 roles中tasks任务编排模组的使用
#在nginx模组添加tasks任务配置文件[root@ansible myroles]# cat roles/nginx/tasks/main.yaml---- name: check alived #任务1的名字ping: #执行ping模块- name: #任务2的名字shell: ls / #执行shell模块register: ls_result #将执行结果保存给变量- debug: var=ls_result #变量的值赋值给debug进行输出#完成后的目录结构如下所示[root@ansible myroles]# tree /myroles//myroles/├── nginx.yaml #nginx模组入口配置文件└── roles└── nginx #nginx原型模组目录├── files├── handlers├── tasks│ └── main.yaml #nginx模组的tasks任务配置文件├── templates└── vars7 directories, 2 files
12.3 执行简单的roles任务模型
#执行nginx入口配置文件[root@ansible myroles]# ansible-playbook nginx.yamlPLAY [all] ****************************************************************************************************TASK [Gathering Facts] ****************************************************************************************ok: [webA]ok: [webB]TASK [nginx : check alived] ***********************************************************************************ok: [webA]ok: [webB]TASK [nginx : shell] ******************************************************************************************changed: [webA]changed: [webB]TASK [nginx : debug] ******************************************************************************************ok: [webA] => {"ls_result": {"changed": true,"cmd": "ls /","delta": "0:00:00.002805","end": "2018-06-21 11:52:29.343592","failed": false,"rc": 0,"start": "2018-06-21 11:52:29.340787","stderr": "","stderr_lines": [],"stdout": "bin\nboot\ndev\netc\nhome\nlib\nlib64\nmedia\nmnt\nopt\nproc\nroo\nroot\nrun\nsbin\nservice\nsrv\nsys\ntmp\nusr\nvar","stdout_lines": ["bin","boot","dev","etc","home","lib","lib64","media","mnt","opt","proc","roo","root","run","sbin","service","srv","sys","tmp","usr","var"]}}ok: [webB] => {"ls_result": {"changed": true,"cmd": "ls /","delta": "0:00:00.002708","end": "2018-06-21 11:52:29.359754","failed": false,"rc": 0,"start": "2018-06-21 11:52:29.357046","stderr": "","stderr_lines": [],"stdout": "bin\nboot\ndev\netc\nhome\nlib\nlib64\nmedia\nmnt\nopt\nproc\nroo\nroot\nrun\nsbin\nservice\nsrv\nsys\ntmp\nusr\nvar","stdout_lines": ["bin","boot","dev","etc","home","lib","lib64","media","mnt","opt","proc","roo","root","run","sbin","service","srv","sys","tmp","usr","var"]}}PLAY RECAP ****************************************************************************************************webA : ok=4 changed=1 unreachable=0 failed=0webB : ok=4 changed=1 unreachable=0 failed=0
- ansible-playbook执行入口配置文件nginx.yaml后,它会自动在roles目录下查找nginx目录并进入后查找tasks任务目录并执行main.yaml的任务配置文件。
- 其实,这个roles的操作等效于以下配置
#本配置和之前的roles配置等效[root@ansible myroles]# cat /service/scripts/test.yaml---- hosts: allgather_facts: Truetasks: #其实roles的本质就是将tasks任务单独写了。- name: check alived #并在入口文件里追加了roles去查找tasks配置文件路径ping:- name:shell: ls /register: ls_result- debug: var=ls_result
12.4 roles中vars自定义变量模组的使用
#创建自定义变量vars模组的配置文件[root@ansible myroles]# cat roles/nginx/vars/main.yaml---my_name: yunjisuanphone: 1800000000[root@ansible myroles]# cat roles/nginx/tasks/main.yaml---- name: check alivedping:- name:shell: ls /register: ls_result- debug: var=ls_result- name: #添加对变量引用的任务编排shell: echo my phone is {{ phone }}register: echo_result- debug: var=echo_result[root@ansible myroles]# ansible-playbook nginx.yaml #执行入口配置文件
12.5 使用copy,script模块的标准化
roles模型里使用copy,script模块,默认从roles/nginx/files这里面找
[root@ansible myroles]# cat roles/nginx/files/testwelcome to yunjisuan[root@ansible myroles]# cat roles/nginx/files/test.shecho "aaa" >> /tmp/test[root@ansible myroles]# chmod +x roles/nginx/files/test.sh[root@ansible myroles]# cat roles/nginx/tasks/main.yaml---- name: check alivedping:- name:shell: ls /register: ls_result- debug: var=ls_result- name:shell: echo my phone is {{ phone }}register: echo_result- debug: var=echo_result- name: #添加copy模块copy: src=test dest=/root/- name: #添加script模块(自动在目标IP机器上执行脚本)script: test.sh[root@ansible myroles]# ansible-playbook nginx.yaml
12.6 roles中template模块的使用
roles模型里使用template模块,默认从roles/nginx/template里面找
[root@ansible myroles]# cat roles/nginx/templates/test.j2myname is {{ my_name }},my phone is {{ phone }} #引用自定义变量my ipaddress is {{ansible_all_ipv4_addresses[0]}} #引用内置变量[root@ansible myroles]# cat roles/nginx/tasks/main.yaml---- name: check alivedping:- name:shell: ls /register: ls_result- debug: var=ls_result- name:shell: echo my phone is {{ phone }}register: echo_result- debug: var=echo_result- name:copy: src=test dest=/root/- name:script: test.sh- name:template: src=test.j2 dest=/root/test2 #下发可变配置文件[root@ansible myroles]# ansible-playbook nginx.yaml
12.7 roles中notify模块的使用
roles使用notify模块,默认从roles/nginx/handles里面找
[root@ansible myroles]# cat roles/nginx/handlers/main.yaml---- name: start_nginx #定义handlers的动作类型shell: /usr/local/nginx/sbin/nginx- name: stop_nginx #定义handlers的动作类型shell: /usr/local/nginx/sbin/nginx -s stop- name: reload_nginx #定义handlers的动作类型shell: /usr/local/nginx/sbin/nginx -s reload[root@ansible myroles]# cat roles/nginx/tasks/main.yaml---- name: check alivedping:- name:shell: ls /register: ls_result- debug: var=ls_result- name:shell: echo my phone is {{ phone }}register: echo_result- debug: var=echo_result- name:copy: src=test dest=/root/- name:script: test.sh- name:template: src=test.j2 dest=/root/test2notify: start_nginx #执行template任务后下发通知给handlers执行start_nginx[root@ansible myroles]# ansible-playbook nginx.yaml
特别提示:
notify下发通知只有当之前的任务造成了变化那么才会被执行,如果没有发生任何改变,则notify不会被执行。例如:
#tasks任务造成改变,触发notify[root@ansible myroles]# cat /tmp/test.yaml---- hosts: webAgather_facts: Truetasks:- name:copy: src=/tmp/test dest=/root/ #这步造成目标改变才能出发notifynotify: start_nginxhandlers:- name: start_nginxshell: /usr/local/nginx/sbin/nginx[root@ansible myroles]# ansible-playbook /tmp/test.yamlPLAY [webA] ***************************************************************************************************TASK [Gathering Facts] ****************************************************************************************ok: [webA]TASK [copy] ***************************************************************************************************changed: [webA] #发生了改变RUNNING HANDLER [start_nginx] #触发notify *********************************************************************************changed: [webA]PLAY RECAP ****************************************************************************************************webA : ok=3 changed=2 unreachable=0 failed=0#我们再次执行/tmp/test.yaml[root@ansible myroles]# ansible-playbook /tmp/test.yamlPLAY [webA] ***************************************************************************************************TASK [Gathering Facts] ****************************************************************************************ok: [webA]TASK [copy] ***************************************************************************************************ok: [webA] #没有造成任务改变,未触发notify通知PLAY RECAP ****************************************************************************************************webA : ok=2 changed=0 unreachable=0 failed=0

浙公网安备 33010602011771号