我正在尝试编写一个ansible脚本,该脚本在同一mongo服务器上的多个数据库上的多个集合上配置相同的id。如何运行with_items $ n次?
集合是已知的,并且MongoDb具有相同的配置名称
我最初使用with_items尝试了几种方法。我认为我应该能够使用with_nested,但似乎不喜欢我如何将$ n格式化为列表。
- 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}}"
理想情况下,我希望能够为每个mongo更新所有集合。例如
- 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])
我的set_fact出现的错误是
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}}"}
答案 0 :(得分:0)
问题是您试图将{{
标记嵌套在另一个Jinja表达式内:
mongo_list: "{{range(1, {{ number_devsims }} + 1) | list}}"
由于您已经在Jinja上下文中,所以不需要嵌套{{...}}
;这应该是:
mongo_list: "{{range(1, number_devsims + 1) | list}}"
有了这个修复程序,我认为其他所有事情都应该工作。我将您的command
任务包装在一个debug
任务中进行测试,如下所示:
---
- 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}}"
结果是:
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