Chr*_*isP 0 linux maven ansible
我正在尝试从 Ansible Tower 运行一个 Maven 项目,但是 Ansible kepps 给了我一个“mvn:命令无法识别”错误,如果我从 Linux 服务器上的命令行手动执行,mvn 运行良好
我在尝试从 Ansible 运行 Gradle 时遇到了类似的问题,但是解决方案在这里不起作用。以前,问题是我在运行 playbook 时需要设置 env 变量。我在这里尝试过,但无济于事。我检查了我的路径,可以在那里看到 Maven 路径。
我接受了 Zeitounator 的建议,并尝试使用绝对路径从 Ansible 进行调用,这奏效了。但是,正如他所提到的,这只是一个测试,而不是一个实际的解决方案。
Ansible 似乎不知道在哪里寻找 Maven,我不确定如何将这些信息传达给它。我觉得这是 Ansible 的一个愚蠢的怪癖,它有愚蠢的简单解决方案,如果我有除 'mvn: command not found' 以外的任何其他东西可以解决这个问题。
这是我的剧本中的电话:
- name: test mvn
shell: mvn -version
args:
chdir: /opt/jenkins/batchtest/
environment:
MAVEN_HOME: /opt/maven/
JAVA_HOME: /usr/java/jdk1.8.0_162
Run Code Online (Sandbox Code Playgroud)
这是获取路径的结果:
-bash-4.2$ echo $PATH
/bin:/usr/lib64/qt-3.3/bin:/opt/maven/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
Run Code Online (Sandbox Code Playgroud)
这是 mvn -version 的输出,以防这里有我遗漏的东西
-bash-4.2$ mvn -version
Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-24T13:49:05-06:00)
Maven home: /opt/maven
Java version: 1.8.0_202, vendor: Oracle Corporation
Java home: /usr/java/jre1.8.0_202-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-957.12.1.el7.x86_64", arch: "amd64", family: "unix"
Run Code Online (Sandbox Code Playgroud)
You are launching your test commands above directly on the server with the bash shell.
Ansible shell module uses the sh shell by default (see documentation)
I have to take a guess here but I'm 99.9% sure that the maven bin path is added to your PATH variable in a bash specific init file, so it is not available when you are using sh.
You now have 2 solutions:
PATH so that both bash and sh pick the value. For system-wide setting, you can use /etc/profile. For user specific, using ~/.profile should do the trick.- name: test mvn
shell: mvn -version
args:
chdir: /opt/jenkins/batchtest/
executable: /bin/bash
environment:
MAVEN_HOME: /opt/maven/
JAVA_HOME: /usr/java/jdk1.8.0_162
Run Code Online (Sandbox Code Playgroud)