rebert
By:
rebert

Debug with Ansible not working proper

August 1, 2017 91 views
Ansible Logging Ubuntu

I created a ansible playbook. playbook works fine. It creates several droplets in different regions. The problem is the debug section. As you can see the debugging iterates fthrough the droplets and should display the ipaddress. The output shows me the whole json code from droplets and in the end the desired message.
how can I filter the whole code and display the ip
address of the droplets ?

Thanks for help.

Ansible Playbook:

  • hosts: localhost connection: local gather_facts: false

tasks:
- name: Create Test Host at Digital Ocean

  digital_ocean:
    state: present
    command: droplet
    ssh_key_ids: *************
    name: "{{ item.name }}"
    api_token: *****************
    size_id: 512mb
    region_id: "{{ item.regions }}"
    image_id: ubuntu-14-04-x64
    unique_name: yes
    wait_timeout: 500
  with_items:
    - {name: "bfh01", regions: "nyc1"}
    - {name: "bfh02", region: "fra1"}
    - {name: "bfh03", region: "sgp1"}
    - {name: "bfh04", region: "sfo1"}

  register: droplet

- debug: 
    msg="IP is {{ item.droplet.ip_address }}"
  with_items:
   - "{{ droplet.results }}"

Output Example:
TASK [debug] ****************************************************************************************************************************************************************
ok: [localhost] => (item={'ansibleparsed': True, u'droplet': {u'status': u'active', u'kernel': None, u'volumeids': [], u'locked': False, u'features': [], u'tags': [], u'image': {u'mindisksize': 20, u'name': u'14.04.5 x64', u'createdat': u'2017-06-21T13:17:21Z', u'slug': u'ubuntu-14-04-x64', u'regions': [u'nyc1', u'sfo1', u'nyc2', u'ams2', u'sgp1', u'lon1', u'nyc3', u'ams3', u'fra1', u'tor1', u'sfo2', u'blr1'], u'id': 25758610, u'distribution': u'Ubuntu', u'type': u'snapshot', u'public': True, u'sizegigabytes': 0.27}, u'snapshotids': [], u'sizeslug': u'512mb', u'networks': {u'v4': [{u'type': u'public', u'netmask': u'255.255.240.0', u'ipaddress': u'165.227.84.22', u'gateway': u'165.227.80.1'}], u'v6': []}, u'nextbackupwindow': None, u'vcpus': 1, u'backupids': [], u'memory': 512, u'region': {u'available': True, u'sizes': [u'512mb', u'1gb', u'2gb', u'4gb', u'8gb', u'16gb'], u'slug': u'nyc1', u'name': u'New York 1', u'features': [u'privatenetworking', u'backups', u'ipv6', u'metadata', u'installagent', u'storage']}, u'createdat': u'2017-08-01T10:09:41Z', u'disk': 20, u'ipaddress': u'165.227.84.22', u'id': 56978525, u'size': {u'pricemonthly': 5.0, u'available': True, u'transfer': 1.0, u'pricehourly': 0.00744, u'regions': [u'ams2', u'ams3', u'blr1', u'fra1', u'lon1', u'nyc1', u'nyc2', u'nyc3', u'sfo1', u'sfo2', u'sgp1', u'tor1'], u'vcpus': 1, u'memory': 512, u'disk': 20, u'slug': u'512mb'}, u'name': u'bfh01'}, u'changed': True, 'ansiblenolog': False, 'ansibleitemresult': True, 'item': {u'regions': u'nyc1', u'name': u'bfh01'}, u'invocation': {u'moduleargs': {u'uniquename': True, u'virtio': True, u'regionid': u'nyc1', u'sizeid': u'512mb', u'apitoken': u'VALUESPECIFIEDINNOLOGPARAMETER', u'backupsenabled': False, u'userdata': None, u'id': None, u'imageid': u'ubuntu-14-04-x64', u'state': u'present', u'waittimeout': 500, u'command': u'droplet', u'sshkeyids': [u'**********'], u'ipv6': False, u'privatenetworking': False, u'wait': True, u'sshpubkey': None, u'name': u'bfh01'}}}) => {
"item": {
"changed": true,
"droplet": {
"backupids": [],
"created
at": "2017-08-01T10:09:41Z",
"disk": 20,
"features": [],
"id": 56978525,
"image": {
"createdat": "2017-06-21T13:17:21Z",
"distribution": "Ubuntu",
"id": 25758610,
"min
disksize": 20,
"name": "14.04.5 x64",
"public": true,
"regions": [
"nyc1",
"sfo1",
"nyc2",
"ams2",
"sgp1",
"lon1",
"nyc3",
"ams3",
"fra1",
"tor1",
"sfo2",
"blr1"
],
"size
gigabytes": 0.27,
"slug": "ubuntu-14-04-x64",
"type": "snapshot"
},
"ipaddress": "165.227.84.22",
"kernel": null,
"locked": false,
"memory": 512,
"name": "bfh01",
"networks": {
"v4": [
{
"gateway": "165.227.80.1",
"ip
address": "165.227.84.22",
"netmask": "255.255.240.0",
"type": "public"
}
],
"v6": []
},
"nextbackupwindow": null,
"region": {
"available": true,
"features": [
"privatenetworking",
"backups",
"ipv6",
"metadata",
"install
agent",
"storage"
],
"name": "New York 1",
"sizes": [
"512mb",
"1gb",
"2gb",
"4gb",
"8gb",
"16gb"
],
"slug": "nyc1"
},
"size": {
"available": true,
"disk": 20,
"memory": 512,
"pricehourly": 0.00744,
"price
monthly": 5.0,
"regions": [
"ams2",
"ams3",
"blr1",
"fra1",
"lon1",
"nyc1",
"nyc2",
"nyc3",
"sfo1",
"sfo2",
"sgp1",
"tor1"
],
"slug": "512mb",
"transfer": 1.0,
"vcpus": 1
},
"sizeslug": "512mb",
"snapshot
ids": [],
"status": "active",
"tags": [],
"vcpus": 1,
"volumeids": []
},
"invocation": {
"module
args": {
"apitoken": "VALUESPECIFIEDINNOLOGPARAMETER",
"backupsenabled": false,
"command": "droplet",
"id": null,
"image
id": "ubuntu-14-04-x64",
"ipv6": false,
"name": "bfh01",
"privatenetworking": false,
"region
id": "nyc1",
"sizeid": "512mb",
"ssh
keyids": [
"**********"
],
"ssh
pubkey": null,
"state": "present",
"unique
name": true,
"userdata": null,
"virtio": true,
"wait": true,
"wait
timeout": 500
}
},
"item": {
"name": "bfh01",
"regions": "nyc1"
}
},
"msg": "IP is 165.227.84.22"
}

PLAY RECAP ******************************************************************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0

Be the first one to answer this question.