Oleg
Oleg

Reputation: 181

Output of Ansibble task

I am using command hcloud to create cloud server in Hetzner. I get an output like this:

changed: [localhost] => (item={'name': 'TEST-VARIABLES', 'server_type': 'cx11', 'os_image': 'ubuntu-20.04', 'server_labels': 'Name=test-server', 'server_location': 'hel1'}) => {
    "ansible_loop_var": "item",
    "changed": true,
    "hcloud_server": {
        "backup_window": "None",
        "datacenter": "hel1-dc2",
        "delete_protection": false,
        "id": "19461514",
        "image": "ubuntu-20.04",
        "ipv4_address": "11.111.111.111",
        "ipv6": "1a71:7f9:c011:0b09::/64",
        "labels": {
            "Name": "test-server"
        },
        "location": "hel1",
        "name": "TEST-VARIABLES",
        "placement_group": null,
        "rebuild_protection": false,
        "rescue_enabled": false,
        "server_type": "cx11",
        "status": "running"
    },
    "invocation": {
        "module_args": {
            "allow_deprecated_image": false,
            "api_token": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "backups": null,
            "datacenter": null,
            "delete_protection": null,
            "endpoint": "https://api.SERVER.cloud/v1",
            "firewalls": null,
            "force": false,
            "force_upgrade": false,
            "id": null,
            "image": "ubuntu-20.04",
            "labels": {
                "Name": "test-server"
            },
            "location": "hel1",
            "name": "TEST-VARIABLES",
            "placement_group": null,
            "rebuild_protection": null,
            "rescue_mode": null,
            "server_type": "cx11",
            "ssh_keys": null,
            "state": "present",
            "upgrade_disk": false,
            "user_data": null,
            "volumes": null
        }
    },
    "item": {
        "name": "TEST-VARIABLES",
        "os_image": "ubuntu-20.04",
        "server_labels": "Name=test-server",
        "server_location": "hel1",
        "server_type": "cx11"
    },
    "root_password": "DFLDJFLDFDLFKJDLFKJ"
}

I try to get line with "ipv4_address": "11.111.111.111", and "root_password": "DFLDJFLDFDLFKJDLFKJ", but when I use task:

---

- name: Create a basic server
  hcloud_server:
    api_token: "{{ token }}"
    name: "{{ item.name }}"
    server_type: "{{ item.server_type }}"
    image: "{{ item.os_image }}"
    labels: "{{ item.server_labels }}"
    location: "{{ item.server_location }}"
    state: present
  register: server_info
  with_items: "{{ server }}"

- name: Here IP
  debug:
    var: server_info.root_password

I got error:

TASK [/etc/ansible/roles/CREATE-server : Here IP.] *********************************************************************************************************
ok: [localhost] => {
    "server_info.root_password": "VARIABLE IS NOT DEFINED!"
}

Could you please help, how I can get IP line and password line, to use them in the next task (for example to send via email). Thank you!

Upvotes: 1

Views: 117

Answers (1)

Frenchy
Frenchy

Reputation: 17007

you register the content of loop, so your result is a list (results):

    - name: display
      debug:
        msg: "ip: {{ item.hcloud_server.ipv4_address }} has password: {{ item.root_password }}" 
      loop: "{{ server_info.results }}" 

result: here you have just one server declared, so just one item in the list results

"msg": "ip: 11.111.111.111 has password: DFDFDFDFDFDFDFDF"

if you want to limit the output of record, you could add loop_control parameter to loop with argument label:

loop: "{{ server_info.results }}"
loop_control:
  label: "{{ item.hcloud_server.ipv4_address }}"

you could put another comment if you want with label or even empty string:

loop: "{{ server_info.results }}"
loop_control:
  label: "result"

Upvotes: 2

Related Questions