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
相关推荐
郝学胜-神的一滴13 小时前
系统设计 012:从用户系统出发,吃透缓存、数据库与高并发设计
java·数据库·python·缓存·php·软件构建
人工智能导论实践课13 小时前
奥比中光深度相机astra pro的初步ros包开发
人工智能·python
wj3055853781 天前
课程 9:模型测试记录与 Prompt 策略
linux·人工智能·python·comfyui
星寂樱易李1 天前
iperf3 + Python-- 网络带宽、网速、网络稳定性
开发语言·网络·python
qingfeng154151 天前
企业微信机器人开发:如何实现自动化与智能运营?
人工智能·python·机器人·自动化·企业微信
彦为君1 天前
Agent 安全:从权限提示到沙箱隔离
python·ai·ai编程
PILIPALAPENG1 天前
Python 语法速成指南:前端开发者视角(JS 类比版)
前端·人工智能·python
用户8356290780511 天前
Python 操作 PowerPoint 页眉与页脚指南
后端·python
枫叶林FYL1 天前
项目九:异步高性能爬虫与数据采集中枢 —— 基于 Crawl<sub>4</sub>AI 与 Playwright 的现代化数据采集平台 项目总览
爬虫·python·深度学习·wpf