趁着最近在搞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. 【顽固BUG】Visual Studio 2015 + TestDriven.NET-3.8.2860_Personal_Beta 调用的目标发生了异常。

    前言 突然怎么弄也无法断点调试了 输出如下: ------ Test started: Assembly: Server5.V2.dll ------ 调用的目标发生了异常. 而且网站运行提示: -- ...

  2. java基础---->多线程之Daemon(五)

    在java线程中有两种线程,一种是用户线程,另一种是守护线程.守护线程是一种特殊的线程,当进程中不存在非守护线程了,则守护线程自动销毁.今天我们通过实例来学习一下java中关于守护线程的知识.我是个平 ...

  3. [干货] 有了微信小程序,谁还学ReactNative?

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

  4. window.location.href和document.location.href、document.URL的区别

    1.document表示的是一个文档对象,window表示的是一个窗口对象,一个窗口下可以有多个文档对象. 所以一个窗口下只有一个window.location.href,但是可能有多个documen ...

  5. nodejs 事件EventEmitter

    index.js: // 引入 events 模块 var events = require('events'); //处理函数要写在调用前 var eventHandler = function() ...

  6. Egret3D学习笔记一 (Unity插件使用)

    一 官方教程: http://developer.egret.com/cn/github/egret-docs/Engine3D/getStarted/getStarted/index.html 大部 ...

  7. jQuery Ajax 全解析(转载)

    本文地址: jQuery Ajax 全解析 本文作者:QLeelulu 转载请标明出处! jQuery确实是一个挺好的轻量级的JS框架,能帮助我们快速的开发JS应用,并在一定程度上改变了我们写Java ...

  8. 170718、springboot编程之发送邮件

    Spring提供了非常好用的JavaMailSender接口实现邮件发送.在Spring Boot的Starter模块中也为此提供了自动化配置.下面通过实例看看如何在Spring Boot中使用Jav ...

  9. Webpack基础入门学习笔记

    # Webpack Project Build 1.创建一个项目目录文件夹 如:D:/demo 2.打开demo文件夹,按住Shift + 鼠标右键,选择[在此处打开命令窗口] 3.初始化npm,生成 ...

  10. 使用angular路由切换后 轮播以及iscrollJs失效的问题

    我们在使用angular的时候,路由总是最让人头疼的地方. 在这里为大家解决一些用angular来回切换遗留下的小问题 比如我们在使用ng-route时如果主页面含有轮播图,当你切换到其他页面再切回主 ...