How to use different variables file between prod, dev1, dev2, etc?

24 views
Skip to first unread message

Jeffrey Wen

unread,
Nov 29, 2016, 3:06:24 PM11/29/16
to Ansible Project
Here is what my current `ansible/` directory looks like:

├── development
  └── hosts
├── production
  └── hosts
├── group_vars
  └── prod.example.com
  └── dev1.example.com
  └── dev2.example.com
├── playbooks
  ├── prod.example.com.com
    └── site.yml
  ├── dev1.example.com.com
    └── site.yml
  ├── dev2.example.com.com
    └── site.yml
└── roles
   
├── webservers
   
    ├── templates
   
      └── main.yml
   
    ├── tasks
       
  └── main.yml
       
└── vars
           
└── main.yml

In my `roles/webservers/templates/main.yml`, I would reference variables such as
  1. `http_port`
  2. `document_root`
  3. `server_name`
I can easily reference them in my `roles/webservers/templates/main.yml` by typing the following into my template:
  1. `{{ http_port }}`
  2. `{{ document_root }}`
  3. `{{ server_name }}`
I know when I type it like the above, it references my `roles/webservers/vars/main.yml` file; which contain variables for production.

I would like to change the 3 variables if I am referencing my `dev1.example.com` and `dev2.example.com` environments, but I am unsure on how to do this with using `group_vars/dev1.example.com` and `group_vars/dev2.example.com`

Brian Coca

unread,
Nov 29, 2016, 3:11:58 PM11/29/16
to ansible...@googlegroups.com
Move from vars/main.yml to default/main.yml then the group_vars will be able to override the role vars.



----------
Brian Coca

Jeffrey Wen

unread,
Nov 29, 2016, 3:15:33 PM11/29/16
to Ansible Project
Hi Brian,

If I change it to `defaults/main.yml`, how would I reference my `group_vars/dev1.example.com` variables file?

Jeffrey Wen

unread,
Nov 29, 2016, 3:37:44 PM11/29/16
to Ansible Project
I actually added in the variables into my development/hosts file like so:

[dev1]

[dev1:vars]
http_port=80
server_name=dev1.example.com
document_root=/var/www/dev1.example.com

[dev2]

[dev2:vars]
http_port=80
server_name=dev2.example.com
document_root=/var/www/dev2.example.com

Do you believe this is an efficient/proper way of having specific variables per node/environment?

Brian Coca

unread,
Nov 29, 2016, 3:41:38 PM11/29/16
to ansible...@googlegroups.com
either in inventory file or in host_vars/, both work

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/608c1728-c011-4045-a863-69932669e100%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages