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: 用于指定操作 ...
随机推荐
- 【go】go语言socket通信样例
server.go package main import ( "net" "fmt" "io" ) func main() { liste ...
- java高级---->Thread之FutureTask的使用
FutureTask类是Future 的一个实现,并实现了Runnable,所以可通过Excutor(线程池) 来执行,也可传递给Thread对象执行.今天我们通过实例来学习一下FutureTask的 ...
- JS-随机div颜色
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- LeetCode - Nth Highest Salary
题目大概意思是要求找出第n高的Salary,直接写一个Function.作为一个SQL新手我学到了1.SQL中Function的写法和变量的定义,取值.2.limit查询分 页的方法. 在这个题 ...
- PHP获取POST的原始数据的方法
一般我们都用$_POST或$_REQUEST两个预定义变量来接收POST提交的数据.但如果提交的数据没有变量名,而是直接的字符串,则需要使用其他的方式来接收. 方法一: 使用全局变量$GLOBALS[ ...
- window自带字体
一.在默认情况下, Windows 默认提供下列字体: Windows 95/98/98SE 宋体.黑体.楷体_GB2312.仿宋_GB2312 Windows XP/2000/2003/ME/NT ...
- SSH配置优化和慢的解决方法
author: headsen chen date: 2018-08-18 00:28:37 ssh配置优化 vim /etc/ssh/sshd_config 1,修改root端口 2,不允许ro ...
- Golang学习 - builtin 包
Go builtin包提供了go预先声明的函数.变量等的文档.这些函数变量等的实现其实并不是在builtin包里,只是为了方便文档组织. 这些内置的变量.函数.类型无需引入包即可使用. 默认提供的有: ...
- LaTeX:Question & Answer
tikz 宏包中循环 foreach 的使用方法 矩阵环境输入 displaystyle 分式与垂直间距的设置 在 LaTeX 中使用 mathrsfs 宏包遇到 "rsfs7.tfm&qu ...
- rk3188 公板调试记录
打开ccache后编译android果然快很多. make rk3188_sdk_defconfig 触摸屏有问题,然后再吧input底下的touchscreen 屏蔽掉 james@jame ...