0

I am using an ansible-local provisioner in Packer, and trying to use the community.general collection, however I cannot get my playbook to see it at all.

The Packer code is:

{
  "type": "ansible-local",
  "staging_directory": "/tmp/packer-provisioner-ansible-local",
  "command": "ANSIBLE_COLLECTIONS_PATHS=/tmp/packer-provisioner-ansible-local/collections/roles ansible-playbook",
  "playbook_file": "{{ user `jenkins_workspace` }}/packer/ansible/playbook.yml",
  "playbook_dir": "{{ user `jenkins_workspace` }}/packer/ansible",
  "galaxy_command": "ANSIBLE_COLLECTIONS_PATHS=/tmp/packer-provisioner-ansible-local/collections/roles ansible-galaxy collection",
  "galaxy_file": "{{ user `jenkins_workspace` }}/packer/ansible/collections/requirements.yml",
  "inventory_groups": "{{user `inventory_groups`}}"
}

The collections seems to install no problem in the correct DIR

amazon-ebs: Executing Ansible Galaxy: cd /tmp/packer-provisioner-ansible-local && ANSIBLE_COLLECTIONS_PATHS=/tmp/packer-provisioner-ansible-local/roles ansible-galaxy collection install -r /tmp/packer-provisioner-ansible-local/requirements.yml -p /tmp/packer-provisioner-ansible-local/roles
amazon-ebs: |Installing 'community.general:2.0.0' to '/tmp/packer-provisioner-ansible-local/roles/ansible_collections/community/general'

However then Ansible is unable to use it

amazon-ebs: Executing Ansible: cd /tmp/packer-provisioner-ansible-local && ANSIBLE_COLLECTIONS_PATHS=/tmp/packer-provisioner-ansible-local/roles ansible-playbook /tmp/packer-provisioner-ansible-local/playbook.yml --extra-vars "packer_build_name=amazon-ebs packer_builder_type=amazon-ebs packer_http_addr=ERR_HTTP_ADDR_NOT_IMPLEMENTED_BY_BUILDER -o IdentitiesOnly=yes"  -c local -i /tmp/packer-provisioner-ansible-local/packer-provisioner-ansible-local801906075
amazon-ebs: ERROR! couldn't resolve module/action 'community.general.docker_compose'. This often indicates a misspelling, missing collection, or incorrect module path.

The Anisble play in question is

    - name: Docker compose build the containers
      community.general.docker_compose:
        recreate: never
        pull: yes
        build: yes
        state: present
        stopped: yes
        project_src: "{{ home }}"

Any advice on where I am going wrong with this would be great....

apr_1985
  • 1,764
  • 2
  • 14
  • 27
  • I have worked around this by using the built in docker-compose module as I am still on 2.9, but I wanted to make the code future proof for when we move to 2.10. – apr_1985 Feb 08 '21 at 16:32
  • That's what I was just about to ask: what version you're on -- that syntax did not magically become backported to every ansible version in the world; if you're on 2.9, you have to use the 2.9 version as there _was no collections concept_ in 2.9; I would guess that's why they accept both styles in 2.10 to allow migration over time – mdaniel Feb 08 '21 at 17:06

2 Answers2

3

This is because I was using Ansible 2.9 and the feature only came in at 2.10 (that'll teach me to not check the docs version I am reading :D ).

Shame the Ansible error mentions collections being missing if it cant use them...

ERROR! couldn't resolve module/action 'community.general.docker_compose'. This often indicates a misspelling, missing collection, or incorrect module path.

Hopefully the question might help others in a similar position.

apr_1985
  • 1,764
  • 2
  • 14
  • 27
0

Here the docker-compose collection is missing.

Run this to install the docker collection, make sure you are on Ansible version 2.10+ and rerun the playbook

ansible-galaxy collection install community.docker 
ninohead
  • 325
  • 2
  • 6