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.
Why does my debug message list gather the server names but not all of the public and private IPs?
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.