ansible playbook 使用 script 模块在远程主机上执行脚本

  1. 需求一: 要在所有的远程主机上执行一下收集脚本,并在控制台打印出来
    脚本如下:

    [root@192 Playbooks]# cat CollectServerInformation.sh
    #!/bin/bash

    Collect server information

    echo "Collecting server information..."

    OS Information

    echo "Operating System Information:"
    cat /etc/*release

    Kernel Version

    echo "Kernel Version:"
    uname -r

    echo "Network Configuration:"
    ip -4 a

编辑ansible-playbook

v1版本:

复制代码
[root@192 Playbooks]# cat AnsiblePlay-CollectServerInformation.yaml
---
  - name: Execute script
    gather_facts: no
    become: yes
    become_user: root
    hosts: all
    tasks:
    - name: Execute script
      script:
        cmd: /opt/Playbooks/CollectServerInformation.sh
  • gather_facts: no : 不搜集主机 facts 可以加快执行速度
  • become: yes : 是否使用sudo
  • become_user: root :切换的用户

执行效果:

复制代码
[root@192 Playbooks]# ansible-playbook AnsiblePlay-CollectServerInformation.yaml

可以看出虽然脚本执行成功了,但是并没有在控制台打印出来。

v2版本:

复制代码
[root@192 Playbooks]# cat AnsiblePlay-CollectServerInformation.yaml
---
  - name: Execute script
    gather_facts: no
    become: yes
    become_user: root
    hosts: all
    tasks:
    - name: Execute script
      script:
        cmd: /opt/Playbooks/CollectServerInformation.sh
      register: script_result

    - name: Output result
      debug:
        msg: "{{ script_result.stdout  }}"
  • register: script_result : 将脚本的值注册到 script_result 中
    debug: # 调用debug模块打印 script_result 变量中的内容
    msg: "{{ script_result.stdout }}"
  1. 需求二: 要求把脚本的输出结果写入到ansible主机,并以远程主机名命名

这里的远程主机名指定是 inventory 清单中的主机名

如下:

编写ansible-play:

复制代码
---
- name: Execute remote script and save output on Ansible control node
  hosts: all
  gather_facts: no
  become: yes
  become_user: root
  tasks:
    - name: Execute script on remote hosts and save output to a temporary file
      script:
        cmd: /opt/Playbooks/hardware_info.sh
      register: script_result

    - name: Save script output with hostname as filename
      copy:
        content: "{{ script_result.stdout }}"
        dest: "/root/{{ inventory_hostname }}_output.txt"
      when: script_result.stdout is defined

    - name: Fetch script output from remote hosts
      fetch:
        src: "/root/{{ inventory_hostname }}_output.txt"
        dest: "/opt/Playbooks/output/"
        flat: yes
      when: script_result.stdout is defined	

上述ansible-play中定义了3个 task

task1:

将脚本的输出结果注册到变量script_result

task2:

将远程主机的script_result.stdout 中的内容复制到 /root/{{ inventory_hostname }}_output.txt 文件,inventory_hostname 指定是在ansible清单中,对应的主机名。

task3:

将远程主机上/root/{{ inventory_hostname }}_output.txt 文件拷贝到ansible主机 /opt/Playbooks/output/ 目录下这里最后面记得加上/

  • when: script_result.stdout is defined : 在执行task前先判断 script_result.stdout 变量是否被定义
相关推荐
key_Go7 小时前
7.Ansible自动化之-实施任务控制
python·ansible·numpy
yuxb733 天前
Ansible 基础到实操笔记
linux·笔记·ansible
IT成长日记4 天前
【自动化运维神器Ansible】playbook命令行变量定义全流程解析
运维·自动化·ansible·变量·命令行·playbook
vivo互联网技术4 天前
vivo Pulsar 万亿级消息处理实践(4)-Ansible运维部署
大数据·ansible·自动化运维·pulsar·消息处理·分布式消息中间件
朱小弟cs65 天前
Orange的运维学习日记--41.Ansible基础入门
linux·运维·学习·ci/cd·自动化·ansible·devops
IT成长日记5 天前
【自动化运维神器Ansible】playbook案例解析:Tags组件实现任务选择性执行
运维·自动化·ansible·playbook·tags
IT成长日记5 天前
【自动化运维神器Ansible】playbook实践示例:HTTPD安装与卸载全流程解析
运维·自动化·ansible·playbook·httpd·案例解析
天翼云开发者社区10 天前
Ansible部署Node_exporter
ansible·应用自动化运维
ZZZKKKRTSAE11 天前
RHCE认证题解
linux·运维·服务器·ansible·rhce·rhel9·红帽