ansible-playbook设置java环境变量后不生效解决方法

ansible playbook脚本中有安装jdk操作,并且设置环境变量后且source了 /etc/profile文件,还会提示没有JAVA_HOME
代码如下:

- name: install  jdk1.8 
  yum: name=jdk1.8.0_144.x86_64 state=present disablerepo=* enablerepo=packages

- name: check jdk variable 
  shell: cat /etc/profile
  register: result

#jdk_version 在变量文件中有定义
- name: jdk variable configuration /etc/profile file
  shell: /bin/echo {{ item }} >> /etc/profile; source /etc/profile
  when: result.stdout.find('JAVA_HOME') == -1
  with_items:
     - export JAVA_HOME=/usr/java/jdk{{ jdk_version }}
     - export JAVA_BIN=/usr/java/jdk{{ jdk_version }}/bin
     - export PATH=$PATH:/usr/java/jdk{{ jdk_version }}/bin
     - export JRE_HOME=/usr/java/jdk{{ jdk_version }}/jre
     - export CLASSPATH=.:/usr/java/jdk{{ jdk_version }}/lib:/usr/java/jdk{{ jdk_version }}/jre/lib
     - export JAVA_HOME JAVA_BIN PATH JRE_HOME CLASSPATH

在ansible环境下,控制其他机器,除了/etc/profile文件需要修改,还需要修改/root/.bashrc文件中的值

- name: check jdk variable 
  shell: cat /root/.bashrc
  register: bashrc_result

- name: jdk variable configuration /root/.bashrc file
  shell: /bin/echo {{ item }} >> /root/.bashrc; source /root/.bashrc
  when: bashrc_result.stdout.find('JAVA_HOME') == -1
  with_items:
     - export JAVA_HOME=/usr/java/jdk{{ jdk_version }}

你可能感兴趣的:(ansible)