从输出中提取值并发送到下一个任务

Extract value from output and send to next task

我正在尝试在 Ansible Tower 中定义一个模板,我想在其中提取 Kafka Broker 中活动控制器的 ID,然后在另一个将执行滚动重启的模板/任务中使用此值,但会确保主动控制器最后启动

当我运行这个Ansible任务

- name: Find active controller
  shell: '/bin/zookeeper-shell 192.168.129.227 get /controller'
  register: resultAC

我得到以下结果。我想提取 brokerid 并将值 2 分配给一个变量,该变量可以在同一模板中的不同任务中使用,或者当模板是 a 的一部分时将其传递给另一个模板工作流定义。

我试过使用 resultAC.stdout_lines[5].brokerid 但那不起作用。

resultAC的结构:

{
    "resultAC": {
        "stderr_lines": [],
        "changed": true,
        "end": "2020-08-19 07:36:01.950347",
        "stdout": "Connecting to 192.168.129.227\n\nWATCHER::\n\nWatchedEvent state:SyncConnected type:None path:null\n{\"version\":1,\"brokerid\":2,\"timestamp\":\"1597241391146\"}",
        "cmd": "/bin/zookeeper-shell 192.168.129.227 get /controller",
        "failed": false,
        "delta": "0:00:02.843972",
        "stderr": "",
        "rc": 0,
        "stdout_lines": [
            "Connecting to 192.168.129.227",
            "",
            "WATCHER::",
            "",
            "WatchedEvent state:SyncConnected type:None path:null",
            "{\"version\":1,\"brokerid\":2,\"timestamp\":\"1597241391146\"}"
        ],
        "start": "2020-08-19 07:35:59.106375"
    },
    "_ansible_verbose_always": true,
    "_ansible_no_log": false,
    "changed": false
}

因为您的 JSON 只是字符串列表的一部分,所以它不会被解析或视为 JSON。
您必须使用 Ansible 过滤器 from_json 才能将其解析回字典。

鉴于剧本:

- hosts: all
  gather_facts: no
  vars: 
    resultAC:
      stdout_lines: 
        - "Connecting to 192.168.129.227"
        - ""
        - "WATCHER::"
        - ""
        - "WatchedEvent state:SyncConnected type:None path:null"
        - "{\"version\":1,\"brokerid\":2,\"timestamp\":\"1597241391146\"}"
      
  tasks:
    - debug: 
        msg: "{{ (resultAC.stdout_lines[5] | from_json).brokerid }}"

回顾一下:

PLAY [all] *************************************************************************************************************************************************************

TASK [debug] ***********************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "2"
}

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

更进一步,也许我会 select and match stdout_lines 列表中的 JSON,以防它不总是在第六行:

- hosts: all
  gather_facts: no
  vars: 
    resultAC:
      stdout_lines: 
        - "Connecting to 192.168.129.227"
        - ""
        - "WATCHER::"
        - ""
        - "WatchedEvent state:SyncConnected type:None path:null"
        - "{\"version\":1,\"brokerid\":2,\"timestamp\":\"1597241391146\"}"
      
  tasks:
    - debug: 
        msg: "{{ (resultAC.stdout_lines | select('match','{.*\"brokerid\":.*}') | first | from_json).brokerid }}"