Let me know if Im missing the mark, I feel like I havnt fully grasped your question yet.
I’ve found a good approach is to break inventory files into environments, and in the inventory files have groups that correspond to roles. For you, it might look like two different files:
# inventory/production.yml
all:
children:
nodejs:
hosts:
nodejs01.infra_com
nodejs02.infra_com
db:
hosts:
db01.infra_com
db02.infra_com
# inventory/test.yml
all:
children:
nodejs:
hosts:
nodejs_test
db:
hosts:
db_test
Then in your playbooks, you target the roles. For example:
# playbooks/mariadb/playbook.yml
---
- hosts: db
tasks:
....
When you run ansible (push), you will specify the inventory you want to include:
ansible-playbook -i inventory/production.yml playbooks/mariadb/playbook.yml
For push setups, this structure will mean your local host or ansible controller will connect to the hosts in the db
group in the production.yml
inventory over SSH.
Since your doing ansible-pull right now, your really only working with local connections. You could do a hairpin ssh connection (like the remote host ssh’s to itself) but i think local connections will be better anyway.
Instead of your playbooks targeting localhost like this:
- hosts: localhost
tasks:
......
You can use the same setup i described above, but set the ansible_connection at the command line. You probably also need to limit the playbook to the host your running on.
ansible-playbook -i inventory/production.yml playbooks/mariadb/playbook.yml -e 'ansible_connection=local' --limit $(hostname)
Finally, you can configure your group_vars
or host_vars
as needed, but set them up by the server role (inventory/group_vars/db.yml
) or the hostname in the inventory file, even if the ansible_connection is local (inventory/host_vars/nodejs01.infra_com.yml
). You can even do production or test groups, with a slight adjustment to the inventory file:
# inventory/production.yml
all:
children:
production:
children:
nodejs:
hosts:
nodejs01.infra_com
nodejs02.infra_com
db:
hosts:
db01.infra_com
db02.infra_com
Some additional reading:
https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html#inventory-setup-examples
https://docs.ansible.com/ansible/2.8/user_guide/playbooks_best_practices.html
I hope this is helpful