sorin
sorin

Reputation: 170548

How can I store ansible playbooks in different directories but still be able to call them?

It seems that ansible assumes that the ansible.cfg file exists in the current working directory so when you try to call a playbook that exists in a subdirectory it will fail to load the roles and other stuff.

Is it possible to store playbook in different directories?

Please note that the ansible.cfg is part of the source code.

Upvotes: 3

Views: 5796

Answers (2)

Mario Keller
Mario Keller

Reputation: 421

I was facing the same problem. I wanted to putt all the playbooks in a separate directory instead of having them all flying around in my base directory. My base directory is under git control an it contains a local ansible.cfg file with the following content:

[defaults]
inventory=hosts
roles_path=roles

By setting roles_path ansible with look for the roles directory in the base directory instead of the subdirectory of your playbooks. Be aware that if you have another directory with files you're referencing in your playbooks, you have to qualify this as followed:

- name: copy nagios config files
  copy:
    src: ../files/nagios3/config/
    dest: /etc/nagios3/conf.d/
    owner: root
    group: root
  notify: reload nagios

In this case the playbook is located in a playbooks subdirectoy at the same level as the files directory. Just like that:

.
├── README
├── ansible.cfg
├── files
│   └── ...
├── host_vars
│   └── ...
├── hosts
├── playbooks
│   └── ...
├── roles
│   └── ...
└── templates
    └── ...

Upvotes: 2

Xiong Chiamiov
Xiong Chiamiov

Reputation: 13734

Per the documentation, Ansible will look for the configuration file in the following order:

  • ANSIBLE_CONFIG (an environment variable)
  • ansible.cfg (in the current directory)
  • .ansible.cfg (in the home directory)
  • /etc/ansible/ansible.cfg

So if you'd like to call playbooks from alternate directories, you can pass along ANSIBLE_CONFIG pointing at the appropriate ansible.cfg.

Upvotes: 4

Related Questions