Reputation: 3
I'm trying to write an ansible script that configures the same id on multiple collections on multiple dbs all on the same mongo server. How do I go about running with_items $n number of times?
The collections are known and the MongoDbs have the same name configured
I tried initially a few approaches with with_items. I think I should be able to use with_nested but it doesn't seem to be liking how I formatted my $n into a list.
- set_fact:
mongo_list: "{{range(1, {{ number_devsims }} + 1) | list}}"
- name: Updating simid
command: "mongo \"{{ mongo_db_name }}-{{ item[1] }}\" --eval 'db.{{ item[0] }}.update({}, {$set : {simid : \"{{ devsim_eng_simid }}\"}})'"
with_nested:
- ['engines', 'servers', 'simulations']
- "{{ mongo_list}}"
Ideally I'd like to be able to update all collections for each mongo. So for example
- set_fact:
mongo_list: [1, 2]
TASK [devsim_data : Updating simid] *********************************************************************************
Wednesday 21 August 2019 10:42:51 -0700 (0:00:07.602) 0:00:37.450 ******
[WARNING]: The loop variable 'item' is already in use. You should set the `loop_var` value in the `loop_control`
option for the task to something else to avoid variable collisions and unexpected behavior.
changed: [eco] => (item=['engines', 1])
changed: [eco] => (item=['engines', 2])
changed: [eco] => (item=['servers', 1])
changed: [eco] => (item=['servers', 2])
changed: [eco] => (item=['simulations', 1])
changed: [eco] => (item=['simulations', 2])
The error I seem to be getting with my set_fact is
TASK [devsim_data : set_fact] ***************************************************************************************
Wednesday 21 August 2019 10:50:40 -0700 (0:00:00.066) 0:00:17.668 ******
fatal: [eco]: FAILED! => {"msg": "template error while templating string: expected token ':', got '}'. String: {{range(1, {{ number_devsims }} + 1) | list}}"}
Upvotes: 0
Views: 1444
Reputation: 312630
The problem is that you're trying to nest {{
markers inside another Jinja expression:
mongo_list: "{{range(1, {{ number_devsims }} + 1) | list}}"
Since you're already inside a Jinja context, the nested {{...}}
is unnecessary; this should just be:
mongo_list: "{{range(1, number_devsims + 1) | list}}"
With that fix, I think everything else should pretty much work. I wrapped your command
task in a debug
task to test it, like this:
---
- hosts: localhost
gather_facts: false
vars:
number_devsims: 2
mongo_db_name: example
devsim_eng_simid: example-eng-simid
tasks:
- set_fact:
mongo_list: "{{range(1, number_devsims + 1) | list}}"
- name: Updating simid
debug:
msg:
command: "mongo \"{{ mongo_db_name }}-{{ item[1] }}\" --eval 'db.{{ item[0] }}.update({}, {$set : {simid : \"{{ devsim_eng_simid }}\"}})'"
with_nested:
- ['engines', 'servers', 'simulations']
- "{{ mongo_list}}"
And this results in:
PLAY [localhost] *****************************************************************************************************************************************************************************************************************************************************************************
TASK [set_fact] ******************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Updating simid] ************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => (item=['engines', 1]) => {
"msg": {
"command": "mongo \"example-1\" --eval 'db.engines.update({}, {$set : {simid : \"example-eng-simid\"}})'"
}
}
ok: [localhost] => (item=['engines', 2]) => {
"msg": {
"command": "mongo \"example-2\" --eval 'db.engines.update({}, {$set : {simid : \"example-eng-simid\"}})'"
}
}
ok: [localhost] => (item=['servers', 1]) => {
"msg": {
"command": "mongo \"example-1\" --eval 'db.servers.update({}, {$set : {simid : \"example-eng-simid\"}})'"
}
}
ok: [localhost] => (item=['servers', 2]) => {
"msg": {
"command": "mongo \"example-2\" --eval 'db.servers.update({}, {$set : {simid : \"example-eng-simid\"}})'"
}
}
ok: [localhost] => (item=['simulations', 1]) => {
"msg": {
"command": "mongo \"example-1\" --eval 'db.simulations.update({}, {$set : {simid : \"example-eng-simid\"}})'"
}
}
ok: [localhost] => (item=['simulations', 2]) => {
"msg": {
"command": "mongo \"example-2\" --eval 'db.simulations.update({}, {$set : {simid : \"example-eng-simid\"}})'"
}
}
PLAY RECAP ***********************************************************************************************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Upvotes: 1