r/ansible•
bachya

Ansible and Docker stacks

Curious to get opinions on a best practice for deploying Docker-based stacks via Ansible.

Currently, I have a Git repo for each host that contains a docker-compose.yml, which defines the containers I want running on that host. Pre-Ansible, I would log into each host, pull the latest, and run docker-compose up -d. Now that I’m managing my hosts via a series of playbooks, I’m pondering the best way to deploy these stacks. I see two ideas:

  1. Keep the stack configuration in its own repo. In the playbook, focus on pulling the latest repo and spinning it up (using ansible.builtin.git and community.general.docker_compose, respectively).
  2. Take the stack information in each repo and translate it to a series of tasks in each playbook (using community.general.docker_container).

I’m currently doing #1 because there’s a nice separation of concerns between stack configuration and playbook; if I ever need to manipulate the stack directly, I can log into the host without Ansible.

That said, the other side of my brain likes the idea of a single source of truth for the entire infrastructure, configuration, and stack. In that case, putting everything into a single playbook sounds appetizing.

I’ve been chewing on this for quite a while and haven’t had any revelations, so I’m reaching out to you all for inspiration. Thanks!

Posted Feb 2, 2021, 5:50 PM UTC · edited

157
hgevoyio•

I’d keep them separate. Docker is docker and ansible is just a way of deploying your ‘stack’.

2
edthezombie•

I think the old train of thought was to keep them separate so you don't have a bunch of code mixed together. But I kind of agree with the new train of thought that all code related to something should exist in the same repo.

I see some people with the argument that then you don't have as much code re-use, but if you have code that you want re-used, it should be a role in it's own repo.

That's just kind of the trends I've seen.

2

Just for the love of god stick to one way of deploying containers. I am stuck with a bunch of legacy code where we deploy containers (such as node_exporter, cadvisor, consul etc) that are part of the 'platform' using ansible (and having systemd manage the running of them), the application on top is deployed using mesos/marathon. It's a pain. Thank god we're in the process of yeeting everything on kubernetes so we can just run everything the same way and just separate the stuff using namespaces etc.

Word of warning, one does not "just use kubernetes brah" if you do not have a lot of containers and a lot of nodes . Do not bother. Shit's complicated.

1
edthezombie•

Tbh it's the same concept of using vCenter/vSphere for VMs. It's a scale issue. If you have a few containers running and don't need to scale to some crazy amount, running a container with only Docker is honestly fine. But if you have to handle production workloads then K8s starts to make more sense. Same with vCenter/vSphere, if you literally only care to virtualize some servers, you can just use ESXi, but if you want to have hundreds of VMs and be able to take hosts on and offline without downtime, then you really need vCenter/vSphere.

Unfortunately, our industry is buzzword city and K8s is the new buzzword. So even if you're just running one or two containers, everyone will say run it in K8s lol

1
Genesis2001•

My solution is both and neither...sorta. I prefer megarepos of infrastructure code, because it's simpler for me to deploy. I'm also using swarm, but the same can be ported over to compose if you have non-clustered servers. Compose is probably easier for this, due to storage concerns if you have need of volumes for your stacks (storage is one problem I'm currently trying to solve in Swarm).

- hosts: my-swarm-manager-nodes[0]
  tasks:
    - name: deploy services to swarm
      community.general.docker_stack:
        name: "{{ item.name }}"
        state: present
        compose:
          - "{{ item.compose }}"
      with_items: "{{ my_services }}"
      when: my_services is defined
  vars:
    my_services:
      - name: some_stack:
        compose: "{{ lookup('file', '../../docker/some_stack.yml') | from_yaml }}"
1
bachyaOP•

Appreciate the ideas!

FYI, I've done Swarm storage by plugging a drive into one of the nodes, exposing it to the others as an NFS share, and setting my volumes to point to that NFS share. Works well and can obviously be set up via Ansible. 👍

1
zHevoGuy•

Just to be clear, these are not stacks, they are just compose services. Stacks has its own meaning in docker world

-1