How do we set config file in another place?

On each project we have each ansible.cnf file.
When I run playbook file or run ansbile-galaxy command for each project, it always load /etc/ansible/ansible.cfg file, How do we change this config file in another path?

Hey,

Have a look to the related documentation page / section.

I don’t know for AWX jobs, but assuming each of your project have its own ansible.cfg file in the directory you’re running your playbook, it should be used over the one you have in /etc/ansible/. You could also use the ANSIBLE_CONFIG envvar to tell Ansible which config file to use for your run.

Also ask yourself why you need to do that in the first place; if it’s because you want to define roles / collections paths for each job, for instance, then you could also use envvars as well.

Edit: Haven’t seen @utoddl already answered your question, as you asked in another thread.

3 Likes

To expand on @ptn’s comment about environment variables vs. ansible.cfg files.

When our group first started using Ansible, we put custom ansible.cfg files all over the place, no two of them just alike. We do our playbook development and initial testing on our ssh bastion; final testing and production happen in our AWX instance. With so many different ansible.cfg files floating around, getting consistency with each other, and particularly with our AWX instance, was a struggle.

So we abandoned / banned all ansible.cfg files besides the one in /etc/ansible/, which we tweaked to match our AWX environment as closely as possible. So everybody gets the same, sane, “local” defaults. For those cases where you really do need to customize something, we set an environment variable. The values from environment variables overlay those from the one ansible.cfg file, so we get to adjust individual settings without losing the carefully curated set in /etc/ansible/ansible.cfg.

To facilitate transitioning from ~/.ansible.cfg to environment variables, we came up with ansible-cfg2env, a Python script that uses ansible-config underneath to examine settings and preserve any customizations in a form a shell can use. Here’s how it describes itself:

$ ansible-cfg2env -h
usage: ansible-cfg2env [-h] [-c] [-a]

optional arguments:
  -h, --help  show this help message and exit
  -c          format for csh/tcsh instead of sh/ksh/bash.
  -a          include comments for all settings, not just those differing from their default values.

ansible-cfg2env converts ansible configuration settings from ansible.cfg
format to a format suitable to set the equivalent command shell environment
variables. Given that Ansible ignores system level ansible.cfg files after
finding a user's ~/.ansible.cfg (rather than combining settings from all
found config files), we're now recommending that users use environment
variables to override only those settings they need to change. Settings
from environment variables take precedence over those from found
ansible.cfg files.

Typical usage:
  ansible-cfg2env -a > ~/.ansible.cfg.sh
  mv ~/.ansible.cfg ~/.ansible.cfg-$(date -r ~/.ansible.cfg +%Y-%m-%d)
  echo ". ~/.ansible.cfg.sh" >> ~/.bash_profile
  . ~/.ansible.cfg.sh

Since I switched off my ~/.ansible.cfg as described above, I now customize my Ansible configuration exclusively with environment variables through ~/.ansible.cfg.sh while still benefiting from the settings curated by our group as preserved in /etc/ansible/ansible.cfg.

1 Like