Run Command Inside of Docker Container Using Ansible

前端 未结 5 732
情话喂你
情话喂你 2020-12-07 16:48

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.

5条回答
  •  难免孤独
    2020-12-07 17:28

    After discussion with some very helpful developers on the ansible github project, a better way to do this is like so:

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

    If you want to do this on a remote docker host, add:

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

    to the add_host block.

    See the Ansible documentation for a more complete example.

提交回复
热议问题