Debug with Ansible not working proper

August 1, 2017 883 views
Ansible Logging Ubuntu
rebert
By:
rebert

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={'_ansible_parsed': True, u'droplet': {u'status': u'active', u'kernel': None, u'volume_ids': [], u'locked': False, u'features': [], u'tags': [], u'image': {u'min_disk_size': 20, u'name': u'14.04.5 x64', u'created_at': 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'size_gigabytes': 0.27}, u'snapshot_ids': [], u'size_slug': u'512mb', u'networks': {u'v4': [{u'type': u'public', u'netmask': u'255.255.240.0', u'ip_address': u'165.227.84.22', u'gateway': u'165.227.80.1'}], u'v6': []}, u'next_backup_window': None, u'vcpus': 1, u'backup_ids': [], 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'private_networking', u'backups', u'ipv6', u'metadata', u'install_agent', u'storage']}, u'created_at': u'2017-08-01T10:09:41Z', u'disk': 20, u'ip_address': u'165.227.84.22', u'id': 56978525, u'size': {u'price_monthly': 5.0, u'available': True, u'transfer': 1.0, u'price_hourly': 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, '_ansible_no_log': False, '_ansible_item_result': True, 'item': {u'regions': u'nyc1', u'name': u'bfh01'}, u'invocation': {u'module_args': {u'unique_name': True, u'virtio': True, u'region_id': u'nyc1', u'size_id': u'512mb', u'api_token': u'VALUE_SPECIFIED_IN_NO_LOG_PARAMETER', u'backups_enabled': False, u'user_data': None, u'id': None, u'image_id': u'ubuntu-14-04-x64', u'state': u'present', u'wait_timeout': 500, u'command': u'droplet', u'ssh_key_ids': [u'**********'], u'ipv6': False, u'private_networking': False, u'wait': True, u'ssh_pub_key': None, u'name': u'bfh01'}}}) => {
    "item": {
        "changed": true, 
        "droplet": {
            "backup_ids": [], 
            "created_at": "2017-08-01T10:09:41Z", 
            "disk": 20, 
            "features": [], 
            "id": 56978525, 
            "image": {
                "created_at": "2017-06-21T13:17:21Z", 
                "distribution": "Ubuntu", 
                "id": 25758610, 
                "min_disk_size": 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"
            }, 
            "ip_address": "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": []
            }, 
            "next_backup_window": null, 
            "region": {
                "available": true, 
                "features": [
                    "private_networking", 
                    "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, 
                "price_hourly": 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
            }, 
            "size_slug": "512mb", 
            "snapshot_ids": [], 
            "status": "active", 
            "tags": [], 
            "vcpus": 1, 
            "volume_ids": []
        }, 
        "invocation": {
            "module_args": {
                "api_token": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
                "backups_enabled": false, 
                "command": "droplet", 
                "id": null, 
                "image_id": "ubuntu-14-04-x64", 
                "ipv6": false, 
                "name": "bfh01", 
                "private_networking": false, 
                "region_id": "nyc1", 
                "size_id": "512mb", 
                "ssh_key_ids": [
                    "**********"
                ], 
                "ssh_pub_key": null, 
                "state": "present", 
                "unique_name": true, 
                "user_data": 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   
1 Answer

As you've seen, Ansible is overly verbose when looping over a hashed variable and will print out its entire contents. (See this GitHub issue for other examples and some general work-a-rounds.)

How I've gotten around this in the past is to use Jinja's map filter on the results to just grab the value I want. For example, to just have the IP address:

    - debug: 
        msg="IP is {{ item }}"
      with_items:
       - "{{ droplet.results|map(attribute='droplet.ip_address')|list }}"

This produces the output:

TASK [debug] *****************************************************************************************
ok: [localhost] => (item=XXX.XXX.XX.XXX) => {
    "changed": false, 
    "item": "XXX.XXX.XX.XXX", 
    "msg": "IP is XXX.XXX.XX.XXX"
}
ok: [localhost] => (item=XXX.XXX.XX.XXX) => {
    "changed": false, 
    "item": "XXX.XXX.XX.XXX", 
    "msg": "IP is XXX.XXX.XX.XXX"
}

Which is still a bit more verbose than desired, but much more reasonable to deal with.

Have another answer? Share your knowledge.