Ansible: Change playbooks location

Viewed 2822

I have all playbooks in /etc/ansible/playbooks and I want to execute them anywhere on the pc

I tried to configure playbook_dir variable in ansible.cfg

[defaults]
playbook_dir = /etc/ansible/playbooks/

and tried to put ANSIBLE_PLAYBOOK_DIR variable in ~/.bashrc

export ANSIBLE_PLAYBOOK_DIR=/etc/ansible/playbooks/

but I only got the same error in both cases:

nor@nor:~$ ansible-playbook test3.yaml
ERROR! the playbook: test3.yaml could not be found

This is my ansible version:

ansible 2.9.7
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/nor/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.7/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.7.3 (default, Oct  7 2019, 12:56:13) [GCC 8.3.0]

Does anyone know the problem and how to solve it?

3 Answers

According to https://manpages.debian.org/testing/ansible/ansible-inventory.1.en.html :

--playbook-dir 'BASEDIR'

Since this tool does not use playbooks, use this as a subsitute playbook directory.This sets the relative path for many features including roles/ group_vars/ etc.

This means that ANSIBLE_PLAYBOOK_DIR is not used as a replacement for specifying the the absolute / relative path to your playbook, but it tells the playbook where it should look for roles, host/group vars , etc.

The goal you're trying to achieve is has no solution on the ansible side, you need to achieve this by configuring your shell profile accordingly. set the following in your .bashrc file:

export playbooks_dir=/path/to/playbooks

when you call the playbook use ansible-playbook $playbooks_dir/test3.yml

PLAYBOOK_DIR says:

"A number of non-playbook CLIs have a --playbook-dir argument; this sets the default value for it."

Unfortunately, there is no hint in the doc what "the non-playbook CLIs" might be. ansible-playbook isn't one of them, obviously.


FWIW. If you're looking for a command-line oriented framework try ansible-runner. For example, export the location of private_data_dir

shell> export ansible_private=/path/to/<private-data-dir>

Then run the playbook

shell> ansible-runner -p playbook.yml run $ansible_private

As others have said, ANSIBLE_PLAYBOOK_DIR is for setting the relative directory for roles/, files/, etc. IMHO, it's not terribly useful.

If I understand the op, this is how I accomplish a similar result with all versions of ansible ...

PPWD=$PWD cd /my/playbook/dir && ansible-playbook my_playbook.yml; cd $PPWD

Explained,

PPWD=$PWD is to remember the current/present/previous working directory, then cd /my/playbook/dir and if that succeeds run ansible-playbook my_playbook.yml (everything is relative from there); regardless, always change back to the previous working directory

Related