Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 285
Héctor Avatar asked Feb 08 '16 12:02

Héctor


People also ask

What is item in ansible?

Ansible with_items is a lookup type plugins which is used to return list items passed into it. Actual plugin name is items. Ansible have different plugin types, further these plugin types have various plugins in each category. One such plugin type is lookup, which allows ansible to access data from outside resources.

What does gather facts do in ansible?

This module is automatically called by playbooks to gather useful variables about remote hosts that can be used in playbooks. It can also be executed directly by /usr/bin/ansible to check what variables are available to a host.


1 Answers

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: ...
like image 103
udondan Avatar answered Sep 29 '22 15:09

udondan