linre 发表于 2014-11-20 11:16:20

ansible的playbook模板和facts的后续文档

上次的文章有些偏语法,那 ! 我们来一个简单的实战吧,用ansible的playbook配置nginx,是有点过于简单了。。。。。

Python
user root; worker_processes {{ ansible_processor_count }}; pid /var/run/nginx.pid; events { worker_connections {{ connections }} ; # multi_accept on; } http { sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; server_tokens off; include /etc/nginx/mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; gzip on; gzip_disable "msie6"; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_buffers 16 8k; gzip_http_version 1.1; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; map $scheme $server_https { default off; https on; } include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; }

   

user root;
worker_processes {{ ansible_processor_count }};
pid /var/run/nginx.pid;
events {
    worker_connections {{ connections }} ;
    # multi_accept on;
}
http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    server_tokens off;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    gzip on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    map $scheme $server_https {
      default off;
      https on;
    }
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}


配置nginx的ansible playbook模板

Python
- hosts: web user: root sudo: True vars: connections : "1024" tasks: - name: wget api get_url: url=http://10.10.10.67:8000/ceshi?ip={{ ansible_hostname }} dest=/etc/ansible/facts.d/vhost.fact mode=0777 - name: install nginx action: yum name=nginx state=latest - name: write nginx.conf action: template src=nginx.conf dest=/etc/nginx/nginx.conf notify: - restart nginx handlers: - name: restart nginx action: service name=nginx state=restarted

   

- hosts: web
user: root
sudo: True
vars:
   connections : "1024"
tasks:
   - name: wget api
       get_url: url=http://10.10.10.67:8000/ceshi?ip={{ ansible_hostname }} dest=/etc/ansible/facts.d/vhost.fact mode=0777
   - name: install nginx
       action: yum name=nginx state=latest
   - name: write nginx.conf
       action: template src=nginx.conf dest=/etc/nginx/nginx.conf
       notify:
       - restart nginx
handlers:
   - name: restart nginx
       action: service name=nginx state=restarted


好了,咱们看看结果吧:

有时候咱们可以在playbook直接区分系统,而不用在模板中。

Python
when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'

   

when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'


举一反三,这样类似的facts有很多的。


大家有注意到我连接了一个http接口,这个是做啥的? 我给接口get传递了我的hostname信息,接口通过数据库查询后,用jinja2模板返回了我一个nginx.conf配置。 这个方法是靠谱的,我已经用在了两个项目上了。


有兴趣的朋友可以看看tornado的模板渲染nginx配置,如果朋友们想做运维平台化,推荐使用这方法,我知道的几家 腾讯,金山,人人,百度等 都用了这些个方法。当然他们的处理逻辑更复杂,但主要思路都是采用模板来渲染的配置文件。有点啰嗦了。有兴趣的朋友可以一看,靠谱哦 !


看完了,就可以做成我这样的接口了。像我们这边,每个机组下的nginx的server{}块都在数据库里面是有体现的,这些又会和cmdb资产系统想关联。 所以在playbook的host不管有多少个,他都可以精确的调用接口渲染nginx的配置文件。

Python
get_url: url=http://10.10.10.67:8000/ceshi?ip={{ ansible_hostname }
   

get_url: url=http://10.10.10.67:8000/ceshi?ip={{ ansible_hostname }


对于大量的信息配置,还有一个方法是利用ansible的模块,返回json的facts信息,然后传递给模板文件。(这方法其实也行,和第一个接口的方式大同小异)

页: [1]
查看完整版本: ansible的playbook模板和facts的后续文档