我需要调用一个shell脚本,该脚本将返回ec2在Ansible任务中的私有ip。一旦我在变量private_ip_var中获得了IP,我希望将该变量注入jinja2模板以生成一个配置文件。
我是这么想的:
- hosts: all
vars:
inline_variable: 'hello again'
tasks:
- name: Gets the IP of the ec2 instance
command: get_ec2_private_ip.sh <----- shell script to dynamically get the ip of ec2
register: private_ip_var` <------ saving shell return value to this var
tasks:
- name: Inject that private_ip_var into the jinja template
template:
src: src=config.cfg.j2
dest: config.cfgconfig.cfg.j2
blah blah
The ip of the ec2 is: {{ private_ip_var }} <------------ THIS IS WHAT I WANT TO ACHIEVE
Variable given as inline - {{ inline_variable }} <------------- DONT CARE ABOUT THIS VAR输出- config.cfg
------
blah blah
The ip of the ec2 is: 10-251-50-12 <----------------- THIS IS WHAT I WANT
Variable given as inline - hello again <---------------- DONT CARE ABOUT THIS VAR我不关心上面的inline_variable;我只关心private_ip_var;如何使用Ansible实现这一点,以便我可以从jinja2模板生成配置文件?
发布于 2020-02-17 08:38:05
要获得AWS EC2信息,您需要在您的(Linux)主机上安装以下内容:
然后使用模块ec2_instance_info。下面是一个示例:
ec2_instance_info:
instance_ids:
- i-23456789然后你可以过滤ip地址信息。您很可能对以下参数感兴趣:
primary (真/假)
。
有关更多示例,请查看:https://docs.ansible.com/ansible/latest/modules/ec2_instance_info_module.html#examples
https://stackoverflow.com/questions/60251928
复制相似问题