0

I have an ansible playbook pretty much written out, including output to a file. I have the few fields that I want to gather from my EC2 environment (simply tag:Name; Public IP, Private IP). I'm having two small issues that I cannot seem to figure out, pertaining to the output.

  1. Why does my debug message list gather the server names but not all of the public and private IPs?

  2. How can I get each Name on its own line with its respective Public and Private IPs?

I have found various snippets and samples around and combined them to create a very basic starter playbook that I will expand on later.

Here is my playbook:

---
- name: Test ec2 info
  hosts: local
  connection: local

  tasks:
    - name: Get EC2 Info
      ec2_instance_info:
        filters:
          "tag:Name": "SERVER0*"
      register: ec2_name

    - name: Get instance IP addresses.
      debug:
          msg: "{{ item.0 }} | {{ item.1 }} | {{ item.2 }}"
      with_together:
        - "{{ ec2_name.instances | map(attribute='tags.Name') | list }}"
        - "{{ ec2_name.instances[0].public_ip_address }}"
        - "{{ ec2_name.instances[0].private_ip_address }}"

    - name: Gather and Save Instance Info
      set_fact:
         Tag_Name: "{{ ec2_name.instances | map(attribute='tags.Name') | list }}"
         Pub_IP: "{{ ec2_name.instances[0].public_ip_address }}"
         Pvt_IP: "{{ ec2_name.instances[0].private_ip_address }}"

    - local_action:
        copy content="{{ Tag_Name }} | {{ Pub_IP }} | {{ Pvt_IP }}" dest=ec2iptest.txt

Here's the debug output to screen:

TASK [Get only running instance IP addresses.] *********************************
ok: [localhost] => (item=['SERVER01', 'xx.xx.xx.xx', '10.1.0.10']) => {
    "msg": "SERVER01 | xx.xx.xx.xx | 10.1.0.10"
}
ok: [localhost] => (item=['SERVER02', None, None]) => {
    "msg": "SERVER02 |  | "
}
ok: [localhost] => (item=['SERVER03', None, None]) => {
    "msg": "SERVER03 |  | "
}
ok: [localhost] => (item=['SERVER04', None, None]) => {
    "msg": "SERVER04 |  | "
}

TASK [Gather and Save Instance Info] *******************************************
ok: [localhost]

TASK [copy] ********************************************************************
changed: [localhost -> localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=5    changed=1

And here is the output to the text file:

['SERVER01', 'SERVER02', 'SERVER03', 'SERVER04'] | xx.xx.xx.xx | 10.1.0.10

It's putting all server tag Names on one line in brackets and providing only one public IP and one private IP.

My desired output to text file (with pipe delimiters) is:

SERVER_NAME01 | PublicIP | PrivateIP
SERVER_NAME02 | PublicIP | PrivateIP
SERVER_NAME03 | PublicIP | PrivateIP
...

Any assistance with the correct gathering and line wrapping would be great.

Thank you.

Cochise
  • 1
  • 4

2 Answers2

0

I think what you need is like below:

Pub_IP: "{{ ec2_name.instances[0].network_interfaces[0].association.public_ip }}"

Pvt_IP: "{{ ec2_name.instances[0].network_interfaces[0].private_ip_address }}"
Dwayne
  • 207
  • 2
  • 5
  • Thank you for the reply, but unfortunately did not work for me. Adding `.network_interfaces[0]` causes an error: `'dict object' has no attribute 'public_ip_address'`. – Cochise May 22 '20 at 13:43
0

register: info

Pub_IP: "info['instances'][0]['public_ip_address']"
Pvt_IP: "info['instances'][0]['private_ip_address']"
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 13 '21 at 00:03
  • Please add some explanation to your answer such that others can learn from it – Nico Haase Dec 16 '21 at 09:32