簡體   English   中英

Ansible,無法將標准輸出保存為變量

[英]Ansible, can't save stdout as variable

遵循 ansible playbook 來檢索 ceph 池配置、頁數。 輸出存儲在 JSON 中,我無法僅存儲/檢索標准輸出值的問題。 想法是將 stdout 值用於另一個循環,這會創建具有完全相同頁數的池

- name: retrieve number of pg for all metric pools
    shell: ceph osd pool get {{ item.poolname }} pgp_num -f json
    loop: "{{ metricspools }}"
    register: poolpg

  - name: output poolpg
    set_fact:
      pgout: "{{ poolpg.stdout | from_json }}"
  - debug: var=pgout

給我錯誤

FAILED! => {"msg": "Unexpected templating type error occurred on ({{ poolpg.stdout | from_json }}): expected string or buffer"}

但是,如果我更改為以下

- name: output poolpg
  set_fact:
      pgout: "{{ poolpg }}"
- debug: var=pgout

然后將打印包含所有字符串(包括所需的標准輸出值)的整個 JSON 整個 JSON 輸出:

ok: [es-si-ce-dev-998.admin] => {
    "poolpg": {
        "changed": true, 
        "msg": "All items completed", 
        "results": [
            {
                "_ansible_ignore_errors": true, 
                "_ansible_item_result": true, 
                "_ansible_no_log": false, 
                "_ansible_parsed": true, 
                "changed": true, 
                "cmd": "ceph osd pool get staniX pgp_num -f json", 
                "delta": "0:00:00.251288", 
                "end": "2019-12-10 16:43:09.160898", 
                "failed": false, 
                "invocation": {
                    "module_args": {
                        "_raw_params": "ceph osd pool get staniX pgp_num -f json", 
                        "_uses_shell": true, 
                        "chdir": null, 
                        "creates": null, 
                        "executable": null, 
                        "removes": null, 
                        "stdin": null, 
                        "warn": true
                    }
                }, 
                "item": {
                    "poolname": "staniX", 
                    "poolnum": 3174
                }, 
                "rc": 0, 
                "start": "2019-12-10 16:43:08.909610", 
                "stderr": "", 
                "stderr_lines": [], 
                "stdout": "\n{\"pool\":\"staniX\",\"pool_id\":3174,\"pgp_num\":128}", 
                "stdout_lines": [
                    "", 
                    "{\"pool\":\"staniX\",\"pool_id\":3174,\"pgp_num\":128}"
                ]
            }, 
            {
                "_ansible_ignore_errors": true, 
                "_ansible_item_result": true, 
                "_ansible_no_log": false, 
                "_ansible_parsed": true, 
                "changed": true, 
                "cmd": "ceph osd pool get staniY pgp_num -f json", 
                "delta": "0:00:00.252257", 
                "end": "2019-12-10 16:43:10.753515", 
                "failed": false, 
                "invocation": {
                    "module_args": {
                        "_raw_params": "ceph osd pool get staniY pgp_num -f json", 
                        "_uses_shell": true, 
                        "chdir": null, 
                        "creates": null, 
                        "executable": null, 
                        "removes": null, 
                        "stdin": null, 
                        "warn": true
                    }
                }, 
                "item": {
                    "poolname": "staniY", 
                    "poolnum": 3175
                }, 
                "rc": 0, 
                "start": "2019-12-10 16:43:10.501258", 
                "stderr": "", 
                "stderr_lines": [], 
                "stdout": "\n{\"pool\":\"staniY\",\"pool_id\":3175,\"pgp_num\":64}", 
                "stdout_lines": [
                    "", 
                    "{\"pool\":\"staniY\",\"pool_id\":3175,\"pgp_num\":64}"
                ]
            }
        ]
    }
}

您在具有循環的任務上注冊poolpg 因此poolpg.stdout不存在,但poolpg.results[0].stdout存在。 您擁有的結果與您首先循環的元素一樣多。

要從結果中的所有標准輸出中創建 json 對象列表,請使用以下示例:

  1. 提取每個poolpg.resultsstdout屬性
  2. from_json過濾器映射到每個元素
  3. 將結果存儲在pgout並顯示結果
---
- name: Parse several results as json strings
  hosts: localhost
  gather_facts: false

  vars:
    # Your data reduced to a single json line.
    # Structured view available in your question
    poolpg: {"changed": true, "msg": "All items completed", "results": [{"_ansible_ignore_errors": true, "_ansible_item_result": true, "_ansible_no_log": false, "_ansible_parsed": true, "changed": true, "cmd": "ceph osd pool get staniX pgp_num -f json", "delta": "0:00:00.251288", "end": "2019-12-10 16:43:09.160898", "failed": false, "invocation": {"module_args": {"_raw_params": "ceph osd pool get staniX pgp_num -f json", "_uses_shell": true, "chdir": null, "creates": null, "executable": null, "removes": null, "stdin": null, "warn": true}}, "item": {"poolname": "staniX", "poolnum": 3174}, "rc": 0, "start": "2019-12-10 16:43:08.909610", "stderr": "", "stderr_lines": [], "stdout": "\n{\"pool\":\"staniX\",\"pool_id\":3174,\"pgp_num\":128}", "stdout_lines": ["", "{\"pool\":\"staniX\",\"pool_id\":3174,\"pgp_num\":128}"]}, {"_ansible_ignore_errors": true, "_ansible_item_result": true, "_ansible_no_log": false, "_ansible_parsed": true, "changed": true, "cmd": "ceph osd pool get staniY pgp_num -f json", "delta": "0:00:00.252257", "end": "2019-12-10 16:43:10.753515", "failed": false, "invocation": {"module_args": {"_raw_params": "ceph osd pool get staniY pgp_num -f json", "_uses_shell": true, "chdir": null, "creates": null, "executable": null, "removes": null, "stdin": null, "warn": true}}, "item": {"poolname": "staniY", "poolnum": 3175}, "rc": 0, "start": "2019-12-10 16:43:10.501258", "stderr": "", "stderr_lines": [], "stdout": "\n{\"pool\":\"staniY\",\"pool_id\":3175,\"pgp_num\":64}", "stdout_lines": ["", "{\"pool\":\"staniY\",\"pool_id\":3175,\"pgp_num\":64}"]}]}

  tasks:
    - name: Read each stdout as json
      set_fact:
        pgout: "{{ poolpg.results | map(attribute='stdout') | map('from_json') | list }}"

    - name: Show result
      debug:
        var: pgout

這使:

PLAY [Parse several results as json strings] ****************************************************************************************************************************************************************************************************************************

TASK [Read each stdout as json] *****************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Show result] ******************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "pgout": [
        {
            "pgp_num": 128,
            "pool": "staniX",
            "pool_id": 3174
        },
        {
            "pgp_num": 64,
            "pool": "staniY",
            "pool_id": 3175
        }
    ]
}

PLAY RECAP **************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM