首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安赛布尔任务的输出

安赛布尔任务的输出
EN

Stack Overflow用户
提问于 2022-04-11 07:22:08
回答 1查看 59关注 0票数 1

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

代码语言:javascript
复制
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",但是当我使用任务时:

代码语言:javascript
复制
---

- 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

我有错误:

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-11 08:32:15

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

代码语言:javascript
复制
    - name: display
      debug:
        msg: "ip: {{ item.hcloud_server.ipv4_address }} has password: {{ item.root_password }}" 
      loop: "{{ server_info.results }}" 

结果:这里您只声明了一台服务器,所以列表中只有一项结果。

代码语言:javascript
复制
"msg": "ip: 11.111.111.111 has password: DFDFDFDFDFDFDFDF"

如果要限制记录的输出,可以添加loop_control参数以循环使用参数标签:

代码语言:javascript
复制
loop: "{{ server_info.results }}"
loop_control:
  label: "{{ item.hcloud_server.ipv4_address }}"

如果您想使用label或甚至空字符串,可以添加另一个注释:

代码语言:javascript
复制
loop: "{{ server_info.results }}"
loop_control:
  label: "result"
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71824024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档