playbook写法及ansible安装jdk

网友投稿 388 2022-11-03


playbook写法及ansible安装jdk

YAML语法和playbook写法  ​​​​链接1​​​ ​​链接2​​

环境准备

jdk-8u152-linux-x64.tar.gz

study01 测试机1

study02 测试机2

熟悉yaml文件

[root@master ansible]# cat 1.yaml ---- hosts: web tasks: - name: source JAVA_HOME shell: hostname >> /tmp/test.txt && date '+%F %R' >> /tmp/test.txt #shell: date '+%F %R' >> /tmp/test.txt #貌似不能连续写两个shell,因此上面用&&串联两个命令

主机配置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": []}...ignoringfatal: [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": []}...ignoringTASK [创建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

测试机验证

验证结果正常。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:使用PreferenceActivity时,如何获取ListPreference中选中的值
下一篇:免费查询个人征信API(免费查询个人征信的公众号)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~