Ansible:'item'未定义

Héc*_*tor 6 ansible docker ansible-playbook

我试图用with_itemsdelegate_to在几台主机上运行一个泊坞窗容器.我公司拥有一批test/etc/ansible/hosts:

[test]
my_machine1
my_machine2
Run Code Online (Sandbox Code Playgroud)

而这个任务:

 - name: Run app container
    docker:
      name: "{{artifact_id}}"
      insecure_registry: true
      image: "{{image}}:{{version}}"
      pull: always
      state: reloaded
      ports:
      - "{{port_mapping}}"
      delegate_to: '{{item}}'
      with_items:
      - "{{groups['test']}}"
Run Code Online (Sandbox Code Playgroud)

但是当我运行它时,我收到此错误:

{"failed":true,"msg":"ERROR!'item'未定义"}

我究竟做错了什么?

提前致谢

udo*_*dan 22

你需要照顾压力.delegate_to并且with_items是任务的一部分,而不是docker模块.

- name: Run app container
  docker:
    name: "{{artifact_id}}"
    insecure_registry: true
    image: "{{image}}:{{version}}"
    pull: always
    state: reloaded
    ports:
      - "{{port_mapping}}"
  delegate_to: '{{item}}'
  with_items: "{{groups['test']}}"
Run Code Online (Sandbox Code Playgroud)

虽然我不确定你的代表团会在这里工作.您首先需要委托它的背景是什么?通常的方法是将游戏应用于组的主机test.我猜你是在反对localhost运行游戏?

另一个不相关的事情:我与docker模块pull: always一起使用时遇到了问题state: reloaded.与docker-compose不同,无论是否拉出更新的图像,docker模块都将始终重新启动容器.


- hosts: localhost
  tasks:
    - download nexus
    - build image
    - upload to registry
    - ...
- hosts: test
  tasks:
    - docker: ...
Run Code Online (Sandbox Code Playgroud)