要使用Ansible执行一个命令shell bash到docker容器或卷,可以使用Ansible的docker_container或docker_volume模块。以下是一个使用docker_container模块的示例:
---
- name: Execute command in Docker container
hosts: localhost
tasks:
- name: Create a Docker container
docker_container:
name: mycontainer
image: myimage
state: started
- name: Execute command in Docker container
command: docker exec -it mycontainer bash -c "your-command"
register: command_output
- name: Print command output
debug:
var: command_output.stdout_lines
在上面的示例中,我们首先使用docker_container模块创建了一个名为mycontainer的容器。然后,我们使用command模块执行了一个命令docker exec -it mycontainer bash -c "your-command",将命令的输出保存在command_output变量中。最后,我们使用debug模块打印了命令的输出。
如果你要执行的命令是针对docker卷而不是容器,可以使用docker_volume模块。以下是一个使用docker_volume模块的示例:
---
- name: Execute command in Docker volume
hosts: localhost
tasks:
- name: Create a Docker volume
docker_volume:
name: myvolume
state: present
- name: Execute command in Docker volume
command: docker run -v myvolume:/path/to/volume your-image your-command
register: command_output
- name: Print command output
debug:
var: command_output.stdout_lines
在上面的示例中,我们首先使用docker_volume模块创建了一个名为myvolume的卷。然后,我们使用command模块执行了一个命令docker run -v myvolume:/path/to/volume your-image your-command,将命令的输出保存在command_output变量中。最后,我们使用debug模块打印了命令的输出。
请注意,在执行这些示例之前,确保已经安装了Docker并正确配置了Ansible的Docker插件。
上一篇:Ansible执行任务时出现“致命错误:[test1]:FAILED!=>{"msg":"超时(62秒)等待提升权限提示:"}”的错误信息。
下一篇:Ansible执行“"ssh-keygen-Ripaddress"”命令时会阻塞,如何解决?