Below is my JSON file:
{
"changed": false,
"invocation": {
"module_args": {
"cucm_ip": "1.1.1.1",
"cucm_option": "DBREPLICATION",
"cucm_pwd": "123",
"cucm_user": "123"
}
},
"meta": [
{
"DB/RPC/DBMON?": "Y/Y/Y",
"IP_ADDRESS": "1.1.1.1",
"PING (msec)": "0.029",
"REPL.QUEUE": "0",
"REPLICATION SETUP (RTMT) & Details": "(2) Setup Completed",
"Replication Group ID": "(g_2)",
"SERVER_NAME": "bopub1"
},
{
"DB/RPC/DBMON?": "Y/Y/Y",
"IP_ADDRESS": "1.1.1.2",
"PING (msec)": "0.175",
"REPL.QUEUE": "0",
"REPLICATION SETUP (RTMT) & Details": "(2) Setup Completed",
"Replication Group ID": "(g_3)",
"SERVER_NAME": "bosub1"
},
{
"DB/RPC/DBMON?": "Y/Y/Y",
"IP_ADDRESS": "1.1.1.3",
"PING (msec)": "0.293",
"REPL.QUEUE": "0",
"REPLICATION SETUP (RTMT) & Details": "(2) Setup Completed",
"Replication Group ID": "(g_6)",
"SERVER_NAME": "bosub2"
}
]
}
I need to get the value from key 'REPLICATION SETUP (RTMT) & Details', how to achieve this..
I always got fatal error '"msg": "An unhandled exception occurred while templating '{{ item.'REPLICATION SETUP (RTMT) & Details' }}'. Error was a <class 'ansible.errors.AnsibleError'>, original message: template error while templating string: expected name or number. String: {{ item.'REPLICATION SETUP (RTMT) & Details' }}"'..
Q: "Get the value from key 'REPLICATION SETUP (RTMT) & Details'"
A: Read the JSON from the file into the dictionary
- include_vars:
file: data.json
name: data
gives
data: changed: false invocation: module_args: cucm_ip: 1.1.1.1 cucm_option: DBREPLICATION cucm_pwd: '123' cucm_user: '123' meta: - DB/RPC/DBMON?: Y/Y/Y IP_ADDRESS: 1.1.1.1 PING (msec): '0.029' REPL.QUEUE: '0' REPLICATION SETUP (RTMT) & Details: (2) Setup Completed Replication Group ID: (g_2) SERVER_NAME: bopub1 - DB/RPC/DBMON?: Y/Y/Y IP_ADDRESS: 1.1.1.2 PING (msec): '0.175' REPL.QUEUE: '0' REPLICATION SETUP (RTMT) & Details: (2) Setup Completed Replication Group ID: (g_3) SERVER_NAME: bosub1 - DB/RPC/DBMON?: Y/Y/Y IP_ADDRESS: 1.1.1.3 PING (msec): '0.293' REPL.QUEUE: '0' REPLICATION SETUP (RTMT) & Details: (2) Setup Completed Replication Group ID: (g_6) SERVER_NAME: bosub2
Put the key 'REPLICATION SETUP (RTMT) & Details' into a variable. You don't have to escape special characters if you use single quotes. Declare the variables
rsd: "{{ data.meta|map(attribute=my_key)|list }}"
my_key: 'REPLICATION SETUP (RTMT) & Details'
gives
rsd:
- (2) Setup Completed
- (2) Setup Completed
- (2) Setup Completed
Example of a complete playbook for testing
- hosts: localhost vars: rsd: "{{ data.meta|map(attribute=my_key)|list }}" my_key: 'REPLICATION SETUP (RTMT) & Details' tasks: - include_vars: file: data.json name: data - debug: var: data - debug: var: rsd
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.