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 "主机"
相关推荐
明航咨询_贾老师1 天前
RHCA Ansible高级自动化(DO374)认证信息整理
运维·自动化·ansible·rhca·红帽认证
有毒的教程5 天前
Ansible批量操作自动化完整教程:批量部署服务、配置同步、软件更新实战
运维·自动化·ansible
Hy行者勇哥10 天前
用Cursor智能编写Ansible/Terraform脚本,打通CI/CD链路
ci/cd·ansible·terraform
芷栀夏12 天前
飞牛NAS怎么部署Immich?家庭照片自动备份与远程访问教程
服务器·nginx·ansible
悠然南风23 天前
Ansible常见模块总结及LDAP Role 编写与调试
ansible
祺风挽楠1 个月前
ansible编辑
网络·ansible
芳心粽伙饭1 个月前
Ansible课后作业
ansible
烁3471 个月前
Ansible初识
ansible
烁3471 个月前
Ansible安装部署调试
ansible
烁3471 个月前
Ansible命令
ansible