首页 > 解决方案 > 如何从 Ansible 运行 shell 别名 docker 命令?

问题描述

一位客户有一个运行本地操作的剧本,在这种情况下,是针对 RDS 端点的 sqlplus 命令。

 shell: "echo exit|sqlplus64 '{{db_pnyx_username}}/{{db_pnyx_password}}@//{{ host }}:{{ port }}/{{db_pnyx_service_name}}' @vhosts.sql"

当我运行 Ubuntu 时,我很好奇如何避免由于 sqlplus 包而安装如此多的包依赖项,所以我决定尝试以容器方式。

 - name : Execute SQL 
    shell: "echo exit|docker run --rm  --net=host -v $PWD:/tmp rubendob/sqlplus:dockerplus '{{db_pnyx_username}}/{{db_pnyx_password}}@//{{ host }}:{{ port }}/{{db_pnyx_service_name}}' @/tmp/vhosts.sql"
    register: result

它奏效了!

但我还想使用我的 .profile 文件中的别名快捷方式运行 docker,如下所示:

alias sqlplus64='docker run --rm --net=host -v $PWD:/tmp rubendob/sqlplus:dockerplus'

Ansible 总是返回错误:

"stderr": "/bin/sh: 1: sqlplus64: not found", "stderr_lines": ["/bin/sh: 1: sqlplus64: not found"], "stdout": "", "stdout_lines": []}

有没有办法根据最佳实践实现这一目标?我敢打赌我在这里做了很多坏事:(

标签: dockeransible

解决方案


推荐阅读