bigdestroyer February 2016

Ansible: 'item' is undefined

I'm trying to use with_items with delegate_to to run a Docker container in several hosts. I have a group test in /etc/ansible/hosts:

[test]
my_machine1
my_machine2

And this task:

 - 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']}}"

But when I run it, I get this error:

{"failed": true, "msg": "ERROR! 'item' is undefined"}

What am I doing wrong?

Thanks in advance

Answers


udondan February 2016

You need to take care of indention. delegate_to and with_items are part of the task, not of the docker module.

- 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']}}"

Though I'm not sure your delegation will work here. What is the background why you need to delegate it in the first place? The normal way would be to apply the play to the hosts of the group test. I guess you're instead running the play against localhost?

Another unrelated thing: I experienced issues with the docker module when pull: always used together with state: reloaded. Unlike docker-compose, the docker module will always restart the container no matter if there was an updated image pulled or not.


- hosts: localhost
  tasks:
    - download nexus
    - build image
    - upload to registry
    - ...
- hosts: test
  tasks:
    - docker: ...

Post Status

Asked in February 2016
Viewed 1,066 times
Voted 13
Answered 1 times

Search




Leave an answer