![](/img/trans.png)
[英]How do I use Ansible jinja2 map() and join() filters correctly without template error?
[英]How to get dynamic shell variable with Ansible playbook and jinja2 templates
我需要调用一个 shell 脚本,该脚本将在 Ansible 任务中返回 ec2 的私有 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.cfg
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 模板生成该配置文件?
要获取 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.