如何将 ansible 调试输出读取为 Json 文件中的变量..以下是示例

如何将 ansible 调试输出读取为 Json 文件中的变量..以下是示例

这是我的调试输出..

register: create
    - debug: 
        msg: "{{ create.json.info.0.value }}"
ok: [localhost] => {
    "msg": "8308a020-5c9d-4936-8f1a-40f408d3a085"
}

现在我需要将输出添加为 JSON 模板的输入。以下是我的模板...

{
"networkDomainId": "{{create.json.info.0.value}}"
"name": "Sandy_Smoke_VLAN",
"description": "For hosting our Smokeping_test Cloud Servers"
"privateIpv4NetworkAddress": "10.10.0.0"
"privateIpv4PrefixSize": 24
"attachedVlan": { "gatewayAddressing": "HIGH" }
}

但上述模板不起作用。

答案1

下面是一个例子。该剧

- hosts: localhost                                                                           
  vars:                                                                                      
    create:                                                                                  
      json:                                                                                  
        info:                                                                                
          - {key: 'key_1', value: '8308a020-5c9d-4936-8f1a-40f408d3a085'}                    
  tasks:                                                                                     
    - debug:                                                                                 
        msg: "{{ create.json.info.0.value }}"                                                
    - template:                                                                              
        src: test.j2                                                                         
        dest: /scratch/tmp/test

使用模板 test.j2

> cat test.j2 
{
"networkDomainId": "{{create.json.info.0.value}}"
}

给出

PLAY [localhost] 
TASK [debug] 
ok: [localhost] => {
"msg": "8308a020-5c9d-4936-8f1a-40f408d3a085"
}
TASK [template]
changed: [localhost]
PLAY RECAP 
localhost                  : ok=2    changed=1    unreachable=0    failed=0

> cat /scratch/tmp/test 
{
"networkDomainId": "8308a020-5c9d-4936-8f1a-40f408d3a085"
}

相关内容