ansible / molecule

Molecule aids in the development and testing of Ansible content: collections, playbooks and roles
https://ansible.readthedocs.io/projects/molecule/
MIT License
3.89k stars 664 forks source link

Clarify the ansible collection path #3999

Open cidrblock opened 1 year ago

cidrblock commented 1 year ago

In the getting started guide it is recommended to create an ansible.cfg file in the extension directory. I don't believe this is ever used.

A new ansible.cfg file is built in each ephemeral directory and used for each step.

I think the best approach here is to suggest the following entry in each scenario/molecule.yml file:

provisioner:
  name: ansible
  config_options:
    defaults:
      collections_path: ${ANSIBLE_COLLECTIONS_PATH}

and then have the user set the ANSIBLE_COLLECTIONS_PATH environment variable at the command line prior to running molecule

export ANSIBLE_COLLECTIONS_PATH=/home/user/working/collections

We might also consider adding this to the molecule.yml file that is templated out during init.

This leaves a hard coded path out of the code, but does require the next user to set the var after cloning. (Note: The tox-ansible plugin sets this environment variable for the user, so if the molecule scenarios are being run with pytest or tox this should be taken care of. This can be seen here using all three in a collection here ansible.scm

Feedback welcome.

Brad

cidrblock commented 1 year ago

@ajinkyau Can you look into this? ping me if more info is needed

audgirka commented 11 months ago

@cidrblock I'm in favour of adding the provisioner entry to the molecule.yml file during initialisation.

r0x0d commented 11 months ago

Hi, @cidrblock and @ajinkyau! I have tried the approach @cidrblock mentioned in the comment, but it didn't work out for me.

I can see that molecule was able to identify and correctly populate both the ansible.cfg and molecule.yml in the ephemeral directory, but when I run molecule converge, it still thinks that it should have the collection's path alongside the extension's folder.

image

My ansible.cfg generated inside ~/.cache/molecule/extensions/default:

# Molecule managed

[defaults]
ansible_managed = Ansible managed: Do NOT edit this file manually!
display_failed_stderr = True
forks = 50
retry_files_enabled = False
host_key_checking = False
nocows = 1
interpreter_python = auto_silent
collections_path = /home/r0x0d/Workspace/infra/
[ssh_connection]
scp_if_ssh = True
control_path = %(directory)s/%%h-%%p-%%r

and my molecule.yml, also generated inside ~/.cache/molecule/extensions/default:

# Molecule managed

---
dependency:
  command: null
  enabled: true
  env: {}
  name: galaxy
  options: {}
driver:
  name: vagrant
  options:
    managed: true
  provider:
    name: libvirt
  safe_files: []
  ssh_connection_options: []
platforms:
  - box: eurolinux-vagrant/centos-7
    cpus: 2
    default_box: eurolinux-vagrant/centos-7
    hostname: infra.convert2rhel
    memory: 2048
    name: instance
prerun: true
provisioner:
  ansible_args: []
  children: {}
  config_options:
    defaults:
      collections_path: /home/r0x0d/Workspace/infra/
  connection_options: {}
  env: {}
  inventory:
    group_vars: {}
    host_vars: {}
    hosts: {}
    links: {}
  log: true
  name: ansible
  options: {}
  playbooks:
    cleanup: cleanup.yml
    converge: converge.yml
    create: create.yml
    destroy: destroy.yml
    prepare: prepare.yml
    side_effect: side_effect.yml
    verify: verify.yml
role_name_check: 0
scenario:
  check_sequence:
    - dependency
    - cleanup
    - destroy
    - create
    - prepare
    - converge
    - check
    - cleanup
    - destroy
  cleanup_sequence:
    - cleanup
  converge_sequence:
    - dependency
    - create
    - prepare
    - converge
  create_sequence:
    - dependency
    - create
    - prepare
  destroy_sequence:
    - dependency
    - cleanup
    - destroy
  name: default
  test_sequence:
    - dependency
    - cleanup
    - destroy
    - syntax
    - create
    - prepare
    - converge
    - idempotence
    - side_effect
    - verify
    - cleanup
    - destroy
verifier:
  additional_files_or_dirs: []
  enabled: true
  env: {}
  name: ansible
  options: {}