Ansible Configtree Pattern
Here’s a useful pattern for installing configuration files on your ansible managed hosts.
File: roles/configtree/tasks/main.yml
---
- name: "Check if /roles/configtree/files/ exists"
local_action: stat path="/roles/configtree/files/"
register: file
- name: "Copy /roles/configtree/files/"
copy:
src: ""
dest: /
force: true
become: true
when: file.stat.exists
File: myplaybook.yml
---
- hosts: _blog
roles:
- {role: install-nginx }
- {role: configtree, CONFIGTREE_PATH: "{{inventory_dir | basename}}/_blog/" }
- {role: configtree, CONFIGTREE_PATH: "{{inventory_dir | basename}}/{{ansible_hostname}}/" }
post_tasks:
- name: restart nginx
systemd:
name: nginx
state: restarted
enabled: yes
become: yes
Now in the files
directory of the configtree
role I have a sparse copy of the filesystem for certain host profiles.
For example, I store the https nginx config for my blog in this path:
roles/configtree/files/prod/_blog/etc/nginx/sites-enabled/https
and host specific config, like the hosts file, can go in this path:
roles/configtree/files/prod/nyc1-4gb-public/etc/hosts
where nyc1-4gb-public
is the hostname of the machine.
Note in both of these paths I am using the prod
inventory, I might have different config for dev
or I might share config between the two environments by using a role and matching path like this:
- {role: configtree, CONFIGTREE_PATH: "_prod_and_dev_config/" }