Reputation: 173
I need to deploy a bunch of machines from Ansible, these machines need to execute a suite of programs that run on the JVM and one hard requisite is that I need to deploy the JDK from a tar.gz without leveraging RedHat's repositories.
So I need to define an environment variable to the JDK's folder on my hosts and I tried to achieve this with these tasks:
- name: Set JDK environment variables
become: yes
lineinfile:
path: /etc/profile
state: present
line:"{{item}}"
with_items:
- 'export JAVA_HOME="{{java_dir}}"'
- 'export PATH=$PATH;$JAVA_HOME/bin:$PATH'
- name: Reload environment variables
shell:
cmd: source /etc/profile
At this point the variables seem to be correctly set on the system, because if I login into any of the machines the java --version
command works.
Now when the playbook tries to execute a task that needs to be run from the JVM I get this error:
fatal: [hostname]: FAILED! => {
"changed": true,
"cmd": [
"./kafka-storage.sh",
"random-uuid"
],
.....
.....
.....
}
STDERR:
/opt/kafka/bin/kafka-run-class.sh line 346: exec : java: not found
MSG:
non-zero return code
Please keep in mind this playbook used to install the JDK from the internal repositories and everything worked flawlessly so this can't be the fault of Kafka's executable.
I tried to make sure that they can be seen from Ansible I also perform this task that adds this path to the user's home that Ansible is operating with:
- name: Set JDK environment for next tasks
become: yes
lineinfile:
path: /home/user/.bashrc
state: present
line:"{{item}}"
with_items:
- 'export JAVA_HOME="{{java_dir}}"'
- 'export PATH=$PATH;$JAVA_HOME/bin:$PATH'
But still the Kafka executable can't find Java's folder.
Upvotes: 0
Views: 261
Reputation: 173
I've eventually solved this by writing the env variables in a .bash_profile file on the host machine.
- name: Set environmental variables
become: yes
blockinfile:
path: "/[path]/.bash_profile"
state: present
block: |
export JAVA_HOME="{{path_to_jdk}}"
export PATH=$PATH:JAVA_HOME/bin:$PATH
And then used the env vars for a specific task that I needed to run:
- name: Perform task
shell: source /[path]/.bash_profile && command.sh
args:
executable: /bin/bash
become: true
This probably doesn't permanently set these env variables for the rest of the playbook's tasks but it might be viable for one or two tasks.
Upvotes: 0