Ansible——script模块

目录

特点

参数总结

[使用 ansible 命令](#使用 ansible 命令)

[1. 基本示例](#1. 基本示例)

[2. 传递参数](#2. 传递参数)

[3. 使用 creates 参数](#3. 使用 creates 参数)

[4. 使用 removes 参数](#4. 使用 removes 参数)

[示例 Playbook 文件](#示例 Playbook 文件)

基本语法

[1. 基本使用](#1. 基本使用)

[2. 传递参数](#2. 传递参数)

[3. 使用 creates 参数](#3. 使用 creates 参数)

[4. 使用 removes 参数](#4. 使用 removes 参数)

[5. 使用 register 捕获输出](#5. 使用 register 捕获输出)

[6. 使用 args 指定参数](#6. 使用 args 指定参数)


Ansible 的 script 模块是用来在远程主机上运行本地脚本文件的一个模块。它将本地脚本文件复制到目标主机上并执行。这个模块对于一些复杂操作性任务或者需要快速运行自定义脚本的场景非常有用。

特点

  • 脚本语言不限:可以是任何可执行的脚本,比如 Bash、Python、Perl 等。
  • 参数传递:允许传递参数给脚本。
  • 直接执行:无需考虑远程主机的具体环境,直接在远程主机上执行脚本。

参数总结

  1. chdir:

    • 描述:在执行脚本前更改到此目录。
    • 类型:字符串
    • 默认值:无
  2. creates:

    • 描述:仅在指定文件不存在时执行脚本。
    • 类型:字符串
    • 默认值:无
  3. executable:

    • 描述:用于执行脚本的可执行程序(例如 bash, python, ruby 等)。
    • 类型:字符串
    • 默认值:无
  4. removes:

    • 描述:仅在指定文件存在时执行脚本。
    • 类型:字符串
    • 默认值:无
  5. stdin:

    • 描述:为脚本提供标准输入。
    • 类型:字符串
    • 默认值:无
  6. stdin_add_newline:

    • 描述:如果为 yes,则在标准输入数据末尾添加一个新行。
    • 类型:布尔值
    • 默认值:yes
  7. strip_empty_ends:

    • 描述:如果为 yes,则从标准输入数据的两端移除空行。
    • 类型:布尔值
    • 默认值:no
  8. cmd:

    • 描述:要执行的本地脚本的路径和可选参数。
    • 类型:字符串或列表
    • 必需:是

使用 ansible 命令

1. 基本示例

在远程主机上运行本地脚本 my_script.sh

复制代码
ansible all -m script -a "/path/to/my_script.sh"
2. 传递参数

在远程主机上运行本地脚本 my_script.sh 并传递参数:

复制代码
ansible all -m script -a "/path/to/my_script.sh --option value"
3. 使用 creates 参数

只有在远程主机上不存在 /tmp/myfile 时才运行脚本:

复制代码
ansible all -m script -a "creates=/tmp/myfile /path/to/my_script.sh"
4. 使用 removes 参数

只有在远程主机上存在 /tmp/myfile 时才运行脚本:

复制代码
ansible all -m script -a "removes=/tmp/myfile /path/to/my_script.sh"

示例 Playbook 文件

基本语法

一个典型的 script 模块结构如下:

复制代码
- name: Description of the task
  script: /path/to/local/script.sh

还可以添加多个可选参数,如 createsremoves,来条件化执行脚本。

1. 基本使用

在远程主机上执行脚本 my_script.sh

复制代码
---
- name: Run basic script
  hosts: all
  tasks:
    - name: Execute the script
      script: /path/to/my_script.sh
2. 传递参数

在远程主机上执行脚本 my_script.sh 并传递参数:

复制代码
---
- name: Run script with arguments
  hosts: all
  tasks:
    - name: Execute the script with arguments
      script: /path/to/my_script.sh --option value
3. 使用 creates 参数

只有在远程主机上不存在 /tmp/myfile 文件时才执行脚本:

复制代码
---
- name: Conditionally run script if file does not exist
  hosts: all
  tasks:
    - name: Execute the script if file does not exist
      script:
        path: /path/to/my_script.sh
        creates: /tmp/myfile
4. 使用 removes 参数

只有在远程主机上存在 /tmp/myfile 文件时才执行脚本:

复制代码
---
- name: Conditionally run script if file exists
  hosts: all
  tasks:
    - name: Execute the script if file exists
      script:
        path: /path/to/my_script.sh
        removes: /tmp/myfile
5. 使用 register 捕获输出

捕获脚本执行的输出并在后续任务中使用:

复制代码
---
- name: Capture script output
  hosts: all
  tasks:
    - name: Run script and capture output
      script: /path/to/my_script.sh
      register: script_output

    - name: Print script output
      debug:
        var: script_output.stdout
6. 使用 args 指定参数

在脚本中指定额外参数:

复制代码
---
- name: Run script with specific arguments
  hosts: all
  tasks:
    - name: Execute the script with multiple arguments
      script:
        path: /path/to/my_script.sh
        args:
          creates: /tmp/myfile
          removes: /tmp/removefile
相关推荐
_w_z_j_3 小时前
Linux----mmap
linux
程序员zgh4 小时前
Linux系统常用命令集合
linux·运维·服务器·c语言·开发语言·c++
Bigan(安)4 小时前
【奶茶Beta专项】【LVGL9.4源码分析】09-core-obj_class对象类系统
linux·c语言·mcu·arm·unix
紫郢剑侠4 小时前
飞秋@Windows +iptux@Linux,打造内网跨平台IM环境
linux·运维·服务器·im·qq
保持低旋律节奏4 小时前
linux——调试
linux·运维·服务器
牛奶咖啡135 小时前
Linux系统故障排查思路实践教程(下)
linux·运维·服务器·su命令切换用户问题解决·文件打开过多问题解决·linux网络故障问题解决·linux故障排查思路
coder4_5 小时前
Linux 数据同步全攻略:NFS 共享、inotify+rsync 与 sersync 实战指南
linux·rsync·文件共享·nfs·数据同步·inotify·实时备份
Lynnxiaowen5 小时前
今天我们继续学习kubernetes内容Helm
linux·学习·容器·kubernetes·云计算
Bigan(安)5 小时前
【奶茶Beta专项】【LVGL9.4源码分析】08-theme主题管理
linux·c语言·mcu·arm·unix
小汐睡着了5 小时前
解决虚拟机VMware与宿主机网络不通的问题-error
linux·网络·redhat