微信搜索superit|邀请体验:大数据, 数据管理、OLAP分析与可视化平台 | 赞助作者:赞助作者

ansible playbook的template实现

ansible aide_941 7℃

模板templates

文本文件,嵌套有脚本(使用模板编程语言编写)
Jinja2语言,使用字面量,有下面形式

  • 字符串:使用单引号或双引号
  • 数字:整数,浮点数
  • 列表: [item1, item2, …]
  • 元组: (item1, item2, …)
  • 字典: {key1:value1, key2:value2, …}
  • 布尔型: true/false

算术运算: +, -, *, /, //, %, **
比较操作: ==, !=, >, >=, <, <=
逻辑运算: and, or, not
流表达式: For If When
emplates功能:根据模块文件动态生成对应的配置文件

  • templates文件必须存放于templates目录下,且命名为 .j2 结尾
  •  yaml/yml 文件需和templates目录平级,目录结构如下:

./
├── temnginx.yml
└── templates
└── nginx.conf.j2

 

Templates示例
示例:利用templates 同步nginx配置文件
准备templates/nginx.conf.j2文件
vim temnginx.yml
– hosts: websrvs
remote_user: root
tasks:
– name: template config to remote hosts
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
ansible-playbook temnginx.yml
 

Playbook中template变更替换
修改文件nginx.conf.j2 下面行为
worker_processes {{ ansible_processor_vcpus }};

cat temnginx2.yml
– hosts: websrvs
remote_user: root
tasks:
– name: template config to remote hosts
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
ansible-playbook temnginx2.yml
 

when

条件测试:如果需要根据变量、 facts或此前任务的执行结果来做为某task执行与否的前提时要用到条件测试,通过when语句实现,在task中使用, jinja2的语法格式
when语句
在task后添加when子句即可使用条件测试; when语句支持Jinja2表达式语法
示例:
tasks:
– name: “shutdown RedHat flavored systems”
command: /sbin/shutdown -h now
when: ansible_os_family == “RedHat”
示例:分别向两台目标主机复制httpd配置文件(一台Centos6,另一台Centos7),在这里使用when进行条件判断,如果为Centos6则复制http_6.conf.j2,如果为Centos7,则复制http_7.conf.j2

  1. – hosts: appsrvs
  2. remote_user: root
  3. tasks:
  4. – name: install package
  5. yum: name=httpd
  6. name: template 6
  7. template: src=httpd_6.conf.j2 dest=/etc/httpd/conf/httpd.conf
  8. notify: restart service
  9. when: ansible_distribution_major_version == “6”
  10. name: template 7
  11. template: src=httpd_7.conf.j2 dest=/etc/httpd/conf/httpd.conf
  12. notify: restart service
  13. when: ansible_distribution_major_version == “7”
  14. name: start service
  15. service: name=httpd state=started
  16. handlers:
  17. name: restart service
  18. service: name=httpd state=restarted

 

 

迭代: with_items

迭代:当有需要重复性执行的任务时,可以使用迭代机制
对迭代项的引用,固定变量名为” item“
要在task中使用with_items给定要迭代的元素列表
列表格式:
字符串
字典

示例:在目标主机上分别复制空文件f1,f2,f3

  1. – hosts: appsrvs
  2. remote_user: root
  3. tasks:
  4. – name: config file
  5. copy: src=/data/{{item}} dest=/data/
  6. with_items:
  7. – file1
  8. – file2
  9. – file3


示例二:对目标主机分别创建三个用户

  1. – hosts: appsrvs
  2. remote_user: root
  3. tasks:
  4. – name: create user
  5. user: name={{item}}
  6. with_items:
  7. – xixi
  8. – haha
  9. – memeda

迭代嵌套子变量

示例:在目标主分别创建三个组group1、group2、group3和三个用户user1、user2、user3,并且使用户和组一一对应

  1. – hosts: websrvs
  2. remote_user: root
  3. tasks:
  4. – name: add some groups
  5. group: name={{ item }} state=present
  6. with_items:
  7. – group1
  8. – group2
  9. – group3
  10. – name: add some users
  11. user: name={{ item.name }} group={{ item.group }} state=present
  12. with_items:
  13. – { name: ‘user1’, group: ‘group1’ }
  14. – { name: ‘user2’, group: ‘group2’ }
  15. – { name: ‘user3’, group: ‘group3’ }

Playbook中template for if

for
示例:在目标主机动态生成for.conf文件

  1. #vim for.yml
  2. – hosts: appsrvs
  3. remote_user: root
  4. vars:
  5. ports:
  6. 81
  7. 82
  8. ▽ – 83
  9. tasks:
  10. – name: test for
  11. template: src=for.conf.j2 dest=/data/for.conf
  12. #vim for.conf.j2
  13. {% for p in ports %}
  14. server {
  15. listen {{p}}
  16. }
  17. {%endfor%}


示例二:在目标主机上动态生成for1.conf文件

  1. vim for1.yml
  2. – hosts: appsrvs
  3. remote_user: root
  4. vars:
  5. ports:
  6. – web1:
  7. listen: 81
  8. name: www.magedu.com
  9. root: /data/web1
  10. – web2:
  11. listen: 82
  12. name: mobi.magedu.com
  13. root: /data/web2
  14. – web3:
  15. listen: 83
  16. name: app.magedu.com
  17. root: /data/web3
  18. tasks:
  19. – name: test for
  20. template: src=for1.conf.j2 dest=/data/for1.conf
  21. #vim for1.conf.j2
  22. {% for p in ports %}
  23. server {
  24. listen {{p.listen}}
  25. servername {{p.name}}
  26. rootdir {{p.root}}
  27. }
  28. {%endfor%}

if

{% if vhost.server_name is defined %}
server_name {{ vhost.server_name }};
{% endif %}

转载请注明:SuperIT » ansible playbook的template实现

喜欢 (0)or分享 (0)