使用ansible docker_container模块获取容器的命令输出

问题描述 投票:1回答:2

我想用ansible docker_container生成Influxdb配置文件。其行为与docker命令相同

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

我在我的剧本上创建了一个任务,如下所示

  - 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"

当我检查输出路径/opt/influxdb/config时,它是空的。我如何获得influxd config输出?

ansible
2个回答
1
投票

你可以执行/bin/sh作为命令,你也可以使用detachcleanup

  - 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

0
投票

您可以注册模块并使用ansible_facts来获取下一个任务中的日志,如下所示

- 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 }}"
© www.soinside.com 2019 - 2024. All rights reserved.