How to assert that playbook is run with a proper ansible.cfg
Sometime projects has a very stringent requirements on ansible.cfg. F.e. ceph-ansible uses it to specify location for plugins and modules. Some may have quirks for network_cli (f.e. additional retries), etc, etc. I even don’t want to touch the topic of ‘merge’ setting here (which is evil). Anyway, you may have a playbook which requires the specific ansible.cfg to run.
Unfortunately, there is no variable to check the path of the ansible.cfg used. It can be redefined by ANSIBLE_INVENTORY environment variable, or by command line, or different ansible.cfg may be used if ansible is run from a random directory with absolute paths to an inventory and the playbook.
Anyway, you want to assert
your ansible.cfg, but you can’t, because there is no variable.
Here the trick to do assertion.
ansible.cfg:
[tags]
SKIP=never, set_to_skip_in_ansible_cfg
_asserts.yaml:
---
- hosts: localhost, all
run_once: true
gather_facts: false
tasks:
... other asserts here
- name: Check if proper ansible.cfg is used
fail:
msg: 'You should use ansible.cfg from this repository'
tags: [set_to_skip_in_ansible_cfg]
The idea here is that tag set_to_skip_in_ansible_cfg
will be executed on default ansible.cfg
. We specifically add it to skip tags (like ‘never’) so it silently ignored, but only if a proper ansible.cfg
is in use.
Output is not cluttered (task with skipped tag is silently removed from runlist), and it works like magic.