Ansible - conditionally specified volume and host name tls based on the inventory file in the docker module

I use Ansible to deploy my containers for production. During development, I would like Ansible to deploy all containers on my localhost instead of production servers.

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

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

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

web.yml simply defines a group of hosts and assigns a role web.

/roles/web/tasks/main.yml is as follows:

- 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 install tls_hostnameconditionally only if the localhost inventory was used; Similarly, I want to install the volume only if a production inventory file has been used.

Ansible - , , ? ; tls_hostname ( )

+4
2

Ansible 1.8, omit. ,

- 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:
    - ...
+5

, group_vars localhost production. ? , , .

, group_vars, , .

tls_hostname , localhost; , , .

, , tls_hostname ./group_vars/localhost volume ./group_vars/production.

( )

. , <role>/defaults/main.yml. group_vars/all . yml.

- 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:
    - ...

hostname volumes , Ansible .

+2

Source: https://habr.com/ru/post/1598485/


All Articles