006-结合shell脚本执行

环境说明:

  1. 操作系统:Centos7-2009
  2. 确保网络连接正常、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]#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值