简体   繁体   中英

Ansible validate docker-compose with env_file and send to host

I created a role that has in template folder two files: docker-compose.yml.j2 and env.j2

env.j2 is used in docker-compose file:

version: "2"

services:
  service_name:
    image: {{ IMAGE | mandatory }}
    container_name: service_name
    mem_limit: 256m
    user: "2001"
    env_file: ".env"

Now my question: is there some ansible module that sends docker-compose file to host and there validate it because than env and docker-compose are in same folder on host machine?

This example of ansible task return error because env file is not in template folder but on host.

- name: "Copy env file"
  ansible.builtin.template:
    src: "env.j2"
    dest: "/opt/db_backup/.env"
    mode: '770'
    owner: deployment
    group: deployment

- name: "Validate and copy docker compose file"
  ansible.builtin.template:
    src: "docker-compose.yml.j2"
    dest: "/opt/db_backup/docker-compose.yml"
    mode: '770'
    owner: deployment
    group: deployment
    validate: docker-compose -f %s config

This probably falls into the Complex validation configuration cases linked in the documentation for the template module validate parameter

In any case, unless refactoring completely your current file and passing more variables in your environment (eg to allow .env being in a location out of the current directory), you cannot validate docker-compose.yml until both files are in the same location.

An easy scenario would be to copy both files in place, validate prior to doing anything with them and roll back to the previous version in case of error. The below example is far from rocket proof but will give you an idea:

---
- hosts: localhost
  gather_facts: false

  vars:
    IMAGE: alpine:latest
    deploy_dir: /tmp/validate_compose

  tasks:
    - name: "make sure {{ deploy_dir }} directory exits"
      file:
        path: "{{ deploy_dir }}"
        state: directory

    - name: copy project file templates
      template:
        src: "{{ item }}"
        dest: "{{ deploy_dir }}/{{ item | regex_replace('^(.*)\\.j2', '\\g<1>') }}"
        mode: 0640
        backup: true
      loop:
        - .env.j2
        - docker-compose.yml.j2
      register: copy_files

    - block:
        - name: check docker-compose file validity
          command:
            cmd: docker-compose config
            chdir: "{{ deploy_dir }}"
      rescue:
        - name: rollback configuration to previous version for changed files
          copy:
            src: "{{ item.backup_file }}"
            dest: "{{ item.dest }}"
            remote_src: true
          loop: "{{ copy_files.results | selectattr('backup_file', 'defined') }}"

        - name: Give some info about error.
          debug:
            msg:
              - The compose file did not validate.
              - Please see previous error above for details
              - Files have been rolled back to the latest known version.

        - name: Fail
          fail:

    - name: Rest of the playbook using the above validated files
      debug:
        msg: Next tasks...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM