趁着最近在搞ansible,现在学习了一波template模块的用法:

1、使用template模块在jinja2中引用变量,先来目录结构树

[root@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

打开定义好的变量:

[root@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文件查看节点信息:

[root@master ansible]# egrep -v "^#|^$" hosts
[nodes]
192.168.101.14
192.168.101.15

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

[root@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主任务定义:

[root@master ansible]# cat roles/temp/tasks/main.yaml
- name: copy configfile to nodes
template:
src: test.j2
dest: /tmp/test.conf

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

[root@master ansible]# cat work_dir/copy_configfile.yaml
- hosts: nodes
remote_user: root
roles:
- temp

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

执行:

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

然后在两个节点查看:

[root@master ~]# cat /tmp/test.conf
ExecStart=/usr/local/bin/etcd --name master --initial-advertise-peer-urls http://192.168.101.14:2380
[root@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 %}进行控制:

[root@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模块中的值,是节点的固定值

[root@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下面的文件:

[root@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,然后执行:

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

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

[root@master ~]# cat /tmp/test.conf
ExecStart=/usr/local/bin/etcd --name master --initial-advertise-peer-urls http://192.168.101.14:2380
[root@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的文件:

[root@master ansible]# cat roles/temp/templates/test_for.j2
{% for i in range(,) %}
test{{ i }}
{% endfor %}
[root@master ansible]# cat roles/temp/tasks/main.yaml
- name: copy configfile to nodes
template:
src: test_for.j2
dest: /tmp/test.conf

执行该角色:

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

验证两节点的文件内容:

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

4、使用default()默认值

当我们定义了变量的值时,采用变量的值,当我们没有定义变量的值时,那么使用默认给定的值:

首先查看定义的变量:

[root@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的文件:

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

可以看见并没有定义server_port这个变量

查看tasks文件:

[root@master ansible]# cat roles/temp/tasks/main.yaml
- name: copy configfile to nodes
template:
src: test_default.j2
dest: /tmp/test.conf

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

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

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

[root@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:

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

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

可以看见使用了定义的值

ansible之template模块的更多相关文章

  1. 【Ansible】 各种模块

    [Ansible 模块] 就如python库一样,ansible的模块也分成了基本模块和第三方拓展模块(自定义的模块).这些模块其实才是作为真实的逻辑载体,在帮助ansible进行作业. ansibl ...

  2. Ansible Playbooks 常用模块

    官网链接:https://docs.ansible.com/ansible/latest/modules/list_of_all_modules.html ansible python module ...

  3. Ansible playbooks常用模块案例操作

    打开git bash 连接ansible服务器,然后进入deploy用户 #ssh root@192.168.96.188 进入python3.6虚拟环境 #su - deploy #source . ...

  4. Ansible Playbooks常用模块

    File模块 在目标主机创建文件或目录,并赋予其系统权限 - name: create a file file:'path=/oot/foo.txt state=touch mode=0755 own ...

  5. 二、Ansible基础之模块篇

    目录 1. Ansible Ad-Hoc 命令 1.1 命令格式 1.2 模块类型 1.3 联机帮助 1.3.1 常用帮助参数 1.4 常用模块 1.4.1 command & shell 模 ...

  6. Ansible之roles模块--lnmp分布式部署

    Ansible之roles模块--lnmp分布式部署 目录 Ansible之roles模块--lnmp分布式部署 1. role模块的作用 2. roles的目录结构 3. roles内个目录含义解释 ...

  7. ansible命令执行模块使用

    ansible命令执行模块使用 1.命令执行模块-command 在远程节点上运行命令. 命令模块使用命令名称,接上空格-的分割符作为参数使用,但是不支持管道符和变量等,如果要使用这些,那么可以使用s ...

  8. ansible执行shell模块和command模块报错| FAILED | rc=127 >> /bin/sh: lsof: command not found和| rc=2 >> [Errno 2] No such file or directory

    命令: ansible -i hosts_20 st  -m shell -a 'service zabbix_agentd star'  -K --become ansible -i hosts_2 ...

  9. 运维自动化神器ansible之user模块

    运维自动化神器ansible之user模块 一.概述   user模块 可管理远程主机上的 用户,比如创建用户.修改用户.删除用户.为用户创建密钥对等操作. 二.参数介绍   name: 用于指定操作 ...

随机推荐

  1. 调试寄存器 原理与使用:DR0-DR7

    调试寄存器 原理与使用:DR0-DR7 下面介绍的知识性信息来自intel IA-32手册(可以在intel的开发手册或者官方网站查到),提示和补充来自学习调试器实现时的总结. 希望能给你带去有用的信 ...

  2. 【PHP】算法进阶,获取给定值的最优组合:虚拟币抵扣问题解决方案

    商城里边.虚拟币抵扣问题解决方案 虚拟币抵扣规则,按照以下规则执行: 1.如果一个订单包含多款商品,且均支持虚拟币抵扣时:   优先按照最大化使用虚拟币进行全额抵扣原则进行抵扣,若抵扣后用户虚拟币账号 ...

  3. 网络虚拟化技术(二): TUN/TAP MACVLAN MACVTAP

    TUN 设备 TUN 设备是一种虚拟网络设备,通过此设备,程序可以方便得模拟网络行为.先来看看物理设备是如何工作的:

  4. 【jQuery系列之插件】jquery插件之jquery-validation

    equalTo方法: equalTo: function( value, element, param ) { // Bind to the blur event of the target in o ...

  5. Sass-学习笔记【基础篇】

    最下边附结构图 在线编辑器网址如下:http://sassmeister.com/  注意编写的时候,符号千万别用了中文的:.:.....之类的,会报错,Sass也转换不成css. less和sass ...

  6. 经验之道:最有效的iOS内存泄漏检测

    版权声明:本文由胡涛原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/125 来源:腾云阁 https://www.qclou ...

  7. 关于font-size对垂直居中影响的问题

    背景:三个inline-block元素,其中两个内容为空,另外一个包含文字,设置文字的font-size之后,原本垂直居中的三个inline-block的元素,会变的不再垂直居中. 原因: 当设置了f ...

  8. Ubuntu adb devices :???????????? no permissions (verify udev rules) 解决方法

    Ubuntu adb devices :???????????? no permissions (verify udev rules) 解决方法http://www.cnblogs.com/cat-l ...

  9. EUI EXML内部类Skin和ItemRenderer

    没认真看过...现在试试... EXMl支持内部类 两种支持做为内部类的:Skin和ItemRenderer 优点: 这种最大的好处就是皮肤如果只用一次,不需要单独写成一个exml文件,只需要写在组件 ...

  10. Egret打包App 短暂黑屏解决方案 (Egret4.1.0)

    论坛已经有人解决:http://bbs.egret.com/forum.php?mod=viewthread&tid=30288&highlight=app%2B%E9%BB%91%E ...