环境说明:
- 操作系统:Centos7-2009
- 确保网络连接正常、yum源正常
步骤步骤:
1、编写exp1.sh:
#!/bin/bash
echo "SHELL 执行成功"
2、编写exp1-1.yaml:
---
- name: 运行shell脚本并返回结果
hosts: exp
vars:
test_dir: /root
tasks:
- name: 复制脚本到目标主机
copy:
src: exp1.sh
dest: "{{test_dir}}/example.sh"
mode: 0755
- name: 运行脚本
shell: "{{test_dir}}/example.sh"
register: shell_result
- name: 输出结果
debug:
msg: shell_result.stdout_lines
:::tips
确保:exp1-1.yaml与exp1.sh文件处于同一目录,如果需更改,修改的copy.src 的指定文件
:::
:::tips
案例涉及知识点:变量定义和使用、如何返回shell的结果、debug模块的使用、copy模块和shell模块结合变量的使用
:::
实际效果:
1、检查语法:ansible-playbook exp1-1.yaml -k --syntax-check
[root@exp-001 test2]# ansible-playbook exp1-1.yaml -k --syntax-check
playbook: exp1-1.yaml
2、检查执行:ansible-playbook exp1-1.yaml -k --check
[root@exp-001 test2]# ansible-playbook exp1-1.yaml -k --check
SSH password:
PLAY [运行shell脚本并返回结果] *********************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************
ok: [11.0.1.137]
ok: [11.0.1.135]
TASK [复制脚本到目标主机] **************************************************************************************************************************************************
ok: [11.0.1.137]
ok: [11.0.1.135]
TASK [运行脚本] *******************************************************************************************************************************************************
skipping: [11.0.1.135]
skipping: [11.0.1.137]
TASK [输出结果] *******************************************************************************************************************************************************
ok: [11.0.1.135] => {
"msg": "shell_result.stdout_lines"
}
ok: [11.0.1.137] => {
"msg": "shell_result.stdout_lines"
}
PLAY RECAP ********************************************************************************************************************************************************
11.0.1.135 : ok=3 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
11.0.1.137 : ok=3 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
3、执行效果:ansible-playbook exp1-1.yaml -k
[root@exp-001 test2]# ansible-playbook exp1-1.yaml -k
SSH password:
PLAY [运行shell脚本并返回结果] *********************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************
ok: [11.0.1.135]
ok: [11.0.1.137]
TASK [复制脚本到目标主机] **************************************************************************************************************************************************
ok: [11.0.1.135]
ok: [11.0.1.137]
TASK [运行脚本] *******************************************************************************************************************************************************
changed: [11.0.1.135]
changed: [11.0.1.137]
TASK [输出结果] *******************************************************************************************************************************************************
ok: [11.0.1.135] => {
"msg": "shell_result.stdout_lines"
}
ok: [11.0.1.137] => {
"msg": "shell_result.stdout_lines"
}
PLAY RECAP ********************************************************************************************************************************************************
11.0.1.135 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
11.0.1.137 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@exp-001 test2]#