简体   繁体   English

使用 Ansible 在 Docker 容器内运行命令

[英]Run Command Inside of Docker Container Using Ansible

what I'm trying to accomplish is to run commands inside of a Docker container that has already been created on a Digital Ocean Ubuntu/Docker Droplet using Ansible.我想要完成的是在已经使用 Ansible 在 Digital Ocean Ubuntu/Docker Droplet 上创建的 Docker 容器内运行命令。

Can't seem to find anything on this, or I'm majorly missing something.似乎无法找到任何关于此的内容,或者我主要遗漏了一些东西。 This is my Ansible task in my play book.这是我在剧本中的 Ansible 任务。 I'm very new to Ansible so any advice or wisdom would be greatly appreciated.我对 Ansible 很陌生,所以任何建议或智慧将不胜感激。

- name: Test Deploy
    hosts: [my-cluster-of-servers]

tasks: 
  - name: Go Into Docker Container And Run Multiple Commands
    docker:
      name: [container-name]
      image: [image-ive-created-container-with-on-server]
      state: present
      command: docker exec -it [container-name] bash

After discussion with some very helpful developers on the ansible github project , a better way to do this is like so:在与ansible github 项目上的一些非常有帮助的开发人员讨论后,更好的方法是这样的:

- name: add container to inventory
  add_host:
    name: [container-name]
    ansible_connection: docker
  changed_when: false

- name: run command in container
  delegate_to: [container-name]
  raw: bash

If you have python installed in your image, you can use the command module or any other module instead of raw.如果您的映像中安装了 python,则可以使用命令模块或任何其他模块而不是原始模块。

If you want to do this on a remote docker host, add:如果要在远程 docker 主机上执行此操作,请添加:

ansible_docker_extra_args: "-H=tcp://[docker-host]:[api port]"

to the add_host block.到 add_host 块。

See the Ansible documentation for a more complete example.有关更完整的示例,请参阅Ansible 文档

You should be able to execute a script (with your sequence of command in it) with docker exec :您应该能够使用docker exec执行脚本(其中包含您的命令序列):

docker exec container-name bash -l -c /path/to/script > /path/to/log

(See also " Why do I have to use bash -l -c inside my container? ") (另请参阅“为什么我必须在容器内使用bash -l -c ”)

  • /path/to/script should be accessible by your Ansible process.您的 Ansible 进程应该可以访问/path/to/script
  • /path/to/log is a path inside the container, that could be shared in a volume. /path/to/log是容器内的路径,可以在卷中共享。

You can run commands within docker containers using the command module For example this code will execute echo "Hello remote machine" within my_container on the remote machine:您可以使用命令模块docker容器中运行命令例如,此代码将在echo "Hello remote machine"上的my_container 中执行echo "Hello remote machine"

   tasks:
        - name: Execute commands in docker container
          command: docker exec -it my_container bash -c 'echo "Hello remote machine"'

For running the same command within the local machine, just use the local_action flag:要在本地机器中运行相同的命令,只需使用local_action标志:

   tasks:
        - name: Execute commands in docker container
          local_action: command docker exec -it my_container bash -c 'echo "Hello local machine"'

Since Ansible 2.10 docker_container_exec is part of the community.docker collection:由于Ansible 2.10 docker_container_execcommunity.docker集合的一部分:

- name: Run a simple command (command)
  community.docker.docker_container_exec:
    container: foo
    command: /bin/bash -c "ls -lah"
    chdir: /root
  register: result

- name: Print stdout
  debug:
    var: result.stdout

Update: there is a way to do this without using my module, see my other answer更新:有一种方法可以在不使用我的模块的情况下执行此操作,请参阅我的其他答案

I wrote a simple module to run exec on a remote Docker host.我编写了一个简单的模块来在远程 Docker 主机上运行 exec。 I've submitted it to the ansible project , but you can easily add it to your own projects if you need to.我已将其提交给 ansible 项目,但如果需要,您可以轻松地将其添加到您自己的项目中。 The module is only 23 lines long, take it from my pull request and add it to your ./library directory, and then you can add a task in your playbook like so:该模块只有 23 行长,从我的拉取请求中取出并将其添加到您的 ./library 目录,然后您可以在您的剧本中添加一个任务,如下所示:

  - name: Run docker exec command
    docker_exec: 
      command: <some command>
      docker_host: <docker host>
      name: <container name>
    register: exec_output

  - name: Show exec output
    debug: msg="{{ exec_output.result }}"

Ended up doing something like that:最终做了这样的事情:

- name: execute command in docker
  shell: |
    docker exec container sh -l -c "cat /tmp/secret"
  register: hello

- debug: msg="{{ hello.stdout }}"

To run command inside docker container using latest versions of Ansible:要使用最新版本的 Ansible 在 docker 容器内运行命令:

  • install community.docker collection using:使用以下命令安装community.docker 集合

ansible-galaxy collection install community.docker

or better by creating file requirements.yml in the project with contents:或者通过在包含内容的项目中创建文件requirements.yml更好:

---
collections:
    - community.docker

and running ansible-galaxy install -r requirements.yml (similar to pip install -r requirements.txt )并运行ansible-galaxy install -r requirements.yml (类似于pip install -r requirements.txt

  • use the collection in the following way:按以下方式使用集合:
---
- name: local actions 
  hosts: localhost
  gather_facts: false

  tasks:
  - name: task
    community.docker.docker_container_exec: 
      container: container_name
      command: ls /tmp

More about working with collections here有关在此处使用集合的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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