如何使用Ansible Playbook安装Maven?

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

那么,我应该如何安装maven?

3个回答

3
我认为你可以更新$PATH来链接下载的maven文件:
# 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

或者简单地从仓库中安装maven(如果您不需要非常特定的版本):

- 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

(注意: 在这里,您可以通过在 with_items 中添加项目来安装其他软件包。)


0

0

这是我使用Ansible安装Maven并设置环境变量的版本

---
- 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}'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接