设为首页 收藏本站
查看: 4300|回复: 0

[经验分享] ansible之template模块

[复制链接]

尚未签到

发表于 2018-1-2 16:37:06 | 显示全部楼层 |阅读模式
  趁着最近在搞ansible,现在学习了一波template模块的用法:
  1、使用template模块在jinja2中引用变量,先来目录结构树
  

[iyunv@master ansible]# tree  
.
  
├── ansible.cfg
  
├── hosts
  
├── roles
  
│   └── temp
  
│       ├── tasks
  
│       │   └── main.yaml
  
│       ├── templates
  
│       │   ├── test_if.j2
  
│       │   └── test.j2
  
│       └── vars
  
│           └── main.yaml
  
└── work_dir
  ├── copy_configfile.retry
  └── copy_configfile.yaml
  

  打开定义好的变量:
  

[iyunv@master ansible]# cat roles/temp/vars/main.yaml  
master_ip:
192.168.101.14  
master_hostname: master
  
node1_ip:
192.168.101.15  
node1_hostname: node1
  

  打开hosts文件查看节点信息:
  

[iyunv@master ansible]# egrep -v "^#|^$" hosts  
[nodes]
  

192.168.101.14  
192.168.101.15
  

  现在通过定义好的变量在templates目录下创建j2文件:
  

[iyunv@master ansible]# cat roles/temp/templates/test.j2  
ExecStart
=/usr/local/bin/etcd --name {{ master_hostname }} --initial-advertise-peer-urls http://{{ master_ip }}:2380  

  查看tasks主任务定义:
  

[iyunv@master ansible]# cat roles/temp/tasks/main.yaml  

- name: copy configfile to nodes  template:
  src: test.j2
  dest:
/tmp/test.conf  

  查看工作目录下面的执行yaml:
  

[iyunv@master ansible]# cat work_dir/copy_configfile.yaml  

- hosts: nodes  remote_user: root
  roles:
- temp  

  在tasks目录下面的main.yaml定义使用了template模块,调用templates目录下面的test.j2文件
  执行:
  

[iyunv@master ansible]# ansible-playbook work_dir/copy_configfile.yaml  

  然后在两个节点查看:
  

[iyunv@master ~]# cat /tmp/test.conf  
ExecStart
=/usr/local/bin/etcd --name master --initial-advertise-peer-urls http://192.168.101.14:2380  

  

[iyunv@node1 ~]# cat /tmp/test.conf  
ExecStart
=/usr/local/bin/etcd --name master --initial-advertise-peer-urls http://192.168.101.14:2380  

  可以看见在各个节点的tem目录下面的文件都用变量替换了
  2、使用template模块调用的j2文件使用{% if %} {% endif %}进行控制:
  

[iyunv@master ansible]# cat roles/temp/templates/test_if.j2  
{
% if ansible_hostname == master_hostname %}  
ExecStart
=/usr/local/bin/etcd --name {{ master_hostname }} --initial-advertise-peer-urls http://{{ master_ip }}:2380  
{% elif ansible_hostname == node1_hostname %}
  
ExecStart=/usr/local/bin/etcd --name {{ node1_hostname }} --initial-advertise-peer-urls http://{{ node1_ip }}:2380
  
{% endif %}
  

  在上面中使用if进行了判断,如果ansible_hostname变量与定义的master_hostname变量值相等,那么将此文件copy到节点上就使用条件1,而过不满足条件1那么执行条件2
  ansible_hostname这个变量是setup模块中的值,是节点的固定值
  

[iyunv@master ~]# ansible all -m setup -a "filter=ansible_hostname"  
192.168.101.15 | SUCCESS => {
  "ansible_facts": {
  "ansible_hostname": "node1"
  },
  "changed": false,
  "failed": false
  
}
  
192.168.101.14 | SUCCESS => {
  "ansible_facts": {
  "ansible_hostname": "master"
  },
  "changed": false,
  "failed": false
  
}
  

  现在查看tasks下面的文件:
  

[iyunv@master ansible]# cat roles/temp/tasks/main.yaml  

- name: copy configfile to nodes  template:
  src: test_if.j2
  dest:
/tmp/test.conf  

  将上面的test.j2改为了if条件的j2,然后执行:
  

[iyunv@master ansible]# ansible-playbook work_dir/copy_configfile.yaml  

  查看各节点生成的文件内容:
  

[iyunv@master ~]# cat /tmp/test.conf  
ExecStart
=/usr/local/bin/etcd --name master --initial-advertise-peer-urls http://192.168.101.14:2380  

  

[iyunv@node1 ~]# cat /tmp/test.conf  
ExecStart
=/usr/local/bin/etcd --name node1 --initial-advertise-peer-urls http://192.168.101.15:2380  

  可以看见生成的文件内容不一样,于是这样就可以将节点的不同内容进行分离开了
  当然还可以使用另外的方式隔离节点的不同:
  

ExecStart=/usr/local/bin/etcd --name {{ ansible_hostname }} --initial-advertise-peer-urls http://{{ ansible_ens33.ipv4.address }}:2380  

  因为各个节点的ansible_hostname和ip都是固定的所以也可以根据上面进行区分不同(不过这种方式限制了一定的范围)
  3、使用template模块调用j2文件使用for循环:
  创建jinja关于for的文件:
  

[iyunv@master ansible]# cat roles/temp/templates/test_for.j2  
{
% for i in range(1,10) %}  
test{{ i }}
  
{
% endfor %}  

  

[iyunv@master ansible]# cat roles/temp/tasks/main.yaml  

- name: copy configfile to nodes  template:
  src: test_for.j2
  dest:
/tmp/test.conf  

  执行该角色:
  

[iyunv@master ansible]# ansible-playbook work_dir/copy_configfile.yaml  

  验证两节点的文件内容:
  

[iyunv@master ~]# cat /tmp/test.conf  
test1
  
test2
  
test3
  
test4
  
test5
  
test6
  
test7
  
test8
  
test9
  

  

[iyunv@node1 ~]# cat /tmp/test.conf  
test1
  
test2
  
test3
  
test4
  
test5
  
test6
  
test7
  
test8
  
test9
  

  4、使用default()默认值
  当我们定义了变量的值时,采用变量的值,当我们没有定义变量的值时,那么使用默认给定的值:
  首先查看定义的变量:
  

[iyunv@master ansible]# cat roles/temp/vars/main.yaml  
master_ip:
192.168.101.14  
master_hostname: master
  
node1_ip:
192.168.101.15  
node1_hostname: node1
  

  然后查看jinja2的文件:
  

[iyunv@master ansible]# cat roles/temp/templates/test_default.j2  
Listen: {{ server_port
|default(80) }}  

  可以看见并没有定义server_port这个变量
  查看tasks文件:
  

[iyunv@master ansible]# cat roles/temp/tasks/main.yaml  

- name: copy configfile to nodes  template:
  src: test_default.j2
  dest:
/tmp/test.conf  

  执行完成后,查看文件内容:
  

[iyunv@master ~]# cat /tmp/test.conf  
Listen:
80  

  现在向vars/main.yaml中定义server_port变量,并给定值:
  

[iyunv@master ansible]# cat roles/temp/vars/main.yaml  
master_ip:
192.168.101.14  
master_hostname: master
  
node1_ip:
192.168.101.15  
node1_hostname: node1
  
server_port:
8080  

  再次执行,然后查看文件内容:
  

[iyunv@master ~]# cat /tmp/test.conf  
Listen:
8080  

  可以看见使用了定义的值

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-430866-1-1.html 上篇帖子: ansible playbook 示例 下篇帖子: Ⅰ. Ansible Inventory Hosts文件配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表