I'm trying to build an array/list of JSON objects in Ansible. My playbook is called by specifying --extra-vars "userids=123456,654321"
.
I then try to build the list with the following. "TBD" is just a place-holder. I will fill those values with data retrieved from SQL queries later in the playbook.
- name: Initiate User List
set_fact:
all_users: []
- name: Add new JSON Objects to List
set_fact:
all_users: "{{ all_users+ [{ 'UserID': '{{ item }}', 'UserType': 'TBD', 'UserName': 'TBD' }] }}"
loop: "{{ userids.split(',') }}"
- name: Debug
debug:
var: all_users
The list sort of builds, but it is skipping the first item in the list. This is the resulting output:
TASK [Initiate User List] *************************************************************************************************************************************************************
ok: [localhost]
TASK [Add new JSON Objects to List] ******************************************************************************************************************************************************
ok: [localhost] => (item=123456)
ok: [localhost] => (item=654321)
TASK [Debug] *****************************************************************************************************************************************************************************
ok: [localhost] => {
"all_users": [
{
"UserID": "654321",
"UserType": "TBD",
"UserName": "TBD"
},
{
"UserID": "{{ item }}",
"UserType": "TBD",
"UserName": "TBD"
}
]
}
I'm not sure what I'm missing. I tried doing a "set_fact" before the looping task to perform the split() first, but that didn't help. I also tried "with_items" instead of loop, no luck