1

I'm trying for hours already to run a docker installation on a specific group of hosts.

This is my hosts.yml

all:
  hosts:
    01-dev:
      ansible_host: <IP1>
    02-dev:
      ansible_host: <IP2>
    03-dev:
      ansible_host: <IP3>

  children:
    docker:
      hosts:
        01-dev
        02-dev
    dev:
      hosts:
        01-dev
        02-dev
        03-dev

This is my site.yml


- name: Configure hosts
  hosts: dev
  tasks:
    - name: Update and upgrade apt packages
      become: true
      apt:
        upgrade: yes
        update_cache: yes
        cache_valid_time: 86400 #One day
    - name: Install needed network manager libs
      become: yes
      ansible.builtin.package:
        name:
          - network-manager
        state: present
    - name: "Configuring eth"
      become: yes
      community.general.nmcli:
        conn_name: "System ens18"
        ifname: ens18
        type: ethernet
        state: present
        autoconnect: yes
        ip4: "{{ansible_host}}/24"
        gw4: "192.168.3.1"

  hosts: docker
  become: true
  tasks:
    - name: Install aptitude using apt
      apt: name=aptitude state=latest update_cache=yes force_apt_get=yes

    - name: Install required system packages
      apt: name={{ item }} state=latest update_cache=yes
      loop: [ 'apt-transport-https', 'ca-certificates', 'curl', 'software-properties-common', 'python3-pip', 'virtualenv', 'python3-setuptools']

    - name: Add Docker GPG apt Key
      apt_key:
        url: https://download.docker.com/linux/ubuntu/gpg
        state: present

    - name: Add Docker Repository
      apt_repository:
        repo: deb https://download.docker.com/linux/ubuntu bionic stable
        state: present

    - name: Update apt and install docker-ce
      apt: update_cache=yes name=docker-ce state=latest

    - name: Install Docker Module for Python
      pip:
        name: docker

Then I run the command ansible-playbook -i hosts.yml site.yml -vvv --ask-become-pass and I get the error that there are no hosts for a group docker

What is wrong with my hosts file?

1

I think it should be:

children:
    docker:
      hosts:
        01-dev:
        02-dev:

(The colon)

1
  • yes, that fixed it, thanks. But why does ansible expect a colon after the hosts? There are no elements as sub-keys. The syntax is quite counter-intuitive – Christian Feb 5 at 22:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.