setting environment variables

61 views
Skip to first unread message

danish09

unread,
Mar 20, 2018, 8:27:40 AM3/20/18
to Ansible Project
Hi,

I know the solution to this topic has been posted many times and I have tried searching through google and other forums including the ansible docs but unfortunately, nothing seems to be working for me. Please provide your feedback and help me with the solution. Also please let me know if you need any further information.

I am trying to create a playbook to download and install jenkins from a war file and so installing java from the source file as a pre-requisite.

I check for the presence of Java and if not found, go on to download and install java from source and then registering the env variable JAVA_HOME and adding the java binary onto my PATH. However, the environment variable part is frustratingly not working for me.

I initially started out with using the shell module to source /etc/profile using /bin/bash, but that didnt work. Read more and found out about environment keyword. As part of my playbook, I create a user jenkins_user and then download the jenkins war file and try to run with the above defined env variables.  unfortunately, it is not working. However, I am not sure I am doing this the correct way as I keep getting failures and the latest is that 'java' cannot be found by newly created jenkins_user.

Pasted below is all my code.

playbook.yaml


---
- hosts: all
  become
: yes
  tasks
:
   
- name: Do I need to install java
      shell
: java --version
     
register: result
      ignore_errors
: True


   
- include: tasks/install_java.yaml
     
when: result|failed



install_java.yaml



- name: Installing java and setting environment variables
     
  environment
:
    JAVA_HOME
: /usr/share/jdk-9.0.4/
    PATH
: $PATH:/usr/share/jdk-9.0.4/bin/


  get_url
:
    url
: https://download.java.net/java/GA/jdk9/9.0.4/binaries/openjdk-9.0.4_linux-x64_bin.tar.gz
    dest
: /usr/share/
    owner
: root
   
group: root
    mode
: 0644      
 
- name: unarchiving java source file
  unarchive
:
    src
: /usr/share/openjdk-9.0.4_linux-x64_bin.tar.gz
    dest
: /usr/share/
    owner
: root
   
group: root
    mode
: 0644


- name: changing permissions recursively
  file
:
    path
: /usr/share/jdk-9.0.4
    state
: directory
    mode
: 0755
    recurse
: yes


- name: setting the environment
  blockinfile
:
    backup
: yes
    dest
: /etc/profile.d/env.sh
    block
: |
      JAVA_HOME
=/usr/share/jdk-9.0.4/
      PATH
=$PATH:/usr/share/jdk-9.0.4/bin/
    insertafter
: EOF


   
- name: creating jenkins user
 
  user
:
    name
: jenkinsuser
 
register: creation


- include: tasks/install_jenkins.yaml
 
when: creation|success


install_jenkins.yaml



 
- name: downloading latest jenkins war-stable
  get_url
:
        url
: http://mirrors.jenkins.io/war-stable/latest/jenkins.war
        dest
: /home/jenkinsuser
        owner
: jenkinsuser
       
group: jenkinsuser
        mode
: 0755


- name: starting jenkins war-stable
  shell
: java -jar /home/jenkinsuser/jenkins.war
  args
:
    executable
: /bin/bash

 
Thanks

Danish 

Brian Coca

unread,
Mar 20, 2018, 9:31:36 AM3/20/18
to Ansible Project
as you have it there, the `environment` keyword ONLY affects the
get_url task, I would add it to the include so all included tasks
inherit it or put all tasks in install_java in a block that has the
environment keyword attached to it.



--
----------
Brian Coca

danish09

unread,
Mar 20, 2018, 11:27:33 AM3/20/18
to Ansible Project
Thank you for responding to me. I am stuck now at some silly mistake of mine in that playbook. I will come back to your suggestion as soon as I correct the error.

Thanks for your advise again.

danish09

unread,
Mar 23, 2018, 12:24:21 PM3/23/18
to Ansible Project
Thank you very much. Your suggestion is working perfect.
Reply all
Reply to author
Forward
0 new messages