在Ansible中,如何在循环中访问注册变量的特定元素?

2024-04-29 04:44:47 发布

您现在位置:Python中文网/ 问答频道 /正文

前几天我发现当寄存器变量在循环中使用时

- name: Generate pw's for users
  command: "/some_path/generate_hashed_pw.sh -u {{ item }}"
  register: hashed_pws
  with_items:
  - joe
  - sally
  - john

散列pws寄存器是一个散列,它包含results[]键,这是一个散列数组,如下所示

{
  "hashed_pws" : {
    "changed" : true,
    "some_other_key" : "some_other_value",
    "results" : [
      {
        "item" : "joe",
        "stdout": "joes_hashed_pw",
        "some_other_key" : "some_other_value"
      },
      {
        "item" : "sally",
        "stdout": "sallys_hashed_pw",
        "some_other_key" : "some_other_value"
      },
      {
        "item" : "john",
        "stdout": "johns_hashed_pw",
        "some_other_key" : "some_other_value"
      }
    ]
  }
}

那么直接访问的语法是什么,数组中每个散列元素中的“stdout”元素?换句话说,我想要:

- debug: msg="Sallys hashed pw is {{ hashed_pws.results[SOME_KEY_TO_DIRECTLY_GET_SALLYS_STDOUT_VALUE] }}"

这可能是一个python问题,也可能是一个可以回答的问题。


Tags: keyvaluestdoutsome数组itemjohnresults