ansible playbook使用jinja2语法渲染inventory下的主机名和IP到/etc/hosts

1. ansible inventory 下面的 hosts内容如下:

bash 复制代码
[all_host]
app1 ansible_host=10.2.162.147 
app2 ansible_host=10.2.162.148
app3 ansible_host=10.2.162.149
app4 ansible_host=10.2.162.150
app5 ansible_host=10.2.162.151

[nginx]
app1

2. hosts.j2内容如下

bash 复制代码
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

{% for host in groups.all_host %}
{{ hostvars[host]['ansible_host'] }} {{ hostvars[host]['inventory_hostname'] }}
{% endfor %}

3. 渲染结果:

bash 复制代码
cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

10.2.162.147 app1
10.2.162.148 app2
10.2.162.149 app3
10.2.162.150 app4
10.2.162.151 app5
相关推荐
liu****8 分钟前
Python 基础语法(二):程序流程控制
开发语言·python·python基础
大连好光景30 分钟前
Python打日志
运维·python·运维开发
syt_biancheng35 分钟前
博客系统全流程测试总结
python·selenium·测试用例·压力测试·postman
可信计算1 小时前
【算法随想】一种基于“视觉表征图”拓扑变化的NLP序列预测新范式
人工智能·笔记·python·算法·自然语言处理
张广涛1 小时前
【无标题】
python
爱笑的眼睛111 小时前
超越剪枝与量化:下一代AI模型压缩工具的技术演进与实践
java·人工智能·python·ai
yaoh.wang1 小时前
力扣(LeetCode) 14: 最长公共前缀 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·跳槽
reasonsummer1 小时前
【办公类-18-07】20251215(Python)“口腔检查涂氟信息”批量生成打印(区名、学号、姓名、学校、班级、身份证、户籍、性别、民族)
开发语言·python
Rabbit_QL2 小时前
【PyTorch】detach:从计算图中切断梯度的原理与实践
人工智能·pytorch·python
晨曦5432102 小时前
MySQL与Python数学函数大比拼
python·mysql