hashicorp / packer-plugin-ansible

Packer plugin for Ansible Provisioner
https://www.packer.io/docs/provisioners/ansible
Mozilla Public License 2.0
49 stars 35 forks source link

ansible-local provisioner installs galaxy roles to <staging dir>/galaxy_roles/ but does not add that to Ansible's role path #111

Open isuftin opened 2 years ago

isuftin commented 2 years ago

Overview of the Issue

The latest Packer 1.8.3 is using the latest release of this plugin. It seems like the latest release of this plugin via ansible-local now installs roles to /galaxy_roles/...

But when running a playbook via ansible-local, that path is not included in the Ansible role paths

Packer 1.8.3 with error:

...
2022-08-04T20:46:12Z:     amazon-ebs: - downloading role 'ntp', owned by geerlingguy
2022-08-04T20:46:13Z:     amazon-ebs: - downloading role from https://github.com/geerlingguy/ansible-role-ntp/archive/2.3.1.tar.gz
2022-08-04T20:46:13Z:     amazon-ebs: - extracting geerlingguy.ntp to /tmp/packer-provisioner-ansible-local/62ec2e12-8f77-0531-c4d6-e3bb20d3ec92/galaxy_roles/geerlingguy.ntp
2022-08-04T20:46:13Z:     amazon-ebs: - geerlingguy.ntp (2.3.1) was installed successfully
...

2022-08-04T20:46:37Z:     amazon-ebs: TASK [ntp | Set variables for ntp role] ****************************************
2022-08-04T20:46:37Z:     amazon-ebs: ok: [127.0.0.1]
2022-08-04T20:46:37Z:     amazon-ebs:
2022-08-04T20:46:37Z:     amazon-ebs: TASK [ntp | Set up NTP] ********************************************************
2022-08-04T20:46:37Z:     amazon-ebs: ERROR! the role 'geerlingguy.ntp' was not found in /tmp/packer-provisioner-ansible-local/62ec2e12-8f77-0531-c4d6-e3bb20d3ec92/roles:/home/ec2-user/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/tmp/packer-provisioner-ansible-local/62ec2e12-8f77-0531-c4d6-e3bb20d3ec92
2022-08-04T20:46:37Z:     amazon-ebs: 
2022-08-04T20:46:37Z:     amazon-ebs: The error appears to be in '/tmp/packer-provisioner-ansible-local/62ec2e12-8f77-0531-c4d6-e3bb20d3ec92/site.yml': line 29, column 15, but may
2022-08-04T20:46:37Z:     amazon-ebs: be elsewhere in the file depending on the exact syntax problem.
2022-08-04T20:46:37Z:     amazon-ebs: 
2022-08-04T20:46:37Z:     amazon-ebs: The offending line appears to be:
2022-08-04T20:46:37Z:     amazon-ebs: 
2022-08-04T20:46:37Z:     amazon-ebs:       ansible.builtin.include_role:
2022-08-04T20:46:37Z:     amazon-ebs:         name: geerlingguy.ntp
2022-08-04T20:46:37Z:     amazon-ebs:               ^ here

Packer 1.8.2 working as expected:

...
2022-07-22T21:15:24Z:     amazon-ebs: - downloading role 'ntp', owned by geerlingguy
2022-07-22T21:15:26Z:     amazon-ebs: - downloading role from https://github.com/geerlingguy/ansible-role-ntp/archive/2.3.1.tar.gz
2022-07-22T21:15:27Z:     amazon-ebs: - extracting geerlingguy.ntp to /tmp/packer-provisioner-ansible-local/62db122b-92aa-f998-3a08-88fa0c876275/roles/geerlingguy.ntp
2022-07-22T21:15:27Z:     amazon-ebs: - geerlingguy.ntp (2.3.1) was installed successfully
...
2022-07-22T21:16:00Z:     amazon-ebs: TASK [ntp | Set up NTP] ********************************************************
2022-07-22T21:16:00Z:     amazon-ebs:
2022-07-22T21:16:00Z:     amazon-ebs: TASK [geerlingguy.ntp : Include OS-specific variables.] ************************
2022-07-22T21:16:00Z:     amazon-ebs: ok: [127.0.0.1]
2022-07-22T21:16:00Z:     amazon-ebs:
2022-07-22T21:16:00Z:     amazon-ebs: TASK [geerlingguy.ntp : Set the ntp_driftfile variable.] ***********************
2022-07-22T21:16:00Z:     amazon-ebs: ok: [127.0.0.1]
2022-07-22T21:16:00Z:     amazon-ebs:
...

...

Reproduction Steps

Configuration to run ansible-local:

{
      "type": "ansible-local",
      "playbook_dir": "ansible_playbook",
      "playbook_files": ["ansible_playbook/site.yml"],
      "galaxy_file": "ansible_playbook/requirements.yml",
}

Galaxy file:

- src: geerlingguy.ntp
  version: 2.3.1

site.yml:

---
- name: EC2 Provisioning
  hosts: all
  tasks:
    - name: ntp | Set up NTP
      ansible.builtin.include_role:
        name: geerlingguy.ntp
        apply:
          become: true

Remote system's ansible.cfg:

[defaults]
log_path = /var/log/ansible.log
ansible_python_interpreter = /bin/python3
nocows = True
fact_caching = jsonfile
fact_caching_connection = /tmp/facts_cache
fact_caching_timeout = 7200

Remote system is a AWS Marketplace RHEL 8 - ami-08970fb2e5767e3b8 Remote system has Python3.6 and Ansible 4.10.0 installed

Plugin and Packer version

1.8.3

Is no one else getting this?

isuftin commented 2 years ago

btw, I'm able to get around this issue by specifying the roles and collections path in the builder:

{
      "type": "ansible-local",
      "playbook_dir": "ansible_playbook",
      "playbook_files": ["ansible_playbook/site.yml"],
      "galaxy_file": "ansible_playbook/requirements.yml",
      "galaxy_roles_path": "/home/ec2-user/.ansible/roles",
      "galaxy_collections_path": "/home/ec2-user/.ansible/collections"
    }

I did not need to do this in the previous version and this seems like a regression

isuftin commented 2 years ago

Assuming the issue was introduced at this point: https://github.com/hashicorp/packer-plugin-ansible/commit/c64d013146eb224bdc38b1e6417d3bf8c58293dc

rollulus commented 1 year ago

Hey thanks for this issue, you're not alone with this, I just ran into it as well.

voegelas commented 1 year ago

Assuming the issue was introduced at this point: c64d013

That code searches the galaxy_file for strings that match the regular expressions (?m)^roles: and (?m)^collections:. The ansible plugin ought to default to roles if neither regular expression matches. I've rewritten my requirements file in order to work around this bug. There's some information on the current format of the requirements file in the section Installing collections of the Ansible user guide.

Old requirements file:

---
- src: geerlingguy.packer_rhel
- src: geerlingguy.packer-debian
- src: geerlingguy.nfs

New requirements file:

---
roles:
  - geerlingguy.packer_rhel
  - geerlingguy.packer-debian
  - geerlingguy.nfs