Mongodb mongo ansible playbook中的rs.add()有问题

Mongodb mongo ansible playbook中的rs.add()有问题,mongodb,ansible,Mongodb,Ansible,我正在使用我的剧本中的以下任务初始化集群并将次要任务添加到主要任务: - name: Initialize replica set run_once: true delegate_to: host1 shell: > mongo --eval 'printjson(rs.initiate())' - name: Format secondaries run_once: true local_action: module: debug

我正在使用我的剧本中的以下任务初始化集群并将次要任务添加到主要任务:

- name: Initialize replica set
  run_once: true
  delegate_to: host1
  shell: >
       mongo --eval 'printjson(rs.initiate())'

- name: Format secondaries
  run_once: true
  local_action:
     module: debug
     msg: '"{{ item }}:27017"'
  with_items: ['host2', 'host3']
  register: secondaries

- name: Add secondaries
  run_once: true
  delegate_to: host1
  shell: >
        /usr/bin/mongo --eval 'printjson(rs.add({{ item.msg }}))'
  with_items: secondaries.results



我得到以下错误:

TASK [mongodb-setup : Add secondaries] *******************************
fatal: [host1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'msg'\n\nThe error appears to have been in '/var/lib/awx/projects/_dev/roles/mongodb-setup/tasks/users.yml': line 15, 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- name: Add secondaries\n  ^ here\n"}
谢谢你的回复,我已经修改了我的代码如下


-  name: Add secondaries
   run_once: true
   delegate_to: host-1
   shell: >
        /usr/bin/mongo --eval 'printjson(rs.add({{ item }}:27017))'
   with_items:
    - host2
    - host3
但在误差以下


failed:[host-2->host-1](item=host-2)=>{“changed”:true,“cmd”:/usr/bin/mongo--eval'printjson(rs.add(host-2:27017)),“delta”:“0:00:00.173077”,“end”:“2019-08-06 13:29:09.422560”,“item”:“host-2”,“msg”:“非零返回代码”,“rc”:252,“start”:“2019-08-06 13:29:09.249483”,“stderr”:“stderr”:“stderu行[]”,“stdout”:“MongoDB外壳版本:3.2.22\n连接到:test\n2019-08-06T13:29:09.419-0500 E查询[thread1]语法错误:缺失)在参数列表之后@(外壳评估):1:37”,“标准输出行”:[“MongoDB外壳版本:3.2.22”,“连接到:测试”,“2019-08-06T13:29:09.419-0500 E查询[thread1]语法错误:缺失)在参数列表之后@(外壳评估):1:37“]}

您发出的不是与
rs.add()
相关的,而是与您循环的数据相关的。在上一个任务中,您的项目列表是一个字符串

# Wrong #
with_items: secondaries.results
您希望从以前注册的结果中传递实际列表:

with_items: "{{ secondaries.results }}"
也就是说,注册调试任务的结果相当奇怪。您应该使用
set\u fact
在var中注册您需要的内容,或者最好直接在任务中循环其他主机列表。它看起来也像,因此您应该在eval中引用参数。类似于:

- name: Add secondaries
  shell: >
    /usr/bin/mongo --eval 'printjson(rs.add("{{ item }}:27017"))'
  with_items:
    - host2
    - host3


在这种情况下,你使用授权的方式对我来说似乎很奇怪,但如果没有一个完整的剧本示例来说明你正在尝试做什么,就很难给出任何有效的线索(如果有必要,你可以在一个新问题中给出).

您是否尝试手动运行该命令?您不需要以某种方式引用您的参数吗?是的,将其置于引号中,但仍然失败
shell:“/usr/bin/mongo--eval'printjson(rs.add({item}}:27017))””
引用参数,我的意思是(纯粹的猜测)添加到
rs.add
。我刚刚搜索了一下,它看起来像是。查看我编辑的答案below@Zeitounator。感谢您的帮助,但我仍然面临以下错误,在加载YAML时尝试了大量组合
语法错误。\n未找到所需的键\n\shell:\“/usr/bin/mongo--eval'printjson(rs.add(\“{item}\\\:27017\”)\“\n
在我删除shell:”后面的双引号后,问题现在得到解决。非常感谢你的帮助。