Ansible任务的输出

Output of Ansibble task

我正在使用命令 hcloud 在 Hetzner 中创建云服务器。我得到这样的输出:

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"
}

我尝试与 "ipv4_address": "11.111.111.111","root_password": "DFLDJFLDFDLFKJDLFKJ" 保持一致,但是当我使用任务时:

---

- 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

我收到错误:

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

能否请您帮忙,我如何获得 IP 行和密码行,以便在下一个任务中使用它们(例如通过电子邮件发送)。谢谢!

你注册了循环的内容,所以你的结果是一个列表(results):

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

结果:这里你只声明了一个服务器,所以列表结果中只有一个项目

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

如果你想限制记录的输出,你可以添加loop_control参数来循环,参数标签:

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

如果你想用标签甚至空字符串,你可以再发表评论:

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