Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible - Conditionally set volume and tls hostname based on inventory file in docker module

I'm using Ansible to deploy my containers to production. During development, I'd like to have Ansible deploy all containers on my localhost instead of the production servers.

Production servers run on Ubuntu, localhost is OS X with boot2docker.

I have 2 inventory files, production and localhost. My dir structure looks like this:

.
|--inventories
|  |localhost
|  |production
|
|--group_vars
|  |all
|  |localhost
|  |production
|
|--roles
|  |--web
|     |--tasks
         |main.yml
|
|web.yml 

web.yml just defines the host group and assigns the web role.

/roles/web/tasks/main.yml looks like this:

- name: Run web container
  docker:
    name: web
    image: some/image
    state: reloaded
    ...
    tls_hostname: boot2docker
    volumes:
      - "/data/db:/data/db"
    env:
      ...
  tags:
    - ...

I need to set tls_hostname conditionally, only if the localhost inventory was used; likewise, I want to set the volume only if the production inventory file was used.

Very new to Ansible - it seems like I'm not approaching this to right way there's an easier way to do this? I want to avoid creating completely separate tasks to deploy locally; I just need a way to define volume and tls_hostname conditionally (and leave it at default setting otherwise)

like image 393
Samir Said Avatar asked Mar 15 '23 09:03

Samir Said


2 Answers

As of Ansible 1.8, you can omit variables and module parameters using the default filter with the special omit value. For example,

- name: Run web container
  docker:
    name: web
    image: some/image
    state: reloaded
    ...
    tls_hostname: "{{ hostname | default('some default value') }}"
    volumes: "{{ volumes | default(omit) }}"
    env:
      ...
  tags:
    - ...
like image 71
Chin Huang Avatar answered Apr 19 '23 12:04

Chin Huang


I see you do have group_vars files for localhost and production. Are you familiar with that concept? Because I think this is what you're looking for.

The variables defined in the group_vars section will be applied if the host belongs to the respective group.

I need to set tls_hostname conditionally, only if the localhost inventory was used; likewise, I want to set the volume only if the production inventory file was used.

So that sounds like you want to define tls_hostname in ./group_vars/localhost and volume in ./group_vars/production.

(and leave it at default setting otherwise)

Default values can be stored in several places. If you have a role you can store in <role>/defaults/main.yml. group_vars/all also is possible. As well you can set a default value in your yml's.

- name: Run web container
  docker:
    name: web
    image: some/image
    state: reloaded
    ...
    tls_hostname: "{{ hostname | default('some default value') }}"
    volumes: "{{ volumes | default(['/data/db:/data/db']) }}"
    env:
      ...
  tags:
    - ...

If hostname or volumes are not defined Ansible will fall back to the defined default value.

like image 27
udondan Avatar answered Apr 19 '23 12:04

udondan