安塞波。从 {{ item }} 循环中获取特定值

问题描述 投票:0回答:1

我正在尝试使用循环和项目,但我无法让它工作......也许你可以帮助我。

所以我想循环打印此输出的“内容”值,但我无法执行此操作。做

- name: "Verify started instances"
  uri:
    url: "http://localhost:808{{ item[-1] }}/url"
    method: GET
    return_content: yes
  register: status
  until: "status is search('Version')"
  retries: 3
  delay: 60
  ignore_errors: yes
  with_items: "{{ instances_running.stdout_lines }}"

- debug:
    msg: "{{ item }}"
  with_items: "{{ status }}"
  ignore_errors: yes

输出看起来像这样:

TASK [[AppServerAgent] verify started instances] **************************************************************************************************************************************
FAILED - RETRYING: [AppServerAgent] verify started instances (3 retries left).
FAILED - RETRYING: [AppServerAgent] verify started instances (2 retries left).
ok: [SERVER] => (item=inst0)
FAILED - RETRYING: [AppServerAgent] verify started instances (3 retries left).
FAILED - RETRYING: [AppServerAgent] verify started instances (2 retries left).
FAILED - RETRYING: [AppServerAgent] verify started instances (1 retries left).
failed: [SERVER] (item=instnull) => {"ansible_loop_var": "item", "attempts": 3, "changed": false, "item": "instnull", "msg": "invalid literal for int() with base 10: '808l'", "status": -1, "url": "http://localhost:808l/url"}
...ignoring

TASK [debug] **************************************************************************************************************************************************************************
ok: [SERVER] => {
    "msg": {
        "changed": false,
        "failed": true,
        "msg": "All items completed",
        "results": [
            {
                "ansible_loop_var": "item",
                "attempts": 3,
                "changed": false,
                "connection": "close",
                "content": "Version:      2-SNAPSHOT\nBuild number: 1\nBuild id:     2020-11-325_09-03-20\n",
                "content_length": "281",
                "cookies": {},
                "cookies_string": "",
                "date": "",
                "elapsed": 0,
                "failed": false,
                "invocation": {
                    "module_args": {
                        "attributes": null,
                        "backup": null,
                        "body": null,
                        "body_format": "raw",
                        "client_cert": null,
                        "client_key": null,
                        "content": null,
                        "creates": null,
                        "delimiter": null,
                        "dest": null,
                        "directory_mode": null,
                        "follow": false,
                        "follow_redirects": "safe",
                        "force": false,
                        "force_basic_auth": false,
                        "group": null,
                        "headers": {},
                        "http_agent": "ansible-httpget",
                        "method": "GET",
                        "mode": null,
                        "owner": null,
                        "regexp": null,
                        "remote_src": null,
                        "removes": null,
                        "return_content": true,
                        "selevel": null,
                        "serole": null,
                        "setype": null,
                        "seuser": null,
                        "src": null,
                        "status_code": [
                            200
                        ],
                        "timeout": 30,
                        "unix_socket": null,
                        "unsafe_writes": null,
                        "url": "http://localhost:8080/url",
                        "url_password": null,
                        "url_username": null,
                        "use_proxy": true,
                        "validate_certs": true
                    }
                },
                "item": "inst0",
                "msg": "OK (281 bytes)",
                "redirected": false,
                "status": 200,
                "url": "http://localhost:8080/url"
            },
            {
                "ansible_loop_var": "item",
                "attempts": 3,
                "changed": false,
                "failed": true,
                "invocation": {
                    "module_args": {
                        "attributes": null,
                        "backup": null,
                        "body": null,
                        "body_format": "raw",
                        "client_cert": null,
                        "client_key": null,
                        "content": null,
                        "creates": null,
                        "delimiter": null,
                        "dest": null,
                        "directory_mode": null,
                        "follow": false,
                        "follow_redirects": "safe",
                        "force": false,
                        "force_basic_auth": false,
                        "group": null,
                        "headers": {},
                        "http_agent": "ansible-httpget",
                        "method": "GET",
                        "mode": null,
                        "owner": null,
                        "regexp": null,
                        "remote_src": null,
                        "removes": null,
                        "return_content": true,
                        "selevel": null,
                        "serole": null,
                        "setype": null,
                        "seuser": null,
                        "src": null,
                        "status_code": [
                            200
                        ],
                        "timeout": 30,
                        "unix_socket": null,
                        "unsafe_writes": null,
                        "url": "http://localhost:808l/url",
                        "url_password": null,
                        "url_username": null,
                        "use_proxy": true,
                        "validate_certs": true
                    }
                },
                "item": "instnull",
                "msg": "invalid literal for int() with base 10: '808l'",
                "status": -1,
                "url": "http://localhost:808l/url"
            }
        ]
    }
}

我试图使用这样的东西:

- debug:
    msg: "{{ item.content.split('\n') }}"
  with_items: "{{ status }}"
  ignore_errors: yes

但是输出看起来像这样:

TASK [debug] **************************************************************************************************************************************************************************
fatal: [SERVER]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeBytes object' has no attribute 'content'\n\nThe error appears to be in '/deploy.yaml': line 176, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - debug:\n      ^ here\n"}

我该怎么做?

或者也许还有一种方法可以使用其他命令而不是

until: "status is search('Version')"
来查找
content
字段并在名称块中打印输出而不是调试任务?

谢谢!

linux loops ansible
1个回答
-1
投票

尝试“{{ status.results.content }}”

© www.soinside.com 2019 - 2024. All rights reserved.