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 "主机"
相关推荐
Benszen10 小时前
Ansible自动化运维实战
linux·运维·自动化·ansible
gwjcloud13 小时前
Ansible自动化运维工具
运维·自动化·ansible
我爱学习好爱好爱14 小时前
Ansible force_handlers delegate委托 playbook语法格式 template模块
linux·运维·ansible
淼淼爱喝水1 天前
Ansible 基础模块实战作业详细教程
chrome·github·ansible
zfoo-framework1 天前
我对ansible的理解 1.幂等性 2.6大部分
ansible
道清茗1 天前
【RH294知识点汇总】第 1 章《介绍 Ansible》
ansible
中国lanwp2 天前
Docker 一键部署脚本(CentOS7/AlmaLinux9)+ Ansible Playbook
docker·ansible
我爱学习好爱好爱2 天前
Ansible Loop循环 循环遍历的属性 Notify和Handlers
linux·运维·ansible
牛奶咖啡132 天前
DevOps自动化运维实践_ansible-playbook的使用
运维·自动化·ansible·devops·playbook·playbook模块及其示例
我爱学习好爱好爱2 天前
Ansible include任务复用 tags ignore_errors
linux·运维·ansible