Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tailor ansible roles to environment

Tags:

roles

ansible

I have a number of environments, that require a bunch of text files to be tailored in order for things like mule to speak to the right endpoints.

For this environment, this works:

ansible-playbook test03.yml

The only difference between an environment (from ansible's perspective) is the information held in ./roles/esb/vars/main.yml.

I've considered using svn to keep a vars/main.yml for each environment, so each time I need to configure an environment I check out roles and then vars/main.yml for that environment, before I run the command above.

To me, not an elegant solution. How can I do this better?

Directory structure

./test03.yml
./roles/esb/vars/main.yml
./roles/esb/tasks/main.yml
./roles/esb/templates/trp.properties.j2

./test03.yml

---
- hosts: test03-esb
  gather_facts: no
  roles:
    - esb

./roles/esb/vars/main.yml

---
  jndiProviderUrl: 'jnp://mqendpoint.company.com:1099'
  trp_endpoint_estask: 'http://tmfendpoint.company.com:8080/tmf/estask'
  trp_endpoint_builderQ: 'jnp://mqendpoint.company.com:1099'

./roles/esb/tasks/main.yml

---
  - name: replace variables in templates
    template: src=trp.properties.j2  dest=/path/to/mule/deploy/conf/trp.properties

./roles/esb/templates/trp.properties.j2

trp.endpoint.estask={{ trp_endpoint_estask }}
trp.endpoint.builderQ={{ trp_endpoint_builderQ }}
like image 794
Jepper Avatar asked Dec 30 '13 10:12

Jepper


People also ask

Can Ansible use environment variables?

Ansible Environment variables are used to set the environment variable for action on the remote host using environment keyword, which can be set at the playbook level or the task level and which doesn't affect the ansible configuration file or the environment set for the user and it doesn't include automatically to the ...

Where are Ansible roles stored?

Storing and finding roles in the configured roles_path. The default search path is ~/. ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles. in the directory where the playbook file is located.

How do you define roles in Ansible?

Roles provide a framework for fully independent, or interdependent collections of variables, tasks, files, templates, and modules. In Ansible, the role is the primary mechanism for breaking a playbook into multiple files. This simplifies writing complex playbooks, and it makes them easier to reuse.

Can Ansible roles be reused by playbooks in a different directory?

Ansible will look here when we want to use our new role in a playbook later in this tutorial. Within this directory we will define roles that can be reused across multiple playbooks and different servers. Each role that we will create requires its own directory.


1 Answers

In order to use specific values for different environments all you need to do is move your variables from the role itself to a variables file for that specific enrivonment e.g.

production
|- group_vars
  |- servers
|- inventory
staging
|- group_vars
  |- servers
|- inventory
development
|- group_vars
  |- servers
|- inventory
roles
|- esb
  |- tasks
     |- main.yml
  |- templates
     |- trp.properties.j2
etc.

Inside each of the environments' group_vars/servers you could specify variable specific for that environment, e.g.

$ cat production/group_vars/servers
---
jndiProviderUrl: 'jnp://mqendpoint.company.com:1099'
trp_endpoint_estask: 'http://tmfendpoint.company.com:8080/tmf/estask'
trp_endpoint_builderQ: 'jnp://mqendpoint.company.com:1099'

$ cat staging/group_vars/servers
---
jndiProviderUrl: 'jnp://staging.mqendpoint.company.com:1099'
trp_endpoint_estask: 'http://staging.tmfendpoint.company.com:8080/tmf/estask'
trp_endpoint_builderQ: 'jnp://staging.mqendpoint.company.com:1099'

$ cat development/group_vars/servers
---
jndiProviderUrl: 'jnp://dev.mqendpoint.company.com:1099'
trp_endpoint_estask: 'http://dev.tmfendpoint.company.com:8080/tmf/estask'
trp_endpoint_builderQ: 'jnp://dev.mqendpoint.company.com:1099'

The Jinja2 template can then remain the same (it doesn't care where the variables come from after all).

You would then execute:

# Provision production
$ ansible-playbook $playbook -i production/inventory

# Provision staging
$ ansible-playbook $playbook -i staging/inventory

# Provision development
$ ansible-playbook $playbook -i development/inventory
like image 160
jabclab Avatar answered Sep 30 '22 02:09

jabclab