Does anyone know how to put playbooks into folders, but share the same roles, group_vars, and other stuff typically located at the root dir?
Here's what I'd like to have:
root_dir: - group_vars - roles - inventory - playbooks - my_playbook.yml - site.yml - deploy.yml
Our root dir is getting pretty big now and I'd like to split out some playbooks into their own folder (shown as playbooks/ above). An identical tiny playbook fails to run when inside a directory (say, playbooks/) vs at the root dir, because it doesn't grab stuff from group_vars.
I can partially work around this, and run a playbook inside my playbooks/ directory:
- hosts: host_group sudo: true gather_facts: false vars_files: - ../group_vars/all/main.yml
This picks up the vars defined in main.yml. However, it's not clear to me if this would add group variables defined in group_vars/, as opposed to the explicitly specificed ../group_vars/.
Thanks!
By default, Ansible assumes your playbooks are stored in one directory with roles stored in a sub-directory called roles/ . As you use Ansible to automate more tasks, you may want to move your playbooks into a sub-directory called playbooks/ .
To see how jobs work, create a job in Ansible Tower with the following: 1. Run the commands below to create a playbooks directory within the /var/lib/awx/projects directory. This directory will contain all the playbooks that you'll configure in your job in Ansible Tower.
Variables and Vaults A best practice approach for this is to start with a group_vars/ subdirectory named after the group. Inside of this subdirectory, create two files named vars and vault . Inside of the vars file, define all of the variables needed, including any sensitive ones.
The host_vars is a similar folder to group_vars in the repository structure. It contains data models that apply to individual hosts/devices in the hosts. ini file. Hence, there is a YAML file created per device containing specific information about that device.
Ansible will pick up group_vars
without stating path explicitly:
Here is the example directory structure in /tmp/ansible
:
/tmp/ansible
├── group_vars
│ └── test_group.yml
├── inventory
├── playbooks
│ └── foo.yml
└── site.yml
Inventory file:
[test_group]
localhost
Main Playbook site.yml
:
# site.yml
---
- include: playbooks/foo.yml
Secondary Playbook foo.yml
:
---
- hosts: localhost
tasks:
- debug: var=foo
Group Variables test_group.yml
:
foo: bar
Here are all the ways to execute the secondary playbook:
From root folder using relative path /tmp/ansible
:
ansible-playbook -i inventory playbooks/foo.yml -c local
PLAY [localhost] **************************************************************
GATHERING FACTS ***************************************************************
ok: [localhost]
TASK: [debug var=foo] *********************************************************
ok: [localhost] => {
"var": {
"foo": "bar"
}
}
PLAY RECAP ********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
From Playbooks subfolder with inventory in parent path /tmp/ansible/playbooks
:
ansible-playbook -i ../inventory foo.yml -c local
PLAY [localhost] **************************************************************
GATHERING FACTS ***************************************************************
ok: [localhost]
TASK: [debug var=foo] *********************************************************
ok: [localhost] => {
"var": {
"foo": "bar"
}
}
PLAY RECAP ********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
As we can see in the above examples, ansible-playbook will look for variables based on the path of inventory file
or folder were ansible was executed from. Playbooks can be separated without additional effort.
Gregory Shulov's answer works if you don't use roles. If you use roles, though, your subfolder playbooks won't be able to reference them normally.
If you don't have too many files, I would recommend separating them "subfolder" files by prepending them with an underscore. That helped me separate some building block plays from the real playbooks I expect people to run.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With