CSJ
CSJ

Reputation: 2957

get command output of container using ansible docker_container module

I'd like using ansible docker_container to generate influxdb config file. which will act the same as below docker command

docker run --rm influxdb influxd config > /opt/influxdb/config/influxdb.conf

I created a task on my playbook as below

  - name: generate influxdb config using temporary container
    docker_container:
      name: influxdb_conf
      state: present
      image: influxdb:1.4
      command: influxd config > /tmp/influxdb.conf
      volumes:
        - "/opt/influxdb/config:/tmp"

when I check the output path /opt/influxdb/config, it is empty. how can I get the influxd config output?

Upvotes: 2

Views: 3081

Answers (2)

Mahattam
Mahattam

Reputation: 5743

You can register the module and use ansible_facts to get the logs in next task as below

- name: generate influxdb config using temporary container
  docker_container:
    name: influxdb_conf
    state: present
    image: influxdb:1.4
    command: influxd config > /tmp/influxdb.conf
    volumes:
      - "/opt/influxdb/config:/tmp"
  register: docker

- name: display logs
  debug:
    msg:  "{{ docker.ansible_facts.docker_container.Output }}"

Upvotes: 2

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68239

You can execute /bin/sh as command and also you may want to use detach and cleanup:

  - name: generate influxdb config using temporary container
    docker_container:
      name: influxdb_conf
      state: started
      image: influxdb:1.4
      command: "/bin/sh -c 'influxd config > /tmp/influxdb.conf'"
      detach: no
      interactive: yes
      tty: yes
      recreate: yes
      cleanup: yes
      volumes:
        - /opt/influxdb/config:/tmp

Upvotes: 2

Related Questions