0
votes

I am utterly confused about how to manage multiple environments(prod/dev) with ansible. I am also using molecule to test locally.

So here is my Project layout as of now.

|----inventories/   
|    |   
|    |--dev/
|    |  |
|    |  |--group_vars/...
|    |  |
|    |  |--host_vars/...
|    |  
|    |--prod/
|       |
|       |--group_vars/...
|       |
|       |--host_vars/
|            |
|            |--my_playbook_hostname_vars.yml
|     
|----roles/...     
|     
|----hosts.yml    
|
|----my_playbook.yml  
|  

This is the directory structure according to the docu

Now I have my molecule file where I can just link the dev directories like this

# molecule.yml
provisioner:
  name: ansible
  inventory:
    links:
      group_vars: ../../../../inventories/dev/group_vars/
      host_vars: ../../../../inventories/dev/host_vars/

So Molecule works just fine for local testing with a vagrant driver and I could also set it up to use a EC2 driver to test it in the cloud for example. So far so good.

But how do I start the my_playbook.yml? When I use ansible-playbook my_playbook.yml then it does not know where to look for the vars, since there are two environments. How can I tell Ansible to look under inventories/prod/group_vars & inventories/prod/host_vars and then resolve further via the host and group name like it is defined in the hosts.yml without compromising my molecule setup?

When I just setup group_vars/ & host_vars/ in the root dir it works

1
Please edit the question to include: Your playbook, the full command and error when running the playbook, and example of the inventory configuration (how do you get hosts in the inventory).Andy Shinn

1 Answers

4
votes

According to documentation:

-i, --inventory
    specify inventory host path or comma separated host list.

One option is to create a separate hosts file for each environment (pretty much the same way as in the documentation link you provided). Like this:

|-- inventories
|   |-- dev
|   |  |-- group_vars/
|   |  |-- host_vars/
|   |  |-- hosts
|   |-- prod
|      |-- group_vars/
|      |-- host_vars/
|      |-- hosts
|-- roles/
|-- my_playbook.yml 

...and then call ansible-playbook with -i

ansible-playbook my_playbook.yml -i inventories/dev/hosts
# or 
ansible-playbook my_playbook.yml -i inventories/prod/hosts