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
相关推荐
Yxh1813778455430 分钟前
抖去推--短视频矩阵系统源码开发
人工智能·python·矩阵
Humbunklung1 小时前
PySide6 GUI 学习笔记——常用类及控件使用方法(多行文本控件QTextEdit)
笔记·python·学习·pyqt
火车叼位1 小时前
使用 uv 工具在 Windows 系统快速下载安装与切换 Python
python
心扬2 小时前
python网络编程
开发语言·网络·python·tcp/ip
忧陌6062 小时前
DAY 44 预训练模型
python
点云SLAM2 小时前
PyTorch 中contiguous函数使用详解和代码演示
人工智能·pytorch·python·3d深度学习·contiguous函数·张量内存布局优化·张量操作
尘浮7282 小时前
60天python训练计划----day45
开发语言·python
哆啦A梦的口袋呀3 小时前
基于Python学习《Head First设计模式》第六章 命令模式
python·学习·设计模式
努力搬砖的咸鱼3 小时前
从零开始搭建 Pytest 测试框架(Python 3.8 + PyCharm 版)
python·pycharm·pytest
Calvex3 小时前
PyCharm集成Conda环境
python·pycharm·conda