I am playing with ansible to automate ec2 instances. I created ec2 instance and then m trying to get its public dns name via ec2_remote_facts module. but it is giving variable not defined in output.
playbook:
- hosts: localhost
connection: local
tasks:
- name: ec2 instance facts
ec2_remote_facts:
region: ap-southeast-2
filters:
instance-state-name: running
register: ec2
- debug: var=ec2.instances.public_name
Output:PLAY [localhost] ***************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [create ec2 instance] *****************************************************
ok: [localhost]
TASK [debug] *******************************************************************
ok: [localhost] => {
"ec2.instances.public_name": "VARIABLE IS NOT DEFINED!"
}
PLAY RECAP *********************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0
Output for ec2.instances.
ok: [localhost] => {
"ec2.instances": [
{
"ami_launch_index": "0",
"architecture": "x86_64",
"client_token": "",
"ebs_optimized": false,
"groups": [
{
"id": "sg-6c016a08",
"name": "default"
}
],
"hypervisor": "xen",
"id": "i-915b1813",
"image_id": "ami-fedafc9d",
"instance_profile": null,
"interfaces": [
{
"id": "eni-96de4acf",
"mac_address": "0a:14:ac:64:c4:13"
}
],
"kernel": null,
"key_name": "ansible.key",
"launch_time": "2016-08-29T07:32:10.000Z",
"monitoring_state": "disabled",
"persistent": false,
"placement": {
"tenancy": "default",
"zone": "ap-southeast-2c"
},
"private_dns_name": "ip-xx-xx-xx-107.ap-southeast-2.compute.internal",
"private_ip_address": "xx.xx.xx.107",
"public_dns_name": "ec2-xx-xxx-xx-80.ap-southeast-2.compute.amazonaws.com",
"ramdisk": null,
"region": "ap-southeast-2",
"requester_id": null,
"root_device_type": "ebs",
"source_destination_check": "true",
"spot_instance_request_id": null,
"state": "running",
"tags": {
"Name": "Demo"
},
"virtualization_type": "hvm",
"vpc_id": "vpc-abcaf4ce"
}
]
}
what i m missing here?
Thanks
Benjo
If you look closely to ec2.instances you may note that:
it is a list, so you either access items by index ec2.instances[0] or iterate over them with with_items: structure.
there is no public_name attribute, only public_dns_name.
Related
I am trying to read a csv file. However, during print its not reading line by line instead its going over some internal loop and duplicating results.
- name: read csv
read_csv:
path: /u00/app/monitor/newrelic_test.csv
key: Application
register: newrelic
- name: Print newrelic var
ansible.builtin.debug:
var: newrelic.list
output looks lke below:
[testserver1] =>
"newrelic.list":
"Application": "Microsoft",
"env": "Test",
"Datacenter": "DC1",
"Hostname": "testserver1",
},
"Application": "Apple",
"env": "Test",
"Datacenter": "DC2",
"Hostname": "testserver2",
}
[testserver2] => {
"newrelic.list":
{
"Application": "Microsoft",
"env": "Test",
"Datacenter": "DC1",
"Hostname": "testserver1",
},
"Application": "Apple",
"env": "Test",
"Datacenter": "DC2",
"Hostname": "testserver2",
}
]
csv file:
Application,env,Datacenter,Hostname
Microsoft,Test,DC1,testserver1
Apple,Test,DC2,testserver2
Expected result shall be:
[testserver1] =>
"newrelic.list":
"Application": "Microsoft",
"env": "Test",
"Datacenter": "DC1",
"Hostname": "testserver1",
}
[testserver2] => {
"newrelic.list":
{
"Application": "Apple",
"env": "Test",
"Datacenter": "DC2",
"Hostname": "testserver2",
}
]
Edit : - Next block
- name: copy template
template:
src: /u00/ansible/Playbooks/files/infra-config.yml_template
dest: /u00/app/monitor/infra-config.yml
loop: "{{ newrelic.list }}"
loop_control:
loop_var: item
Issue: if newrelic.list is not been used then get error as below:
[testserver1]: FAILED! => {"msg": "Invalid data passed to 'loop', it requires a list, got this instead: {'dict':
I tried adding dict2items but then template not able to read items.
The read_csv module doesn't read a CSV file "line by line"; from the documentation:
Read[s] a CSV file and return[s] a list or a dictionary, containing one dictionary per row.
So when you run this task:
- name: read csv
read_csv:
path: data.csv
key: Application
register: newrelic
You get a variable newrelic which has an empty list element and a dict element containing the data from the CSV file, keyed by Application (because that's what you asked for). If use a debug task to look at the value of newrelic, we see:
ok: [localhost] => {
"newrelic": {
"changed": false,
"dict": {
"Apple": {
"Application": "Apple",
"Datacenter": "DC2",
"Hostname": "testserver2",
"env": "Test"
},
"Microsoft": {
"Application": "Microsoft",
"Datacenter": "DC1",
"Hostname": "testserver1",
"env": "Test"
}
},
"failed": false,
"list": []
}
}
It's not clear exactly what you're trying to do here, but if you want to look up values from your CSV file by inventory hostname, you would need to key it by Hostname instead:
- name: read csv
read_csv:
path: data.csv
key: Hostname
register: newrelic
And then you could write:
- name: Print newrelic var
ansible.builtin.debug:
var: newrelic.dict[inventory_hostname]
Assuming you have hosts testserver1 and testserver2 in your inventory, a playbook like this:
- hosts: localhost
gather_facts: false
tasks:
- name: read csv
read_csv:
path: data.csv
key: Hostname
register: newrelic
- hosts: all
gather_facts: false
tasks:
- name: Print newrelic var
ansible.builtin.debug:
var: hostvars.localhost.newrelic.dict[inventory_hostname]
Would result in this output:
PLAY [localhost] ***************************************************************
TASK [read csv] ****************************************************************
ok: [localhost]
PLAY [all] *********************************************************************
TASK [Print newrelic var] ******************************************************
ok: [testserver1] => {
"hostvars.localhost.newrelic.dict[inventory_hostname]": {
"Application": "Microsoft",
"Datacenter": "DC1",
"Hostname": "testserver1",
"env": "Test"
}
}
ok: [testserver2] => {
"hostvars.localhost.newrelic.dict[inventory_hostname]": {
"Application": "Apple",
"Datacenter": "DC2",
"Hostname": "testserver2",
"env": "Test"
}
}
PLAY RECAP *********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
testserver1 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
testserver2 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
The gcp_sql_instance_info module does not seem to allow you to specify the name of the database you are trying to get info for. All it does is every detail of every database in that project.
What would be the best way to extract the "ipAddress" of a database based on it's name and register that value to a variable?
{
"resources": [
{
"kind": "sql#instance",
"state": "RUNNABLE",
"databaseVersion": "POSTGRES_13",
"settings": {
"authorizedGaeApplications": [],
"tier": "db-custom-1-3840",
"kind": "sql#settings",
"availabilityType": "ZONAL",
"pricingPlan": "PER_USE",
"replicationType": "SYNCHRONOUS",
"activationPolicy": "ALWAYS",
"ipConfiguration": {
"privateNetwork": "example-network",
"authorizedNetworks": [],
"ipv4Enabled": false
},
"locationPreference": {
"zone": "us-west1-b",
"kind": "sql#locationPreference"
},
"dataDiskType": "PD_SSD",
"maintenanceWindow": {
"kind": "sql#maintenanceWindow",
"hour": 0,
"day": 0
},
"backupConfiguration": {
"startTime": "02:00",
"kind": "sql#backupConfiguration",
"backupRetentionSettings": {
"retentionUnit": "COUNT",
"retainedBackups": 7
},
"enabled": false,
"replicationLogArchivingEnabled": false,
"pointInTimeRecoveryEnabled": false,
"transactionLogRetentionDays": 7
},
"settingsVersion": "3",
"storageAutoResizeLimit": "0",
"storageAutoResize": true,
"dataDiskSizeGb": "100"
},
"ipAddresses": [
{
"type": "PRIVATE",
"ipAddress": "10.10.10.1"
}
],
"instanceType": "CLOUD_SQL_INSTANCE",
"backendType": "SECOND_GEN",
"name": "database-1",
"region": "us-west1",
"gceZone": "us-west1-b",
"databaseInstalledVersion": "POSTGRES_13_7",
"createTime": "2022-07-22T20:20:32.274Z"
},
{
"kind": "sql#instance",
"state": "RUNNABLE",
"databaseVersion": "MYSQL_8_0",
"settings": {
"authorizedGaeApplications": [],
"tier": "db-n1-standard-1",
"kind": "sql#settings",
"availabilityType": "ZONAL",
"pricingPlan": "PER_USE",
"replicationType": "SYNCHRONOUS",
"activationPolicy": "ALWAYS",
"ipConfiguration": {
"privateNetwork": "example-network",
"authorizedNetworks": [],
"ipv4Enabled": false
},
"locationPreference": {
"zone": "us-west1-c",
"kind": "sql#locationPreference"
},
"dataDiskType": "PD_SSD",
"backupConfiguration": {
"startTime": "21:00",
"kind": "sql#backupConfiguration",
"backupRetentionSettings": {
"retentionUnit": "COUNT",
"retainedBackups": 7
},
"enabled": false,
"transactionLogRetentionDays": 7
},
"settingsVersion": "1",
"storageAutoResizeLimit": "0",
"storageAutoResize": true,
"dataDiskSizeGb": "10"
},
"ipAddresses": [
{
"type": "PRIVATE",
"ipAddress": "10.10.10.2"
}
],
"instanceType": "CLOUD_SQL_INSTANCE",
"backendType": "SECOND_GEN",
"name": "database-2",
"region": "us-west1",
"gceZone": "us-west1-c",
"databaseInstalledVersion": "MYSQL_8_0_26",
"createTime": "2022-07-27T19:27:59.235Z"
}
]
Q: "Extract the ipAddress of a database based on its name."
A: Crete a dictionary. For example, select the first item from the list ipAddresses
name_ip: "{{ dict(resources|json_query(name_ip_query)) }}"
name_ip_query: '[].[name, ipAddresses[0].ipAddress]'
give
name_ip:
database-1: 10.10.10.1
database-2: 10.10.10.2
Notes
Example of a complete playbook for testing
- hosts: localhost
vars_files:
- resources.yml
vars:
name_ip: "{{ dict(resources|json_query(name_ip_query)) }}"
name_ip_query: '[].[name, ipAddresses[0].ipAddress]'
name_ver: "{{ dict(resources|json_query(name_ver_query)) }}"
name_ver_query: '[].[name, databaseVersion]'
ip_of_db1: "{{ name_ip['database-1'] }}"
tasks:
- debug:
var: name_ip
- debug:
var: name_ver
- debug:
var: ip_of_db1
gives
TASK [debug] *********************************************************************************
ok: [localhost] =>
name_ip:
database-1: 10.10.10.1
database-2: 10.10.10.2
TASK [debug] *********************************************************************************
ok: [localhost] =>
name_ver:
database-1: POSTGRES_13
database-2: MYSQL_8_0
TASK [debug] *********************************************************************************
ok: [localhost] =>
ip_of_db1: 10.10.10.1
There might be more IP addresses. The variable ipAddresses is a list. An option would be to create a list of all IP addresses. For example,
name_ips: "{{ dict(resources|json_query(name_ips_query)) }}"
name_ips_query: '[].[name, ipAddresses[].ipAddress]'
give
name_ips:
database-1: [10.10.10.1]
database-2: [10.10.10.2]
This is a working playbook to achieve the desired output:
- hosts: localhost
vars:
db_instance_name: instance-1
tasks:
- name: get info on an instance
gcp_sql_instance_info:
project: project_id
auth_kind: serviceaccount
service_account_file: "/path/to/sa/example.json"
register: cloud_sql_resources
- set_fact:
query: "resources[?contains(name,'{{ db_instance_name }}')].ipAddresses[].ipAddress"
- set_fact:
ipAddress: "{{ cloud_sql_resources | to_json | from_json | json_query(query) }}"
- debug:
msg: "{{ ipAddress }}"
You can run your playbook like this: ansible-playbook -v main.yaml --extra-vars "db_instance_name=instance-1", that would print:
TASK [debug] ********************************************************************************************************************
ok: [localhost] => {
"msg": [
"35.224.46.127"
]
}
I'm trying to extract value of vm datastore with ansible snippet:
- name: Gather disk facts from virtual machine using name
vmware_guest_disk_facts:
hostname: "{{ vcenter_server }}"
username: "{{ vcenter_user }}"
password: "{{ vcenter_pass }}"
datacenter: "{{ datacenter }}"
validate_certs: no
name: "{{ item }}"
delegate_to: localhost
register: disk_facts
with_items: "{{ vm_list }}"
- name: Get disk info
debug:
var: disk_facts.results.guest_disk_facts.backing_datastore
Output:
[root#sysmgttl1 lvm]# ansible-playbook -i hosts vmfacts.yml -e vcenter_server=vmimgtpw002 -e vm_list=sndprfql8
PLAY [sndprfql8] ***********************************************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************************************
ok: [sndprfql8]
TASK [Gather disk facts from virtual machine using name] *******************************************************************************************************************
ok: [sndprfql8 -> localhost] => (item=sndprfql8)
TASK [Get disk info] *******************************************************************************************************************************************************
ok: [sndprfql8] => {
"disk_facts.results.guest_disk_facts.backing_datastore": "VARIABLE IS NOT DEFINED!"
}
PLAY RECAP *****************************************************************************************************************************************************************
sndprfql8 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
This is what my disk_facts looks like.
ok: [sndprfql8] => {
"disk_facts": {
"changed": false,
"msg": "All items completed",
"results": [
{
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"guest_disk_facts": {
"0": {
"backing_datastore": "QADEV07",
"backing_eagerlyscrub": false,
"backing_filename": "[QADEV07] sndprfql8/sndprfql8.vmdk",
"backing_thinprovisioned": true,
"backing_type": "FlatVer2",
"backing_uuid": "6000C292-7716-6296-de04-69bac9186661",
"backing_writethrough": false,
"capacity_in_bytes": 68719476736,
"capacity_in_kb": 67108864,
"controller_key": 1000,
"key": 2000,
"label": "Hard disk 1",
"summary": "67,108,864 KB",
"unit_number": 0
}
},
"invocation": {
"module_args": {
"datacenter": "RDC",
"folder": null,
"hostname": "vmimgtpw002",
"name": "sndprfql8",
"password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"port": 443,
"use_instance_uuid": false,
"username": "svcvread#chop.edu",
"uuid": null,
"validate_certs": false
}
},
"item": "sndprfql8"
}
]
}
}
If this is really for a one shot use, the following should do:
- name: Get disk info
debug:
var: disk_facts.results[0].guest_disk_facts['0'].backing_datastore
If you goal is to get all values for a list of vms in the result possibly having several disks, the following will give you a flattened list (that you can de-duplicate with the unique filter if needed)
- debug:
msg: "{{ disk_facts | json_query('results[].guest_disk_facts.*[].backing_datastore') }}"
I am trying to store the IP address of my loopback0 as variable. I can call the ipv4 information, but it doesn't let me call the address information. [Scrubbed Info for Privacy]
Code:
- name: Configure IPSLA on Americas Router
gather_facts: false
hosts: IP_SLA
connection: local
serial: 1
tasks:
- name: Gather Switch Info
ios_facts:
- name: Debug
debug:
var: ansible_facts["net_interfaces"]["Loopback0"]["ipv4"]
...
Output:
PLAY [Configure IPSLA on Americas Router] ***************************************************************************************************************************************************************************************************
TASK [Gather Switch Info] *******************************************************************************************************************************************************************************************************************
ok: [host] => {"ansible_facts": {"ansible_net_interfaces": {"Loopback0": {"bandwidth": 8000000, "description": null, "duplex": null, "ipv4": [{"address": "10.x.x.x", "subnet": "32"}], "lineprotocol": "up ", "macaddress": null, "mediatype": null, "mtu": 1514, "operstatus": "up", "type": null}
TASK [Debug] ********************************************************************************************************************************************************************************************************************************
ok: [host] => {
"ansible_facts[\"net_interfaces\"][\"Loopback0\"][\"ipv4\"]": [
{
"address": "10.x.x.x",
"subnet": "32"
}
]
}
But when I try and call the address:
- name: Debug
debug:
var: ansible_facts["net_interfaces"]["Loopback0"]["ipv4"]["address"]
I end up with this error:
TASK [Debug] ****************************************************************************************************************************************
ok: [host] => {
"ansible_facts[\"net_interfaces\"][\"Loopback0\"][\"ipv4\"][\"address\"]": "VARIABLE IS NOT DEFINED!: 'list object' has no attribute 'address'"
How do I get the address stored as a variable so I can use it in future tasks?
The square brackets in:
ok: [host] => {
"ansible_facts[\"net_interfaces\"][\"Loopback0\"][\"ipv4\"]": [
{
"address": "10.x.x.x",
"subnet": "32"
}
]
}
Indicate this ["net_interfaces"]["Loopback0"]["ipv4"] is an array with a single element.
Use [0] to indicate the first element of the array, as demonstrated by the following code:
ansible_facts["net_interfaces"]["Loopback0"]["ipv4"][0]["address"]
I've got multiple values under mounts, I want to have all 'mountpoints' of these attributes.
"mounts": {
"/dev/sdb": {
"fstype": "xfs",
"mountpoint": "/my/point1",
"opts": "defaults,_netdev",
"partition": "/dev/sdb1",
"state": "mounted"
},
"/dev/sdc": {
"fstype": "xfs",
"mountpoint": "/my/point2",
"opts": "defaults,_netdev",
"partition": "/dev/sdc1",
"state": "mounted"
},
"/dev/sdd": {
"fstype": "xfs",
"mountpoint": "/my/point3",
"opts": "defaults,_netdev",
"partition": "/dev/sdd1",
"state": "mounted"
How do I register the three mountpoints in memory for later use, so I get:
/my/point1, /my/point2, /my/point3
I want to place the values in /etc/updatedb.conf with lineinfile, so there should be no spacing.
My end result would look like; cat /etc/updatedb.conf
PRUNEPATHS = "/my/point1 /my/point2 /my/point3"
Currently, I use a template to copy pre-defined variables. But this is not dynamic enough.
I'm playing around how to get the right variables, but no success:
- debug: var=mount[all].mountpoints
To get a list:
- debug: msg="{{ mounts.values() | map(attribute='mountpoint') | list }}"
Or a string:
- debug: msg="{{ mounts.values() | map(attribute='mountpoint') | list | join(' ') }}"
EDIT:
You can get the join of the keys from mounts like this:
mounts: {{ mounts.keys()|join(', ') }}
(old answer that is not valid for this question)
as this describes, you can do
---
- hosts: all
tasks:
- set_fact: mounts={{ ansible_mounts | map(attribute='device')|join(',')}}
- debug: var=ansible_mounts
- debug: var=mounts
sample output
ap test.yml -i hosts -l server
PLAY [all] *********************************************************************
TASK [setup] *******************************************************************
ok: [server]
TASK [set_fact] ****************************************************************
ok: [server]
TASK [debug] *******************************************************************
ok: [server] => {
"ansible_mounts": [
{
"device": "/dev/sda1",
"fstype": "ext4",
"mount": "/",
"options": "rw,errors=remount-ro",
"size_available": 2890289152,
"size_total": 9376751616,
"uuid": "N/A"
},
{
"device": "/dev/sdb1",
"fstype": "ext4",
"mount": "/mnt/data1",
"options": "rw",
"size_available": 50684461056,
"size_total": 200674758656,
"uuid": "N/A"
}
]
}
TASK [debug] *******************************************************************
ok: [server] => {
"mounts": "/dev/sda1,/dev/sdb1"
}
PLAY RECAP *********************************************************************
server : ok=4 changed=0 unreachable=0 failed=0