如何基于AWS EC2库存主机名的IP地址访问Ansible剧本中的主机名标签?

pr

我在ansible 2.9中使用aws ec2插件。我想使用专用IP地址连接到AWS ec2实例,但是在我的剧本中,我想显示其他主机名标签,例如主机名和区域以及相应的专用IP地址。有关如何在剧本中实现此目标的任何建议?

这是我的ec2广告资源:testall.aws_ec2.yml

plugin: aws_ec2

hostnames:
       - tag:Name
       - tag: Region
       - private-ip-address
compose:
  ansible_host: private_ip_address

这就是我的剧本中的内容:

test-playbook.yml 
---
- hosts:  "{{ variable_host | default('test')}}"
  remote_user: ubuntu
  become: yes
  become_method: sudo
  tasks:
      - debug: 
               msg: "{{ inventory_hostname }}"

...

我按照以下方式运行剧本:

ansible-playbook -i testall.aws_ec2.yml test-playbook.yml --extra-vars variable_host=testall

但是,我的剧本只返回tag:Name(清单中主机名中指定的第一项):

TASK [Gathering Facts] ************************************************************************************************
ok: [test-abc-host1]
ok: [test-abc-host2]
奎尔里耶

第一个匹配项用于arginventory_hostname的值hostnames是按优先级降序排列的列表的情况。

来自ansible-doc --type inventory amazon.aws.aws_ec2

hostnames:
      description:
          - A list in order of precedence for hostname variables.
          - You can use the options specified in U(http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html#options).
          - To use tags as hostnames use the syntax tag:Name=Value to use the hostname Name_Value, or tag:Name to use the value of the Name tag.
      type: list
      default: []

您可以通过模块找到每个清单主机的其他属性ec2_metadata_facts

ansible-doc amazon.aws.ec2_metadata_facts

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章