惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Threat Research - Cisco Blogs
G
GRAHAM CLULEY
The Hacker News
The Hacker News
S
Securelist
K
Kaspersky official blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
AWS News Blog
AWS News Blog
H
Hacker News: Front Page
P
Privacy & Cybersecurity Law Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
News | PayPal Newsroom
Project Zero
Project Zero
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
The Cloudflare Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tenable Blog
The Last Watchdog
The Last Watchdog
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
S
Schneier on Security
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
月光博客
月光博客
博客园_首页
V
Visual Studio Blog
宝玉的分享
宝玉的分享
IT之家
IT之家
PCI Perspectives
PCI Perspectives
MyScale Blog
MyScale Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Troy Hunt's Blog
F
Fortinet All Blogs
U
Unit 42
Help Net Security
Help Net Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
J
Java Code Geeks
aimingoo的专栏
aimingoo的专栏
Jina AI
Jina AI
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
MongoDB | Blog
MongoDB | Blog
G
Google Developers Blog
人人都是产品经理
人人都是产品经理
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - Franky

繁星点点

最新轻量级龙虾部署教程 通过netboot.xyz重装系统 giffgaff卡保号教程 Llinux系统中全新透明代理方案(可代理所有出站流量) tailscale开启子网路由常见的坑 OpenWrt固件编译 解决UFW和Docker的问题 使用PGP的非对称算法加密文件 NSSM工具把任意exe程序注册为Windows后台服务 轻量级简约端口转发rinetd 安卓IDE的安装和使用 IP信息查询网站汇总 热点共享代理教程 SSH使用密钥认证登录详细解释 Debian系统公网IP出口切换指南,适用多IP服务器 Windows系统安装Scoop包管理器 安卓Flutter开发学习笔记 使用手机搭建节点服务端 关于github提交记录泄露真实邮箱的处理办法 anytls协议部署教程 caddy给域名用上ECH加密 通过gitlab流水线自动备份仓库 通过CF-Workers代理访问网站 低配机器使用alpine系统跑docker 端口敲门教程 3x-ui面板通过api管理示例 nmap端口扫描 数据库常用命令 snap和winget包管理器 Git推送GitHub常用命令 😩😩😩学不完,根本学不完😩😩😩 使用CDN后获取访客真实IP 一些用于网络测试的下载测速文件 纯v6网络访问v4站点最简单的方法 服务器配置SSH密钥登录 windows重装系统教程 Docker部署哪吒监控 Github-Actions默认用户变量GITHUB_TOKEN的提权语法 给站点套CFT的一些坑 解决CF反代Docker失效的问题 使用py3快速启动一个文件上传和下载的接口 ios系统最新免拔卡使用tiktok方法 clash手搓配置最全协议示例详解 为IP地址申请免费证书并开启HTTPS 实用网站收藏 纯CSS实现剧透效果 在CloudflareWAF中屏蔽国内浏览器 网络安全之百度URL欺骗漏洞分析和复现 收集整理的最新的常用VPS脚本工具 无法注册TG账号和国内号码接收不到验证码详细解决方案 国内环境安装xui面板 免费接码网站合集 完整的网络状态码列表 关于xui面板报错无法监听127.0.0.1:62789的问题 Xboard面板常见问题 项目编译构建文档 ios最新美区ID注册方法 V2board前后端分离 Markdown语法教程 Github加速 Docker镜像加速和离线安装 Liunx配置wrap 代理工具和SSH工具推荐 常用代码记录
超级强大的ansible批量管理主机运维工具使用教程
2024-08-13 · via 繁星点点

1:安装并创建配置文件

sudo apt update
sudo apt install ansible -y
mkdir -p /etc/ansible && cd /etc/ansible && touch ansible.cfg hosts renwu.yml

ansible.cfg 配置Ansible的全局设置。

hosts 定义要管理的主机和主机组。

renwu.yml(或playbook) 描述要在主机上执行的任务和操作步骤。

2:禁用被控主机密钥检查

ansible.cfg中添加以下配置

[defaults]
host_key_checking = False
ansible_ssh_common_args = '-o StrictHostKeyChecking=no'

3:配置被控主机清单

hosts中添加被控主机示例

[myservers]
1 ansible_host=192.168.1.1 ansible_user=root ansible_port=22 ansible_ssh_pass=password1
2 ansible_host=192.168.1.2 ansible_user=root ansible_port=22 ansible_ssh_pass=password2
3 ansible_host=192.168.1.3 ansible_user=root ansible_port=22 ansible_ssh_pass=password3
4 ansible_host=192.168.1.4 ansible_user=root ansible_port=22 ansible_ssh_pass=password4
5 ansible_host=192.168.1.5 ansible_user=root ansible_port=22 ansible_ssh_pass=password5

4:使用ping模块测试所有被控主机连通性

(可选)查看所有被控机的信息 ansible-inventory --list -i /etc/ansible/hosts

ansible -m ping all

5:创建被控主机任务配置文件

renwu.yml中添加任务示例

---
# 定义要执行任务的主机组
- hosts: myservers
  become: yes  # 以管理员权限运行命令
  tasks:
    - name: 将Shell脚本复制到远程主机
      copy:
        # 本地脚本路径
        src: /etc/ansible/script.sh  
        # 远程主机上的目标路径
        dest: /tmp/script.sh  
        # 设置脚本权限为可执行
        mode: '0755'  

    - name: 在远程主机上执行Shell脚本
      shell: /tmp/script.sh  # 在远程主机上执行脚本

或者直接执行远程脚本示例

---
# 定义要执行任务的主机组
- hosts: myservers
  become: yes  # 以管理员权限运行命令
  tasks:
    - name: 更新包列表并安装所需的软件包
      shell: |
        apt update
        apt install curl wget git zip tar lsof -y

    - name: 在远程主机上执行Shell脚本
      shell: bash <(wget -qO- https://example.com//shell/raw/main/demo.sh)
      args:
        executable: /bin/bash  # 确保使用bash执行命令

6:用法示例

  • 对所有被控机器运行renwu.yml中的任务
ansible-playbook renwu.yml
  • 临时对所有主机执行普通命令
ansible all -a "pwd"
  • 临时对所有主机运行远程脚本
ansible all -m shell -a "bash <(wget -qO- https://example.com//shell/raw/main/demo.sh)"
  • 临时将本地脚本复制给所有被控主机并执行
ansible all -m copy -a "src=/etc/ansible/script.sh dest=/tmp/script.sh mode=0755"
ansible all -m shell -a "/tmp/script.sh"
  • 临时对1,3号主机执行shell命令
ansible 1,3 -m shell -a "你的命令"
  • 临时对1,3号主机执行普通命令
ansible 1,3 -a "pwd"

命令结尾后面追加-v选项会显示被控机器详细的执行信息


命令解释

-m 用于指定 Ansible 模块

-a 用于指定传递给模块的参数或命令

模块 指令 中文解释 用法示例
shell -a 执行 shell 命令。支持管道、重定向等 shell 特性。 ansible all -m shell -a "pwd"
command -a 执行命令,不通过 shell。默认模块 ansible all -m command -a "ls -l"
copy -a 复制文件或目录到目标主机。 ansible all -m copy -a "src=/local/file dest=/remote/file mode=0644"
file -a 管理文件和目录的属性(如权限、所有权等)。 ansible all -m file -a "path=/remote/file state=absent"
yum -a 使用 Yum 包管理器安装、更新或删除软件包(适用于 RHEL/CentOS)。 ansible all -m yum -a "name=nginx state=present"
apt -a 使用 APT 包管理器安装、更新或删除软件包(适用于 Debian/Ubuntu)。 ansible all -m apt -a "name=nginx state=latest"
service -a 管理服务(如启动、停止、重启服务)。 ansible all -m service -a "name=nginx state=started"
systemd -a 管理 systemd 服务(如启动、停止、重启服务)。 ansible all -m systemd -a "name=nginx state=started"
user -a 管理用户账户(如创建、删除用户)。 ansible all -m user -a "name=alice state=present"
group -a 管理用户组(如创建、删除组)。 ansible all -m group -a "name=admin state=present"
git -a 管理 Git 仓库(如克隆、拉取、提交等)。 ansible all -m git -a "repo=https://github.com/user/repo.git dest=/path/to/repo"
template -a 使用 Jinja2 模板引擎渲染模板文件。 ansible all -m template -a "src=template.j2 dest=/etc/config"
cron -a 管理 cron 任务。 ansible all -m cron -a "name='Backup' minute='0' hour='2' job='/usr/bin/backup.sh'"
wait_for -a 等待某个条件满足(如端口开放、文件存在等)。 ansible all -m wait_for -a "port=80 delay=10 timeout=300"
docker_container -a 管理 Docker 容器(如启动、停止、删除容器)。 ansible all -m docker_container -a "name=my_container state=started"
docker_image -a 管理 Docker 镜像(如拉取、删除镜像)。 ansible all -m docker_image -a "name=nginx tag=latest state=present"
lineinfile -a 在文件中插入、删除或修改行。 ansible all -m lineinfile -a "path=/etc/hosts line='127.0.0.1 localhost' state=present"
ini_file -a 修改 INI 配置文件。 ansible all -m ini_file -a "path=/etc/myconfig.ini section=database option=host value=localhost"
debug -a 打印调试信息。 ansible all -m debug -a "msg='This is a debug message'"


执行结果解释

  • ok: 表示在该主机上成功完成的任务数。
  • changed: 表示在该主机上有多少任务进行了更改(如文件被复制、脚本被执行)。
  • unreachable: 表示无法连接的主机数量。
  • failed: 表示任务失败的数量。
  • skipped: 表示被跳过的任务数量。
  • rescued: 表示在任务失败后被恢复的数量。
  • ignored: 表示被忽略的任务数量。
  • 绿色:任务顺利完成
  • 橙色:任务执行后有变化,比如文件被修改或某些服务被重启。
  • 红色:任务执行失败,一般会终止剩余的所有任务。

如果所有被控机端口和密码都一样

/etc/ansible/hosts配置可以这样写

[all:vars]
ansible_user=root
ansible_ssh_pass=your_password
ansible_port=22

[myservers]
1 ansible_host=192.168.1.101
2 ansible_host=192.168.1.102
3 ansible_host=192.168.1.103