# How to define and use the environment (production/development)?

**URL:** <https://forum.ansible.com/t/how-to-define-and-use-the-environment-production-development/23575>\
**Category:** Ansible Project\
**Created:** [January 27, 2017, 1:46pm UTC](https://forum.ansible.com/t/how-to-define-and-use-the-environment-production-development/23575 "2017-01-27T13:46:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cev\_Ing](https://avatars.discourse-cdn.com/v4/letter/c/f05b48/32.png) [@Cev\_Ing](https://forum.ansible.com/u/Cev_Ing)\
**Post date:** [January 27, 2017, 1:46pm UTC](https://forum.ansible.com/t/how-to-define-and-use-the-environment-production-development/23575/1 "2017-01-27T13:46:12Z")

</div>

I have different environments and for each an inventory: production.ini and development.ini.

I have a users.yml containing user data:

users:

- username: foo  
uid: 1001  
gid: 1001  
pw\_hash: $6$…  
ssh\_key: ssh-rsa AAAA…

- username: bar  
uid: 1002  
gid: 1002  
pw\_hash: $6$…  
ssh\_key: ssh-rsa AAAA…

I use the users in a user task.

user:  
name: “{{ item.username }}”  
uid: “{{ item.uid }}”  
gid: “{{ item.gid }}”  
with\_items: “{{ users }}”

And I use the SSH keys in an authorized\_key task.

authorized\_key: user={{ item.username }} key={{ item.ssh\_key }}  
with\_items: “{{ users }}”

This works in the production end development environment.

Now I have the requirement to use different SSH keys for some but not all users in production and development.

How to get the different SSH keys in the users.yml without duplicating all the remaining data for each environment?

My first idea was to make the ssh\_key attribute a hash:

users:

- username: foo  
uid: 1001  
gid: 1001  
pw\_hash: $6$…  
ssh\_key:  
production: ssh-rsa AAAA…

development: ssh-rsa BBBB…

By this I can select the right key based on the environment:

authorized\_key: user={{ item.username }} key={{ item.ssh\_key[environment] | default(item.ssh\_key.production) }}  
with\_items: “{{ users }}”

But how to know in a playbook in which environment the playbook is executed?

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [January 27, 2017, 1:53pm UTC](https://forum.ansible.com/t/how-to-define-and-use-the-environment-production-development/23575/2 "2017-01-27T13:53:00Z")

</div>

> How to get the different SSH keys in the users.yml without  
> duplicating all the remaining data for each environment?

group\_vars would be another idea.

> But how to know in a playbook in which environment the playbook is  
> executed?

Setting a variable? If you set this variable inside your different  
variable files, then you could use this variable to get the right item  
out of the hash.

I.e. set 'foobar=production' in your production variable file, and  
then use the value of foobar to get the right hash.

Or am I missing a bit here?

Johannes

---

<div class="post-metadata">

**Author:** ![Cev\_Ing](https://avatars.discourse-cdn.com/v4/letter/c/f05b48/32.png) [@Cev\_Ing](https://forum.ansible.com/u/Cev_Ing)\
**Post date:** [January 27, 2017, 2:03pm UTC](https://forum.ansible.com/t/how-to-define-and-use-the-environment-production-development/23575/3 "2017-01-27T14:03:06Z")

</div>

I got the idea. It is explained here:

[https://www.digitalocean.com/community/tutorials/how-to-manage-multistage-environments-with-ansible](https://www.digitalocean.com/community/tutorials/how-to-manage-multistage-environments-with-ansible)
