趁着最近在搞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. Java Integer常量池

    public class IntegerExample { public static void main(String[] javalatte) { Integer i = 10; Integer ...

  2. OracleClient安装系统环境变量配置

    1.变量名:TNS_ADMIN  值:E:\instantclient_11_2\NETWORK\ADMIN 2.变量名:NLS_LANG    值:SIMPLIFIED CHINESE_CHINA. ...

  3. 浅谈MVC和MVVM模式

    MVC I’m dating with a model… and a view, and a controller. 众所周知,MVC 是开发客户端最经典的设计模式,iOS 开发也不例外,但是 MVC ...

  4. 预装的Office2016,文件图标表显示以及新建失败问题解决 方法

    新购买笔记本电脑,预装的office2016 学生版 启动激活后,会出现文件图标异常, 文件的类型为: ms-resource:Strings/FtaDisplayName.docx (.docx) ...

  5. XML的一些点

    最近学习Spring会配置许多XML文件,没有系统学习过XML遇到了许多问题,系统的看了一下有些拨云见日的感觉. 推荐学习:http://www.w3school.com.cn/xml/xml_int ...

  6. Egret类class和module写法区别

    普通类 Test.ts class Test { public name:string = "Test"; public run(){ console.log(this.name) ...

  7. 使用Spring报错:No default constructor found;

    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error ...

  8. MUI事件管理

    模块:事件管理 http://dev.dcloud.net.cn/mui/event/ 事件绑定: 除了可以使用addEventListener()方法监听某个特定元素上的事件外, 也可以使用.on( ...

  9. 补课:PageRank

    最近连续听到PageRank算法,久闻其名,不闻其详,心里虚得很,今儿补补课. PageRank算法的网络资料非常全面,毕竟是将近二十年的经典算法,算法细节可以参考文末链接,这里简单说说我的理解. P ...

  10. Css-浅谈如何将多个inline或inline-block元素垂直居中

                一直以来,前端开发过程中本人遇到最多,最烦的问题之一是元素如何垂直居中,发现开发过程中,元素的垂直居中一直是个很大的麻烦事,经常发现PC端和电脑模拟元素都垂直居中了,但是遇到移 ...