在Jinja模板中使用Ansible控制主机的IP地址 [英] Use Ansible control master's IP address in Jinja template

查看:368
本文介绍了在Jinja模板中使用Ansible控制主机的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Ansible剧本使用的J2模板中插入IP地址. IP地址不是正在供应的主机的地址,而是从其进行供应的主机的IP.到目前为止,我发现的所有内容都涉及与所配置的主机相关的变量/事实. 换句话说:执行ansible -m setup 127.0.0.1时,我要插入的IP是['ansible_default_ipv4']['address']中的IP.

I would like to insert an IP address in to a J2 template which is used by an Ansible playbook. That IP adress is not the address of the host which is being provisioned, but the IP of the host from which the provisioning is done. Everything I have found so far covers using variables/facts related to the hosts being provisioned. In other words: the IP I’d like to insert is the one in ['ansible_default_ipv4']['address'] when executing ansible -m setup 127.0.0.1.

我认为我可以使用本地剧本来编写动态生成的包含IP的模板文件,但我希望"Ansible方式"可以做到这一点.

I think that I could use a local playbook to write a dynamically generated template file containing the IP, but I was hoping that this might be possible "the Ansible way".

推荐答案

只需使用此:

{{ ansible_env["SSH_CLIENT"].split()[0] }}

这篇关于在Jinja模板中使用Ansible控制主机的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆