ChrisP
ChrisP

Reputation: 137

Ansible doesn't recognize mvn command, but Maven is installed

I'm trying to run a maven project from Ansible Tower, but Ansible kepps giving me a 'mvn: command not recognized' error, mvn runs fine if I do it manually from command line on a Linux server

I had a similar issue trying to run Gradle from Ansible, however the solution for that did not work here. Previously, the issue was that I needed to set env variables when I ran the playbook. I have tried that here, but to no avail. I checked my path and can see the Maven path there.

I took Zeitounator's advice and tried to make the call from Ansible using the absolute path, and that worked. But, as he mentioned, this was just a test and not an actual solution.

It seems that Ansible has no idea where to look for Maven and I'm not really sure how else to convey that information to it. I feel like this is a silly quirk of Ansible that has stupid easy solution, and that could be fixed if I had anything other than 'mvn: command not found' to work with.

Here is the call from my playbook:

- name: test mvn
    shell: mvn -version
    args:
      chdir: /opt/jenkins/batchtest/
    environment:
      MAVEN_HOME: /opt/maven/
      JAVA_HOME: /usr/java/jdk1.8.0_162

and here is the result of getting the PATH:

-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

Here is the output from mvn -version incase there's something here that I'm missing

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

Upvotes: 0

Views: 2080

Answers (1)

Zeitounator
Zeitounator

Reputation: 44760

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:

  1. Change the way you declare your 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.
  2. Change the shell ansible is using to bash so that your are in the exact same situation
- 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

Upvotes: 6

Related Questions