I've got a .json file filled with hundreds of configuration values for an Axis network camera. The contents look like this:
{
"Name": "HTTPS.Port",
"Value": "443"
},
{
"Name": "Image.DateFormat",
"Value": "YYYY-MM-DD"
},
{
"Name": "Image.MaxViewers",
"Value": "20"
},
The Axis API, called Vapix, only provides an update function that updates a single value, so I have to circle through the values and trigger a new API call with every iteration:
name: update parameters
local_action:
module: uri
user: x
password: y
url: "{{ axis_snmp_role.server_url }}?action=update&{{ item }}"
with_items:
- "SNMP.V2c=yes"
- "SNMP.Enabled=yes"
- "ImageSource.I0.Sensor.ExposureValue=100"
Now the above example requires me to hardcode hundreds of config values into the loop. Is there a way to tell Ansible to go through the camera configuration json, update every value via a new api call and stop when there are no more values left within the json file?
Given the configuration's values is a list. For example
shell> cat data.yml
config: [
{"Name": "HTTPS.Port", "Value": "443"},
{"Name": "Image.DateFormat", "Value": "YYYY-MM-DD"},
{"Name": "Image.MaxViewers", "Value": "20"}]
The play
- hosts: localhost
tasks:
- include_vars:
file: data.json
- debug:
msg: "?action=update&{{ item.Name }}={{ item.Value }}"
loop: "{{ config }}"
gives
ok: [localhost] => (item={u'Name': u'HTTPS.Port', u'Value': u'443'}) => {
"msg": "?action=update&HTTPS.Port=443"
}
ok: [localhost] => (item={u'Name': u'Image.DateFormat', u'Value': u'YYYY-MM-DD'}) => {
"msg": "?action=update&Image.DateFormat=YYYY-MM-DD"
}
ok: [localhost] => (item={u'Name': u'Image.MaxViewers', u'Value': u'20'}) => {
"msg": "?action=update&Image.MaxViewers=20"
}
If this is what you want loop the uri module. For example
- local_action:
module: uri
user: x
password: y
url: "{{ axis_snmp_role.server_url }}?action=update&{{ item.Name }}={{ item.Value }}"
loop: "{{ config }}"