Hi Everyone,
Hear is some part of my ansible code, I want to take contactId from Json array when contactAccount is someemail2@somedomain.com, but my script’s this part “{{array.{{item}}.contactId}}” doesn’t work. Could anyone help me to solve this problem?
- set_fact: msg: “{{array.{{item}}.contactId}}” with_sequence: start=0 end={{ array | length-1 }} when: array.{{item}}.contactAccount == ‘someemail2@somedomain.com’
Hear is array example:
[{“contactId”: 1234,“contactAccount”: “someemail1@somedomain.com”
},{ “contactId”: 2345, “contactAccount”: “someemail2@somedomain.com”
},
…
{ “contactId”: 6567, “contactAccount”: “someemail100@somedomain.com”
}
]
Maybe the find filter does what you are looking for?
array.{{item}}.contactaccount.find('someemail') == 1
Untested!
This is not in the filter documentation, I found it in conditionals:
https://docs.ansible.com/ansible/playbooks_filters.html
https://docs.ansible.com/ansible/playbooks_conditionals.html
Johannes
Thank you for your reply, but I can check contactAccount value with my when condition, I want to take contactId according on contactAccount
Sorry, misunderstood your problem.
This is wrong syntax, double curly braces are not allowed inside
double curly braces:
msg="{{array.{{item}}.contactId}}...
Try this
msg= "{{array[item]['contactId'] }}...
(contactId is taken as the string contactID, while item is replaced by
its value. I have no clue how to put that in dot notation...)
Johannes
Thank you Johannes,
i put msg: “{{array[item][‘contactId’]}}”
but i got following error:
fatal: [10.137.25.64]: FAILED! => {“failed”: true, “msg”: “the field ‘args’ has an invalid value, which appears to include a variable that is undefined. The error was: ‘list object’ has no attribute u’3’\n\nThe error appears to have been in ‘/etc/ansible/monitoring/roles/set_global_vars_and_add_tab/tasks/main.yml’: line 53, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- set_fact:\n ^ here\n”}
Obviously arrays do not like to be treated like a list...
https://docs.ansible.com/ansible/playbooks_variables.html#accessing-complex-variable-data
Does this at least spit out something?
msg: "{{array[ [ item ] ]}}"
msg: "{{array[ [ item ] ]['contactId'] }}"
I guess the array needs the square brackets, and AFAIK you get the
value of a variable by putting it in square brackets, when you are
inside the double curly ones...
Otherwise separating with commas might be an alternative.
Johannes
I put msg: “{{array[ [ item ] ][‘contactId’] }}”
But again got error:
fatal: [10.137.25.64]: FAILED! => {“failed”: true, “msg”: “the field ‘args’ has an invalid value, which appears to include a variable that is undefined. The error was: list object has no element [u’3’]\n\nThe error appears to have been in ‘/etc/ansible/monitoring/roles/set_global_vars_and_add_tab/tasks/main.yml’: line 53, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- set_fact:\n ^ here\n”}
I think script doesn’t recognizing “item” as a variable beacuse when I am changing msg: “{{array[ 4 ][‘contactId’]}}” it works and I got 4th element’s contactId
u'3' means that the value is a string (unicode).
Try with:
{{ array[int(item)]['contactId'] }}
Again error((((
msg: “{{ array[int(item)][‘contactId’] }}”
fatal: [10.137.25.64]: FAILED! => {“failed”: true, “msg”: “the field ‘args’ has an invalid value, which appears to include a variable that is undefined. The error was: ‘int’ is undefined\n\nThe error appears to have been in ‘/etc/ansible/monitoring/roles/set_global_vars_and_add_tab/tasks/main.yml’: line 53, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- set_fact:\n ^ here\n”}
I would say that again the brackets are missing:
msg: "{{ array[ [ int(item)] ]['contactId'] }}"
Johannes
msg: "{{ array[ [ int(item)] ]['contactId'] }}"
Error:
fatal: [10.137.25.64]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'int' is undefined\n\nThe error appears to have been in '/etc/ansible/monitoring/roles/set_global_vars_and_add_tab/tasks/main.yml': line 53, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- set_fact:\n ^ here\n"}
Sorry, I posted the solution without trying it.
It's not int(item), but item | int(). So, this works for me:
msg: "{{ array[item | int()]['contactId'] }}"
Thank you very very much. It works thank you for help. Great Job!!!
You're welcome.
I just figured out we were scratching our right ear with left hand.
The same result can be accomplished by using
with_items: "{{ array }}"
Then you can access the attributes by item.contactId and item.contactAccount:
- set_fact:
msg: "{{ item.contactId }}"
with_items: "{{ array }}"
when: item.contactAccount == 'somee...@somedomain.com'
Cheers,
Marko
You are right, I tried and this works too, I think this is better solution!!! Thank you one more time!!!