有什么方法可以使用AWS AWS动态库存文件中的环境变量(AWS_EC2)

发布于 2025-02-10 11:58:31 字数 785 浏览 1 评论 0 原文

我想在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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迟到的我 2025-02-17 11:58:31

否,<代码>过滤器:(及其 dubl_filters: and include_filters:兄弟姐妹)不知道。关于开放源代码的好处是,人们可以在引擎盖下看到事物的工作方式:

  • _query 应用包含和排除过滤器
    • 仅仅旋转 tag:name = whything to to [{“ name”:“ name”:“ tag:” tag:“ name”,“ values”,“ nathy”] }] boto想要的格式,而无需进一步触摸键或值
    • 再次使用这些过滤器来调用 dictif-instances ,而无需涉及jinja2


jinja2列表包含,使用组: keyed_groups:可能是一个选项以及参数化 - 主机:在您的剧本中(例如 - - 主机:cust_code {{customer_code}}

否则,我想您最好的选择是使用 add_host: 在剧本中的单独播放中,因为这使您几乎可以实现无限制的自定义:

- hosts: localhost
  tasks:
  - add_host: ...
    groups:
    - cust_code_machines

- hosts: cust_code_machines
  tasks:
  - debug: msg="off to the races"

No, filters: (and its exclude_filters: and include_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 filters
    • ansible_dict_to_boto3_filter_list merely pivots the tag: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 calls describe-instances with those filters, again, without involving jinja2

Depending on how many instances the unfiltered list contains, using groups: or keyed_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:

- hosts: localhost
  tasks:
  - add_host: ...
    groups:
    - cust_code_machines

- hosts: cust_code_machines
  tasks:
  - debug: msg="off to the races"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文