Allen Liu
Allen Liu

Reputation: 51

How to use ansible playbook to install maven?

- name: Download Apache Maven
  get_url: url=http://apache.claz.org/maven/maven-3/3.1.1/binaries/apache-maven--bin.tar.gz dest=/tmp/apache-maven--bin.tar.gz

- name: Untar Maven
  shell: chdir=/tmp creates=/opt/apache-maven- tar -zxf apache-maven--bin.tar.gz -C /opt

Then, what should I do to install maven?

Upvotes: 5

Views: 13417

Answers (3)

Manu T G
Manu T G

Reputation: 1

here is my version of installing Maven ans setting environment variables using ansible

---
- hosts: BackEndServers

  tasks:
  - name: Update APT package manager repositories cache
    become: true
    apt:
      update_cache: yes

  - name: Download Apache Maven
    become: true
    get_url: url=https://mirrors.estointernet.in/apache/maven/maven-3/3.8.1/binaries/apache-maven-3.8.1-bin.tar.gz dest=/tmp/apache-maven-3.8.1-bin.tar.gz

  - name: Untar Maven
    become: true 
    shell: chdir=/tmp creates=/opt/apache-maven-3.8.1 tar -zxf apache-maven-3.8.1-bin.tar.gz -C /opt

  - name: Set MAVEN_HOME
    become: true
    lineinfile:
      dest: /etc/profile.d/maven.sh
      create: yes
      state: present
      mode: '0744'
      line: '{{ item }}'
    with_items:
      - 'export M2_HOME=/opt/apache-maven-3.8.1'
      - 'export MAVEN_HOME=/opt/apache-maven-3.8.1'
      - 'export PATH=${M2_HOME}/bin:${PATH}'

Upvotes: 0

davidmpaz
davidmpaz

Reputation: 1387

Now days you can rely on roles published in galaxy. Search there to check

For example this: https://galaxy.ansible.com/tecris/maven

Upvotes: 0

smagnan
smagnan

Reputation: 1267

I think you can either update the $PATH to link to the downloaded maven files:

# your two commands

- name: Download Apache Maven 
  get_url: url=http://apache.claz.org/maven/maven-3/3.1.1/binaries/apache-maven--bin.tar.gz dest=/tmp/apache-maven-3.1.1-bin.tar.gz

- name: Untar Maven 
  shell: chdir=/tmp creates=/opt/apache-maven-3.1.1 tar -zxf apache-maven-3.1.1-bin.tar.gz -C /opt

# What is missing

- name: Update path for maven use
  shell: export PATH=/opt/apache-maven-3.1.1/bin:$PATH

Or simply install maven (if you don't need a very specific version) from the depots:

- name: install maven (and other packages if needed)
  become: yes
  apt: pkg={{ item }} state=latest update_cache=yes cache_valid_time=3600
  with_items:
    - maven

(Note: here you can install other packages by adding items in the with_items)

Upvotes: 3

Related Questions