Answers: <p>The best way is of course (like Zeitounator comments) to use a inventory var. Either you use the version number in the docker-compose task as a variable or you create a Jinja2 template docker-compose.yml.j2 and use the variable there. That's what I do.</p>
  
  <p>Example:</p>
  
  <pre><code>all:
    hosts:
      host1.prod:
        image: "xyz/12"
      host2.dev:
        image: "xyz/latest"
  </code></pre>
  
  <p>And the <code>docker-compose.yml.j2</code> contains the variable:</p>
  
  <pre><code>version: '2.1'
  
  services:
    myservice:
      image: "{{ image }}"
      ...
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2020-02-05T15:54:24.857Z
Answers: 2020-02-05T15:54:24.857Z
Answers: TRW
Body: <p>I planned to use <em>docker-compose</em> and a few scripts to manage the deployment of my app on staging and production.
  My idea was to use one <code>docker-compose.yml</code> with one <code>.env</code> file for each environment. In those <code>.env</code> files I would have set some specific values and the version of the different images that compose my application.</p>
  
  <p>But then I discovered <em>Ansible</em> and it looks like my few scripts powered by a community of experts. So I started to investigate it and I would like to use it. </p>
  
  <p>Based on the best practices, I can easily imagine having a standard directory layout[1] with the one inventory for each environment. And a couple of roles. </p>
  
  <p>However, I have no idea how to manage the versions; how to describe which version is executed in one environment?</p>
  
  <p>Ideally, I would like to find a simple solution where my CI can update this version.</p>
  
  <p>Thanks</p>
  
  <p>[1] <a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html#directory-layout" rel="nofollow noreferrer">https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html#directory-layout</a></p>
  
Comments: CC BY-SA 4.0
Comments: 2020-01-31T23:34:46.753Z
Comments: You mentionned having one inventory per environment: simply put the version as a var in your inventory: https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html
Comments: Zeitounator
Comments: CC BY-SA 4.0
Comments: 2020-02-03T08:18:49.387Z
Comments: That was my first idea. But will the var be available for docker-compose ?
Comments: gervais.b
Tags: <ansible><docker-compose><environments><versioning>
Title: How to manage images versions via docker-compose with Ansible
UserDisplayName: gervais.b
ThreadText: # How to manage images versions via docker-compose with Ansible
  
  (Asked by: username_0 on 2020-01-31)
  
  I planned to use *docker-compose* and a few scripts to manage the deployment of my app on staging and production.
  My idea was to use one `docker-compose.yml` with one `.env` file for each environment. In those `.env` files I would have set some specific values and the version of the different images that compose my application.
  
  But then I discovered *Ansible* and it looks like my few scripts powered by a community of experts. So I started to investigate it and I would like to use it.
  
  Based on the best practices, I can easily imagine having a standard directory layout[1] with the one inventory for each environment. And a couple of roles.
  
  However, I have no idea how to manage the versions; how to describe which version is executed in one environment?
  
  Ideally, I would like to find a simple solution where my CI can update this version.
  
  Thanks
  
  [1] <https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html#directory-layout>
  
  **Tags:** ansible, docker-compose, environments, versioning
  
  > **Comments:**
  > **username_1**: You mentionned having one inventory per environment: simply put the version as a var in your inventory: https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html  
  > **username_0**: That was my first idea. But will the var be available for docker-compose ?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2020-02-05. Score: 1 | Accepted answer
  
  The best way is of course (like username_1 comments) to use a inventory var. Either you use the version number in the docker-compose task as a variable or you create a Jinja2 template docker-compose.yml.j2 and use the variable there. That's what I do.
  
  Example:
  
  ```
  all:
    hosts:
      host1.prod:
        image: "xyz/12"
      host2.dev:
        image: "xyz/latest"
  
  ```
  
  And the `docker-compose.yml.j2` contains the variable:
  
  ```
  version: '2.1'
  
  services:
    myservice:
      image: "{{ image }}"
      ...
  
  ```
  
  
  ---
  
  