【问题标题】:Ansible playbook to get all private Ip of my AWS environmentsAnsible playbook 获取我的 AWS 环境的所有私有 IP
【发布时间】:2019-09-11 15:07:38
【问题描述】:

我正在使用 Ansible pLaybook 对我的 AWS 上的所有实例进行扫描。我需要获取他们的私有 IP 并列出它们

我曾尝试使用 json 查询来过滤 Json 格式。格式输出是这样的......

ok: [localhost] => {
    "msg": [
        { 
            "private_dns_name": "ip-10.89.3.12.ec2.internal", 
            "private_ip_address": "10.89.3.12", 
            "public_dns_name": "", 
            "public_ip_address": null, 

        }, 
- hosts: localhost
  connection: local
  gather_facts: yes
  tasks:
    - name: Gather EC2 remote facts.
      ec2_remote_facts:
        region: "{{ region | default('us-east-1') }}"
        filters:
          instance-state-name: running
      register: ec2_remote_facts
    -  set_fact:
         msg: "{{ ec2_remote_facts | json_query('results[*].instances[*].private_ip_address') }} "
    - debug: var=msg

我希望输出只是 private_IP 列表

【问题讨论】:

  • 您是否必须仅使用“ec2_remote_facts”来收集有关 EC2 的数据?我问是因为现在不推荐使用此任务。您可以改用“ec2_instance_facts”

标签: amazon-ec2 ansible


【解决方案1】:

我尝试使用“ec2_instance_facts”如下:

- hosts: localhost
  connection: local
  gather_facts: yes
  tasks:
    - name: Gather EC2 remote facts.
      ec2_instance_facts:
        filters:
          availability-zone: ap-south-1b
      register: ec2_instance_facts
    -  set_fact:
         msg: "{{ ec2_instance_facts | json_query('instances[*].private_ip_address') }} "
    - debug: var=msg

下面是输出:

PLAY [localhost] **************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Gather EC2 remote facts.] ***********************************************************************************************************************************************************************************
ok: [localhost]

TASK [set_fact] ***************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        "172.31.6.87"
    ]
}

PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost                  : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

根据我创建的 EC2 实例,这是正确的。

【讨论】:

    猜你喜欢
    • 2016-09-24
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多