atosatto / ansible-dockerswarm

Docker Engine clustering using "Swarm Mode" and Ansible
https://galaxy.ansible.com/atosatto/docker-swarm/
MIT License
264 stars 148 forks source link

Multiple imports of the role don't work #95

Open till opened 3 years ago

till commented 3 years ago

I am using your role currently like so:

  1. I use it it to setup the "base" system (docker, docker-py, etc.)
  2. I install docker plugins with another role
  3. I am using your role again to re-configure docker's daemon.json

This is a bit of a maybe oddity, but I found no other way to insure that first Docker gets installed, so I can use docker plugin install and only then do I reconfigure daemon.json to include the plugin. If I try to configure daemon.json right away, the initial start of dockerd fails and therefor also the plugin install. Anyhow, I am a bit stumped.

The problem: It seems no matter what I do, it the role skips certain parts and looks like the variables from the second "invocation" of your role are set. In this case, I am relying on docker-py to be installed so I can use it in my docker plugin role. In the second run, I am trying to save some time by disabling (almost) everything but the configuration of daemon.json.

Here is an example playbook (or prepare.yml) with Molecule:

  tasks:
    - import_role:
        name: atosatto.docker-swarm
      vars:
        docker_daemon_config:
          storage-driver: vfs
        docker_py_package_version: 4.4.4
        docker_service_override: |
          [Service]
          ExecStart=
          ExecStart=/usr/bin/dockerd -H unix://
        skip_docker_compose: True
        skip_group: True
    - import_role:
        name: ansible-docker-plugin-loki
      vars:
        docker_loki_version: v1.2.0
    - import_role:
        name: atosatto.docker-swarm
      vars:
        docker_daemon_config:
          storage-driver: vfs
          log-driver: loki
        skip_cli: True
        skip_containerd: True
        skip_docker_compose: True
        skip_docker_py: True
        skip_group: True
        skip_swarm: True

During the first run, skip_docker_py is already true:

    TASK [atosatto.docker-swarm : Show env] ****************************************
    task path: /root/.cache/molecule/ansible-docker-plugin-loki/upgrade/roles/atosatto.docker-swarm/tasks/setup-python-pip.yml:2
    ok: [instance] => {
        "msg": "Show Variables:\nskip_docker_py: True, skip_docker_compose: True\n\nTest if #1\nFalse - False\n\nTest if #2\nFalse\n\nTest if #3\nTrue\n"
    }

I dumped it on a local branch: https://github.com/till/ansible-dockerswarm/commit/f8b42b6a8a0d283d1d5d926d593c9d92f8d24750

I am not sure if this is a regression in Ansible as I haven't noticed this before and my test started failing only "recently". But here is my environment:

    ansible-playbook 2.9.7
      config file = /root/.cache/molecule/ansible-docker-plugin-loki/upgrade/ansible.cfg
      configured module search path = ['/usr/lib/python3.8/site-packages/molecule/provisioner/ansible/plugins/modules', '/root/.cache/molecule/ansible-docker-plugin-loki/upgrade/library', '/tmp/ansible-docker-plugin-loki/library', '/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
      ansible python module location = /usr/lib/python3.8/site-packages/ansible
      executable location = /usr/bin/ansible-playbook
      python version = 3.8.2 (default, Apr 13 2020, 10:20:46) [GCC 9.3.0]
    Using /root/.cache/molecule/ansible-docker-plugin-loki/upgrade/ansible.cfg as config file
    host_list declined parsing /root/.cache/molecule/ansible-docker-plugin-loki/upgrade/inventory/ansible_inventory.yml as it did not pass its verify_file() method
    script declined parsing /root/.cache/molecule/ansible-docker-plugin-loki/upgrade/inventory/ansible_inventory.yml as it did not pass its verify_file() method
    Parsed /root/.cache/molecule/ansible-docker-plugin-loki/upgrade/inventory/ansible_inventory.yml inventory source with yaml plugin
    1 plays in /tmp/ansible-docker-plugin-loki/molecule/upgrade/converge.yml

I tried adding allow_duplicates: True to my import_role statements. But to no avail. Anyhow, any thoughts? Maybe this is the wrong repository for this bug report. 🤷🏼 Any input appreciated.

juanluisbaptiste commented 3 years ago

Have you thought on using a different role to install docker first instead of using this one ? like for example geerlingguy/ansible-role-docker. This way you can install docker first, then the plugin and lastly then run this role with skip_engine: true to disable the docker engine installation.

till commented 3 years ago

@juanluisbaptiste thanks for commenting.

We used the other role in the beginning, but it was too brittle and required way more additions. I know he's doing his best to manage a gazillion roles though, so no offence meant. :)

I really prefer using one, and I prefer to limit my exposure to random things I have to fix all around. ;)

I managed to overcome this like so:

# molecule/foo/requirements.yml
- name: atosatto.docker-swarm
- name: atosatto.docker-swarm-base
  src: https://github.com/atosatto/ansible-dockerswarm.git
  version: master

And then I use:

roles:
  - role: atosatto.docker-swarm-base
  - role: atosatto.docker-swarm

And it works.