我想在AWS_EC2库存文件中使用环境变量,以便能够轻松分离不同的环境的简单情况。让我们以此配置为例:
plugin: aws_ec2
filters:
#tag:Cust_code: "01"
tag:Cust_code: "{{ lookup('env','CUSTOMER_CODE') }}"
虽然第一行有效(已注释),但第二行显然没有,并且返回了一个空的主机列表:
$ export CUSTOMER_CODE="01"
$ echo $CUSTOMER_CODE
01
$ ansible-inventory -i inventory/aws_ec2.yaml --graph
@all:
|--@aws_ec2:
|--@ungrouped:
我读到原因是因为jinja2模板在库存文件中不支持jinja2模板,即使它们似乎根据这篇文章为某些特定参数工作 - a>。
我不想使用动态库存脚本,因为我觉得它可能太复杂了,我不理解它的官方文档。我还希望不在不同环境中使用不同的库存文件,因为我已经必须在同一环境中使用2个不同的库存文件(因为对于某些主机,我需要通过Compose和Jumphosts使用“ Ansible_host:private_ip_address”,而对于我无法使用的jumphosts )。尽管如果没有更好的选择,后者必须是解决方案。
是否有人能够为这个问题提出一个巧妙的解决方案?
I'd like to use an environmental variable inside aws_ec2 inventory file for the simple case of being able to easily separate different environments. Let's take this configuration as an example:
plugin: aws_ec2
filters:
#tag:Cust_code: "01"
tag:Cust_code: "{{ lookup('env','CUSTOMER_CODE') }}"
While the first line works (commented out), the second obviously doesn't and an empty host list is returned:
$ export CUSTOMER_CODE="01"
$ echo $CUSTOMER_CODE
01
$ ansible-inventory -i inventory/aws_ec2.yaml --graph
@all:
|--@aws_ec2:
|--@ungrouped:
I've read that the reason is because jinja2 templates are not supported in inventory files, even though they seem to work for some specific parameters according to this post - https://stackoverflow.com/a/72241930/19407408 .
I don't want to use dynamic inventory script because I feel it might be too complicated and I don't understand the official documentation for it. I also would prefer not to use different inventory files for different environments as I already have to use 2 different inventory files for the same environment (because for some hosts I need to use "ansible_host: private_ip_address" via compose and for jumphosts I can't). Although the latter will have to be the solution if there's no better alternative.
Has anyone been able to come up with a clever solution to this problem?
发布评论
评论(1)
否,<代码>过滤器:(及其
dubl_filters:
andinclude_filters:
兄弟姐妹)不知道。关于开放源代码的好处是,人们可以在引擎盖下看到事物的工作方式:_query
应用包含和排除过滤器tag:name = whything
toto
[{“ name”:“ name”:“ tag:” tag:“ name”,“ values”,“ nathy”] }]
boto想要的格式,而无需进一步触摸键或值dictif-instances
,而无需涉及jinja2jinja2列表包含,使用
组:
或keyed_groups:
可能是一个选项以及参数化- 主机:
在您的剧本中(例如- - 主机:cust_code {{customer_code}}
)否则,我想您最好的选择是使用
add_host:
在剧本中的单独播放中,因为这使您几乎可以实现无限制的自定义:No,
filters:
(and itsexclude_filters:
andinclude_filters:
siblings) are not jinja2 aware. The good thing about ansible being open source is that one can see under the hood how things work:_query
applies the include and exclude filtersansible_dict_to_boto3_filter_list
merely pivots thetag:Name=whatever
over to[{"Name":"tag:Name","Values":["whatever"]}]
format that boto wants, without further touching the key nor the values_get_instances_by_region
just callsdescribe-instances
with those filters, again, without involving jinja2Depending on how many instances the unfiltered list contains, using
groups:
orkeyed_groups:
may be an option along with a parameterized- hosts:
in your playbook (e.g.- hosts: cust_code{{ CUSTOMER_CODE }}
)Otherwise, I'd guess your best bet would be to use
add_host:
in a separate play in the playbook, since that allows you to have almost unlimited customization: