ansible安装jdk并写入环境变量
jdk除了包的安装,还需要写入环境变量
- name: Create taishi dir file: path={{ taishi_dir }} state=directory owner={{ taishi_user }} group={{ taishi_user }} tags: jdk - name: visit old_jdk shell: rpm -qa | grep jdk register: jdk_result ignore_errors: True tags: jdk - name: visit old_java shell: rpm -qa | grep java register: java_result ignore_errors: True tags: jdk - name: uninstall old_jdk shell: rpm -qa | grep jdk | xargs rpm -e --nodeps when: jdk_result is succeeded tags: jdk - name: uninstall old_java shell: rpm -qa | grep java | xargs rpm -e --nodeps when: jdk_result is succeeded - name: Create new jdk dir file: path="{{ taishi_dir }}/jdk" state=directory owner={{ taishi_user }} group={{ taishi_user }} tags: jdk - name: Copy jdkpackage to hosts copy: src=jdk-8u60-linux-x64.tar.gz dest=/tmp tags: jdk - name: Install new jdk1.8 for hosts unarchive: src="/tmp/jdk-8u60-linux-x64.tar.gz" dest="{{ taishi_dir }}/jdk" copy=no mode=0755 tags: jdk #使用lineinfile就不会重复在/etc/profile文件中不会重复写入path内容 - name: set env lineinfile: dest=/etc/profile insertafter="{{item.position}}" line="{{item.value}}" state=present with_items: - {position: EOF, value: "export JAVA_HOME={{ taishi_dir }}/jdk/jdk1.8.0_60/"} - {position: EOF, value: "export PATH=$JAVA_HOME/bin:$PATH"} tags: jdk #使用echo的shell命令的方式多次执行就会多次追加内容 - name: set env2 shell: "echo 'export JAVA_HOME={{ taishi_dir }}/jdk/jdk1.8.0_60/' >> /etc/profile" #执行java命令之前可以手动source最新的环境变量 - name: check java version with env shell: source /etc/profile && java -version tags: jdk #执行java命令的时候可以使用java的全路径来调用 - name: check java version with fullpath shell: "{{ taishi_dir }}/jdk/jdk1.8.0_60/bin/java -version" tags: jdk