ansible 将 json 解析为 include_tasks 循环

ansible parsing json into include_tasks loop

我有以下名为 cust.json 的 json 文件:

{
 "customer":{
        "CUST1":{
                "zone":"ZONE1",
                "site":"ASIA"
        },
       "CUST2":{
                "zone":"ZONE2",
                "site":"EUROPE"
        }
    }
}

我在 main.yml 中使用这个 json 文件来获取客户列表(CUST1 和 CUST2)。

main.yml:

- name: Include the vars
  include_vars:
    file: "{{ playbook_dir }}/../default_vars/cust.json"
    name: "cust_json"

- name: Generate customer config 
  include_tasks: create_config.yml
  loop: "{{ cust_json.customer }}"

我希望循环基本上将每个客户的代码(例如 CUST1)传递给 create_config.yml,这样就会发生如下情况:

create_config.yml:

- name: Create customer config 
  block:
    - name: create temporary file for customer
      tempfile:
        path: "/tmp"
        state: file
        prefix: "my customerconfig_{{ item }}."
        suffix: ".tgz"
      register: tempfile

    - name: Setup other things
      include_tasks: "othercustconfigs.yml"


这将导致:

  1. 正在生成以下文件:/tmp/mycustomerconfig_CUST1/tmp/mycustomerconfig_CUST2
  2. 对于 CUST1 和 CUST2,othercustconfigs.yml 中的任务是 运行。

问题:

  1. 运行 ansible,此时失败:
TASK [myrole : Generate customer config ] ************************************************************************************************************************************************************

fatal: [127.0.0.1]: FAILED! => {
    "msg": "Invalid data passed to 'loop', it requires a list, got this instead: {u'CUST1': {u'site': u'ASIA', u'zone': u'ZONE1'}, u'CUST2': {u'site': u'EUROPE', u'zone': uZONE2'}}. Hint: If you passed a list/dict of just one element, try adding wantlist=True to your lookup invocation or use q/query instead of lookup."
}

如何循环 JSON 以便正确获取客户列表(CUST1 和 CUST2)? loop: "{{ cust_json.customer }}" 显然行不通。

  1. 如果我设法使上述工作正常,是否可以将循环的结果传递给下一个 include_tasks: "othercustconfigs.yml ?所以基本上,将循环项从 main.yml 传递到 config.yml,然后传递到 othercustconfigs.yml。这可能吗?

谢谢!! J

cust_json.customer 是一个散列表,每个客户都有一个键,而不是列表。

dict2items 过滤器可以将这个 hashmap 转换为一个元素列表,每个元素包含一个 keyvalue 属性,例如:

- key: "CUST1"
  value:
    zone: "ZONE1"
    site: "ASIA"
- key: "CUST2"
  value:
    zone: "ZONE2"
    site: "EUROPE"

考虑到这一点,您可以将您的包含转换为以下内容:

- name: Generate customer config 
  include_tasks: create_config.yml
  loop: "{{ cust_json.customer | dict2items }}"

以及您所包含文件中的相关任务:

    - name: create temporary file for customer
      tempfile:
        path: "/tmp"
        state: file
        prefix: "my customerconfig_{{ item.key }}."
        suffix: ".tgz"
      register: tempfile

当然你可以调整所有这些以在需要的地方使用 value 元素,例如item.value.site

您可以查看以下文档以获取深入信息和替代解决方案: