ansible批量筛选出指定操作系统的主机列表

查看主机操作系统的shell脚本getOS.sh

bash 复制代码
#!/bin/bash

# 获取操作系统信息
os_info=$(hostnamectl | grep 'Operating System' | awk -F ':' '{print $NF}')

# 检查是否包含"centos"
if echo "$os_info" | grep -E '*centos*' > /dev/null; then
    # 打印主机IP
    echo "主机IP:$(hostname -I | awk '{print $1}')"
fi

编辑hosts文件,保存待查看的主机列表

bash 复制代码
[sc] 
10.1.1.[1:100] ansible_ssh_user=主机用户 ansible_ssh_pass=主机用户密码

用ansible进行传输脚本和执行

bash 复制代码
传输到各台主机
ansible -i ./hosts all -m copy -a "src=/etc/ansible/scripts/getOS.sh dest=/tmp"

执行脚本
ansible -i ./hosts all -m shell -a "sh /tmp/getOS.sh" >all.txt

cat all.txt|grep "主机"
相关推荐
一切皆文件linux10 小时前
网站集群批量管理-密钥认证与Ansible模块
ansible
让美好继续发生2 天前
ansible学习
学习·ansible
有谁看见我的剑了?2 天前
ansible学习之 Facts
ansible
peanutfish2 天前
Chapter 4 RH294 RHEL Automation with Ansible
linux·ansible·yaml
henan程序媛2 天前
jenkins项目发布基础
运维·gitlab·ansible·jenkins
pyliumy4 天前
ansible 配置
大数据·ansible
运维小白。。4 天前
自动化运维工具 Ansible
运维·自动化·ansible
weixin_438197384 天前
ansible之playbook\shell\script模块远程自动安装nginx
linux·服务器·ansible
peanutfish5 天前
Chapter 5 RH294 RHEL Automation with Ansible
linux·ansible·yaml
人类群星闪耀时5 天前
自动化运维的利器:Ansible、Puppet和Chef详解
运维·自动化·ansible