2

I am writing an Ansible playbook to deploy services to a clustered environment. There are 3 types of hosts (Application, Load balancer, database). Services are deployed via docker-compose on each host separately.

Example of playbook to deploy to a database host.

- name:           Deploy database node.
  hosts:          databases
  become:         yes
  become_method:  sudo

  roles:
  - common
  - zabbix-agent
  - database
  - backup

The Ansible play needs to dynamically generate a docker-compose file based on which roles have been invoked on that host. So this example would need to generate a docker-compose which lists these roles.

How do i programmatically generate a docker-compose.yml file, based on which roles have been invoked?

I have considered:

  • Ansible facts
  • Tags
  • global boolean variables [yuck] but none of these seems like the right way to me.

If there is a better approach to achieving my objective I'm open to any other suggestions too.

Edit:

Can you explain what is in the container here?

'common' role currently installs Docker and docker-compose onto the target host. 'database' starts a mariadb container. 'zabbix-agent' installs..well, zabbix agent on the host. 'backup' copies some shell scripts over.

Is the database in the container, or is the container running next to the database?

the database is a mariadb Docker container. So my compose file will need to include a 'mariadb' section.

What would a good docker-compose.yml look like for you?

Good question. Something like this:

version:                            '2.2'
services:

  terracotta:
    image:                          terracotta/terracotta-server-oss:5.5.1
    container_name:                 terracotta
    restart:                        always
    networks:
      - skyNet
    logging:
      driver:                       "json-file"
      options:
        max-size:                   "10m"
        max-file:                   "5"

  mariadb:
    image:                          mariadb/server:{{docker_mariadb_version}}
    container_name:                 mariadb
    restart:                        always        
    logging:
      driver:                       "json-file"
      options:
        max-size:                   "10m"
        max-file:                   "5"
    networks:
      - skyNet
    ports:
    - 127.0.0.1:3306:3306/tcp
    volumes:
      - /opt/mariadb/data:/var/lib/mysql
    environment:
      MARIADB_ROOT_PASSWORD:        "{{database_root_password}}"


# Network
networks:
  skyNet:
    driver:                         bridge

I pretty much need to be able to need to be able to toggle which services are added to the compose file via Tags or something. Maybe i should change the phrasing of my question?

1
  • 4
    Can you explain what is in the container here? Is the database in the container, or is the container running next to the database? What would a good docker-compose.yml look like for you? Oct 31 '19 at 13:08