【发布时间】:2020-02-16 18:43:56
【问题描述】:
我需要调用一个 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
config.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 模板生成该配置文件?
【问题讨论】:
标签: amazon-web-services amazon-ec2 ansible jinja2 ansible-template