运行 docker Ansible 容器中的复杂 linux 命令
Run a complex linux command in a docker Ansible container
找了半天没找到答案
有一个 Ansible 剧本。
- name: myscript
hosts: myhost
tasks:
- name: myscript
docker_container:
name: myscript
image: myimage
detach: false
working_dir: "/opt/R/project"
command: Rscript $(find ./*_Modules -iname *_Script.R)
此命令有效:Rscript ./01_Modules/02_Script.R
此命令无效:Rscript $(find ./*_Modules -iname *_Script.R)
- 不将 $(find
视为命令,而是路径。
同时,在linux,这一行成功运行,找到脚本
如何将具有 &&
和类似功能的完整 linux 命令传递给命令?
这是您的问题的简化版本
- name: Create a test container
docker_container:
name: test
image: busybox
command: ls |grep var && echo 'it doesn\'t work!'
输出:
ls: |grep: No such file or directory
ls: &&: No such file or directory
ls: echo: No such file or directory
ls: it fails: No such file or directory
var:
spool
www
如果我用引号括起来并使用
/bin/sh -c
- name: Create a test container
docker_container:
name: test
image: busybox
command: /bin/sh -c "ls |grep var && echo 'it works!'"
输出:
var
it works!
找了半天没找到答案
有一个 Ansible 剧本。
- name: myscript
hosts: myhost
tasks:
- name: myscript
docker_container:
name: myscript
image: myimage
detach: false
working_dir: "/opt/R/project"
command: Rscript $(find ./*_Modules -iname *_Script.R)
此命令有效:Rscript ./01_Modules/02_Script.R
此命令无效:Rscript $(find ./*_Modules -iname *_Script.R)
- 不将 $(find
视为命令,而是路径。
同时,在linux,这一行成功运行,找到脚本
如何将具有 &&
和类似功能的完整 linux 命令传递给命令?
这是您的问题的简化版本
- name: Create a test container
docker_container:
name: test
image: busybox
command: ls |grep var && echo 'it doesn\'t work!'
输出:
ls: |grep: No such file or directory
ls: &&: No such file or directory
ls: echo: No such file or directory
ls: it fails: No such file or directory
var:
spool
www
如果我用引号括起来并使用
/bin/sh -c
- name: Create a test container
docker_container:
name: test
image: busybox
command: /bin/sh -c "ls |grep var && echo 'it works!'"
输出:
var
it works!