ansible之template模块
趁着最近在搞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模块的更多相关文章
- 【Ansible】 各种模块
[Ansible 模块] 就如python库一样,ansible的模块也分成了基本模块和第三方拓展模块(自定义的模块).这些模块其实才是作为真实的逻辑载体,在帮助ansible进行作业. ansibl ...
- Ansible Playbooks 常用模块
官网链接:https://docs.ansible.com/ansible/latest/modules/list_of_all_modules.html ansible python module ...
- Ansible playbooks常用模块案例操作
打开git bash 连接ansible服务器,然后进入deploy用户 #ssh root@192.168.96.188 进入python3.6虚拟环境 #su - deploy #source . ...
- Ansible Playbooks常用模块
File模块 在目标主机创建文件或目录,并赋予其系统权限 - name: create a file file:'path=/oot/foo.txt state=touch mode=0755 own ...
- 二、Ansible基础之模块篇
目录 1. Ansible Ad-Hoc 命令 1.1 命令格式 1.2 模块类型 1.3 联机帮助 1.3.1 常用帮助参数 1.4 常用模块 1.4.1 command & shell 模 ...
- Ansible之roles模块--lnmp分布式部署
Ansible之roles模块--lnmp分布式部署 目录 Ansible之roles模块--lnmp分布式部署 1. role模块的作用 2. roles的目录结构 3. roles内个目录含义解释 ...
- ansible命令执行模块使用
ansible命令执行模块使用 1.命令执行模块-command 在远程节点上运行命令. 命令模块使用命令名称,接上空格-的分割符作为参数使用,但是不支持管道符和变量等,如果要使用这些,那么可以使用s ...
- 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 ...
- 运维自动化神器ansible之user模块
运维自动化神器ansible之user模块 一.概述 user模块 可管理远程主机上的 用户,比如创建用户.修改用户.删除用户.为用户创建密钥对等操作. 二.参数介绍 name: 用于指定操作 ...
随机推荐
- Linux下Redis集群环境的搭建
一.安装redis(使用redis3.0版本) 1.需要gcc环境,如果没有执行命令安装gcc yum install gcc-c++ 2.下载redis3.0的源码包并上传至服务器 3.解压源码包 ...
- 【转载】为ASP.NET MVC及WebApi添加路由优先级
路由方面的: 转载地址:http://www.jb51.net/article/73417.htm Author:lijiao 这是一个对Asp.Net Mvc的一个很小的功能拓展,小项目可能不太需要 ...
- pano2vr制作360全景图
1.下载pano2vr中文破解版2.制作360全景选择"矩形球面投影" 3.输出格式选择HTML5, 也可选择Flash(快被淘汰) 4.HTML5输出选项中3个重要选项4.1 F ...
- C++中的const的用法
const对象.指向const对象的指针.const指针(通过一个面试题来了解) 1.const对象 (1)关于const,很多企业的笔试.面试都会出现,很简单,就问你“const的含义?”. 我 ...
- oracle日常函数汇总(转载)
第一篇 著名函数之单值函数 注:N表示数字型,C表示字符型,D表示日期型,[]表示内中参数可被忽略,fmt表示格式 数值类型函数 数值型函数输入数字型参数并返回数值型的值.多数该类函数的返回值支持38 ...
- 170714、springboot编程之多数据源切换(动态)
(1)新建maven java project; 新建一个maven project,取名为:spring-boot-multi-ds (2)在pom.xml添加依赖包: 在pom.xml文件中加入依 ...
- FNV hash算法
原文:https://blog.csdn.net/u013137970/article/details/79020095 FNV算法简介FNV算法属于非密码学哈希函数,它最初由Glenn Fowler ...
- Linux下手工卸载11.2 RAC(非MOS的deinstall方法)
思路来自于经典的<How to Proceed From a Failed 10g or 11.1 Oracle Clusterware (CRS) Installation (Doc ID 2 ...
- iptables,lokkit,ebtables,arptables---logrotate
iptables,lokkit,ebtables,arptables logrotate 这五个位置也被称为五个钩子函数(hook functions),也叫五个规则链. 1.PREROUTING ...
- UVA796 - Critical Links(Tarjan求桥)
In a computer network a link L, which interconnects two servers, is considered critical if there are ...