免费学习自动化运维利器AnsiblePlaybook的任务控制(上)
Ansible 任务控制基本介绍
这主要来介绍PlayBook中的任务控制。
任务控制类似于编程语⾔中的if ... 、for ... 等逻辑控制语句。
这⾥我们给出个实际场景应⽤案例去说明在PlayBook中,任务 控制如何应⽤。
在下的PlayBook中,我们创建了 tomcat、www 和 mysql 三个用户。安装了Nginx 软件包、并同时更新了 Nginx 主配置⽂件和虚拟 主机配置⽂件,最后让Nginx 服务处于启动状态。
整个PlayBook从语法上没有任何问题,但从逻辑和写法上仍然有 ⼀些地⽅需要我们去注意及优化:
1、Nginx启动逻辑⽋缺考虑。若Nginx的配置件语法错误则会导 致启动Nginx失败,以⾄于PlayBook执⾏失败。
2、批量创建用户,通过指令的罗列过于死板。如果再创建若个用户,将难以收场。
---
- name: task control playbook example
hosts: webservers
tasks:
- name: create tomcat user
user: name=tomcat state=present
- name: create www user
user: name=www state=present
- name: create mysql user
user: name=mysql state=present
- name: yum nginx webserver
yum: name=nginx state=present
- name: update nginx main config
copy: src=nginx.conf dest=/etc/nginx/
- name: add virtualhost config
copy: src=www.qfedu.com.conf
dest=/etc/nginx/conf.d/
- name: start nginx server
service: name=nginx state=started
# cat nginx.conf
user www;
worker_processes 2;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user
[$time_local] "$request" '
'$status $body_bytes_sent
"$http_referer" '
'"$http_user_agent"
"$http_x_forwarded_for"';
sendfile on;
tcp_nopush on;
keepalive_timeout 0;
gzip on;
gzip_min_length 1k;
gzip_buffers 8 64k;
gzip_http_version 1.0;
gzip_comp_level 5;
gzip_types text/plain application/x-javascript
text/css application/json application/xml
application/x-shockwave-flash application/javascript
image/svg+xml image/x-icon;
gzip_vary on;
include /etc/nginx/conf.d/*.conf;
}
# cat www.qfedu.com.conf
server {
listen 80;
server_name www.qfedu.com;
root /usr/share/nginx/html;
access_log /var/log/nginx/www.qfedu.comaccess_log main;
error_log /var/log/nginx/www.qfedu.comerror_log;
add_header Access-Control-Allow-Origin *;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 1d;
}
location ~ .*\.(js|css)?$ {
expires 1d;
}
}
我们下⾯将以解决⼀个个问题的形式去优化上例中的PlayBook。
通过问题的解决,来达到我们学习任务控制的⽬的。
条件判断
解决第一个问题 Nginx启动逻辑缺考虑。若Nginx的配置⽂件语法错误则会导致 启动Nginx失败,以于PlayBook执行失败。
如果我们能够在启动之前去对Nginx的配置⽂件语法做正确性的校 验,只有当校验通过的时候我们才去启动或者重启Nginx;否则则跳过 启动Nginx的过程。这样就会避免Nginx 配置件语法问题⽽导致的⽆ 法启动Nginx的⻛险。
Nginx 语法校验
- name: check nginx syntax
shell: /usr/sbin/nginx -t
那如何将Nginx语法检查的TASK同Nginx启动的TASK关联起来呢?
如果我们能够获得语法检查的TASK的结果,根据这个结果去判断 “启动NGINX的TASK”是否执,这将是个很好的⽅案。如何和获取 到语法检查TASK的结果呢? 此时就可以使之前学到的 Ansible中的注 册变量。
获取Task任务结果
- name: check nginx syntax
shell: /usr/sbin/nginx -t
register: nginxsyntax
此时有可能还有疑问,我获取到任务结果,但是结果⾥⾯的内容是 个什么样⼦, 我如何根据内容在后续的PlayBook中使⽤呢?
通过debug模块去确认返回结果的数据结构
- name: print nginx syntax result
debug: var=nginxsyntax
通过debug 模块,打印出来的返回结果。当nginxsyntax.rc 为 0 时语法校验正确。
"nginxsyntax": {
"changed": true,
"cmd": "/usr/sbin/nginx -t",
"delta": "0:00:00.012045",
"end": "2017-08-12 20:19:04.650718",
"rc": 0,
"start": "2017-08-12 20:19:04.638673",
"stderr": "nginx: the configuration file
/etc/nginx/nginx.conf syntax is ok\nnginx:
configuration file /etc/nginx/nginx.conf test is
successful",
"stderr_lines": [
"nginx: the configuration file
/etc/nginx/nginx.conf syntax is ok",
"nginx: configuration file
/etc/nginx/nginx.conf test is successful"
],
"stdout": "",
"stdout_lines": []
}
通过条件判断(when) 指令去使语法校验的结果
- name: check nginx syntax
shell: /usr/sbin/nginx -t
register: nginxsyntax
- name: print nginx syntax
debug: var=nginxsyntax
- name: start nginx server
service: name=nginx state=started
when: nginxsyntax.rc == 0
改进后的PlayBook
---
- name: task control playbook example
hosts: webservers
gather_facts: no
tasks:
- name: create tomcat user
user: name=tomcat state=present
- name: create www user
user: name=www state=present
- name: create mysql user
user: name=mysql state=present
- name: yum nginx webserver
yum: name=nginx state=present
- name: update nginx main config
copy: src=nginx.conf dest=/etc/nginx/
- name: add virtualhost config
copy: src=www.qfedu.com.conf
dest=/etc/nginx/conf.d/
- name: check nginx syntax
shell: /usr/sbin/nginx -t
register: nginxsyntax
- name: print nginx syntax
debug: var=nginxsyntax
- name: start nginx server
service: name=nginx state=started
when: nginxsyntax.rc == 0
免费学习自动化运维利器AnsiblePlaybook的任务控制(上)
以上的逻辑,只要语法检查通过都会去执⾏ "start nginx server"这个TASK。在这个问题的解决,我们学习了when 条件判断 和注册变量的结合使⽤。学习了when条件判断中是可以持复杂逻辑 的。如现在到的逻辑运算符 and。
另外 when 支持如下运算符:
==
!=
> >=
< <=
is defined
is not defined
true
false
支持逻辑运算符: and or
免费学习自动化运维利器AnsiblePlaybook的任务控制(上),更多免费资料领取