YAML语法和playbook写法 链接1 链接2
环境准备
jdk-8u152-linux-x64.tar.gz
study01 测试机1
study02 测试机2
主机配置yaml文件
[root@master ansible]# cat install_jdk.yaml
---
- name: "检测并安装jdk"
hosts: web
tasks:
- name: "检测主机是否安装jdk"
shell: java -version
ignore_errors: True
register: check_jdk_result
- name: "创建jdk目录"
file:
path: /usr/java
state: directory
owner: root
group: root
mode: 0755
when: check_jdk_result.rc != 0
- name: "将jdk解压并拷贝到远程目录下"
unarchive:
src: jdk-8u152-linux-x64.tar.gz
dest: /usr/java
copy: yes
when: check_jdk_result.rc != 0
- name: "添加环境变量,提前手动打开看一下解压后的jdk文件名"
shell: /bin/echo {{ item }} >> /etc/bashrc
loop:
- export JAVA_HOME=/usr/java/jdk1.8.0_152
- export CLASSPATH=\$JAVA_HOME/lib/
- export PATH=\$PATH:\$JAVA_HOME/bin
when: check_jdk_result.rc != 0
- name: "加载配置文件"
shell: source /etc/bashrc
args:
executable: /bin/bash
- name: "测试jdk是否安装好"
shell: java -version
运行
[root@master ansible]# ansible-playbook install_jdk.yaml -i /etc/ansible/hosts
PLAY [检测并安装jdk] ************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************************************************************************ok: [172.16.3.101]
ok: [172.16.3.102]
TASK [检测主机是否安装jdk] *********************************************************************************************************************************************************************************************
fatal: [172.16.3.102]: FAILED! => {"changed": true, "cmd": "java -version", "delta": "0:00:00.006948", "end": "2022-08-15 17:37:43.785598", "msg": "non-zero return code", "rc": 127, "start": "2022-08-15 17:37:43.778650", "stderr": "/bin/sh: java: command not found", "stderr_lines": ["/bin/sh: java: command not found"], "stdout": "", "stdout_lines": []}
...ignoring
fatal: [172.16.3.101]: FAILED! => {"changed": true, "cmd": "java -version", "delta": "0:00:00.005696", "end": "2022-08-13 04:35:15.209605", "msg": "non-zero return code", "rc": 127, "start": "2022-08-13 04:35:15.203909", "stderr": "/bin/sh: java: command not found", "stderr_lines": ["/bin/sh: java: command not found"], "stdout": "", "stdout_lines": []}
...ignoring
TASK [创建jdk目录] *************************************************************************************************************************************************************************************************
ok: [172.16.3.102]
ok: [172.16.3.101]
TASK [将jdk解压并拷贝到远程目录下] *****************************************************************************************************************************************************************************************
ok: [172.16.3.101]
ok: [172.16.3.102]
TASK [添加环境变量,提前手动打开看一下解压后的jdk文件名] ******************************************************************************************************************************************************************************
changed: [172.16.3.102] => (item=export JAVA_HOME=/usr/java/jdk1.8.0_152)
changed: [172.16.3.101] => (item=export JAVA_HOME=/usr/java/jdk1.8.0_152)
changed: [172.16.3.102] => (item=export CLASSPATH=\$JAVA_HOME/lib/)
changed: [172.16.3.101] => (item=export CLASSPATH=\$JAVA_HOME/lib/)
changed: [172.16.3.102] => (item=export PATH=\$PATH:\$JAVA_HOME/bin)
changed: [172.16.3.101] => (item=export PATH=\$PATH:\$JAVA_HOME/bin)
TASK [加载配置文件] **************************************************************************************************************************************************************************************************
changed: [172.16.3.101]
changed: [172.16.3.102]
TASK [测试jdk是否安装好] **********************************************************************************************************************************************************************************************
changed: [172.16.3.101]
changed: [172.16.3.102]
PLAY RECAP *****************************************************************************************************************************************************************************************************172.16.3.101 : ok=7 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
172.16.3.102 : ok=7 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
到测试机上验证
需要手动命令使环境生效(这一步不确定是不是必须)
source /etc/bashrc
测试机验证
验证结果正常。