Ansible 自动化运维

Ansible架构:

一.部署主机清单 前期环境准备:

管理端:

192.168.60.128

被管理端:

client1:192.168.60.129

client2:192.168.60.131

1.1 所有被管理端配置ssh密钥 (1.免密登陆 2.允许root远程登陆) 脚本如下:

bash 复制代码
#!/bin/bash

# 检查 sshpass 是否已安装
if ! command -v sshpass &> /dev/null; then
    echo "请先安装 sshpass 工具!"
    exit 1
fi

# 固定的 IP 地址列表
ip_list="192.168.60.129 192.168.60.131"

# 检查并生成 SSH 密钥
if [ ! -f ~/.ssh/id_rsa ]; then
    echo '----------------'
    echo '1. 创建 key'
    echo '----------------'
    if ! ssh-keygen -f ~/.ssh/id_rsa -t rsa -P ''; then
        echo "生成 SSH 密钥失败!"
        exit 1
    fi
else
    echo "SSH 密钥已存在,跳过生成步骤。"
fi

echo '----------------'
echo '2. 分发 key'
echo '----------------'

for ip in $ip_list; do
    echo "----------------"
    echo "分发 key 到 $ip"
    echo "----------------"
    if sshpass -p2004129 ssh-copy-id -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no root@$ip; then
        echo "分发公钥到 $ip 成功!"
    else
        echo "分发公钥到 $ip 失败!"
    fi
done

运行效果:

1.2 控制端安装ansible

2.1安装 EPEL 源:sudo dnf install epel-release

sudo dnf install ansible

ansible --version

ok~~~~~~~~~~~~~~~~~~~~~~~~~~~

3.安装完成之后 配置主机清单~ 配置文件地址:/etc/ansible/hosts

Server1\] 为组名 将.129 和.131分配在一个组\~\~\~\~\~\~\~\~\~\~ ![](https://i-blog.csdnimg.cn/direct/15c9d1b98e1c43958fd09ad76f363a38.png) 4.测试使用: 测试1 # ansible Server1 -m ping //测试主机是否通\~\~\~\~\~\~\~\~ 出现绿色就说明通了\~\~\~\~\~\~![](https://i-blog.csdnimg.cn/direct/5a82120eaf72466e835779c1ac45933a.png) 测试2 # ansible Server1 -m command - a 'hostname' // -m 指定模块 -a 执行的命令 command命令执行模块 ![](https://i-blog.csdnimg.cn/direct/fd5d894adaab4cf0903a343704473681.png) 测试3 #ansible Server1 -m shell -a 'ip addr \| grep inet' **注意command不能识别管道!!!!!!** ![](https://i-blog.csdnimg.cn/direct/746db6282f5f4f28bf2f66d5564d63a6.png) #### **1.3 ansible 核心配置文件:** 就最前面这两个 ansible.cfg # ansible核心配置文件 ![](https://i-blog.csdnimg.cn/direct/223d4203023c447a86f44cfc9585b8c7.png) hosts #主机清单 如何设置子组(就是将多个组 合并在一个组) 在host文件下这样写: \[data:children\] // children关键字 组名 组名 \~n ![](https://i-blog.csdnimg.cn/direct/ee31c6f6d6624473988d7979222024ae.png) 测试一下看 # ansible data -m ping ![](https://i-blog.csdnimg.cn/direct/46e3866300df4eba909c1b4765582508.png) 测试所有主机是否ping通 # ansible all -m ping ![](https://i-blog.csdnimg.cn/direct/74b5ec5c20544232bb686ba090c05189.png) **ansibel核心模块** ![](https://i-blog.csdnimg.cn/direct/02347cb5fb5c4cc6b6a593ec9ccae24f.png) 如何查询命令帮助: ansible-doc -s command ansible官方文档:[Ansible Documentation](https://docs.ansible.com/ "Ansible Documentation") ![](https://i-blog.csdnimg.cn/direct/630fe73287b541fca0185568bf9fccdc.png) 模块使用: ## **二.命令脚本相关模块** **1.command** 支持简单的命令 不支持管道符号!!!! 默认模块 不写 也是他 ansible all -m command -a '命令' **2.shell模块** 支持 一些特殊的符号! ansible all -m shell -a 'ip a s ens160 \| grep inet' ![](https://i-blog.csdnimg.cn/direct/9df201f96f73463ea4ed117b66658dc2.png) **3.script模块 传输**脚本到被管理端 并执行 3.1比如我们在管理端写一个安装nmap软件脚本 脚本内容 名称: ![](https://i-blog.csdnimg.cn/direct/54c6948b85c142c2af26c938e696529d.png) 执行命令:ansible all -m script -a '/home/test/script-ans/nmapins.sh' ![](https://i-blog.csdnimg.cn/direct/ab9b6fff284c44b2829ad31c2b351f74.png) 看一下被控端受否安装(直接在管理端测试nmap这个工具) ansible all -m shell -a 'nmap localhost' ![](https://i-blog.csdnimg.cn/direct/7aa7991a7070416f92b324c32a94fac3.png) ## 二.文件相关模块 ![](https://i-blog.csdnimg.cn/direct/ead954633f4b42568aa04538893f744a.png) #### 2.1创建目录和文件 ![](https://i-blog.csdnimg.cn/direct/5639bd6cba764c7b8ae0b2f7f8a295f8.png) 目录:ansible all -m file -a 'path=/home/ansible-test state=directory' ![](https://i-blog.csdnimg.cn/direct/c26c5b7bac5246bfb00fd5dd870b6e80.png) 文件:ansible all -m file -a 'path=/home/ansible-test/hello.txt state=touch' ![](https://i-blog.csdnimg.cn/direct/d36af26ca34444a6952b4811d2c1f09d.png) #### 2.2创建软连接 ansible all -m file -a 'src=/home/ansible-test/hello.txt path=/home/test/hello.txt.soft state=link' ![](https://i-blog.csdnimg.cn/direct/541f50c548f24cb39a0d5f4831d20d6c.png) 看看是否成功:ansible all -a 'ls -l /home/test' ![](https://i-blog.csdnimg.cn/direct/60c3dc60d94748a98c4915d7aff42c7c.png) 删除(都是加一个参数就可以 state=ansent) 删除一个文件:ansible all -m file -a 'path=/home/ansible-test/hello.txt state=absent' 注意是:state=absent 不是 state:absent ![](https://i-blog.csdnimg.cn/direct/d522b1ae318c452692624f08942387c9.png) #### 案例:创建一个txt文件 所有者是:root 组:root 权限:755 ansible all -m file -a 'path=/tmp/xxw.txt state=touch group=root owner=root' ![](https://i-blog.csdnimg.cn/direct/70447900d07e40fbae6c2053b59863af.png) ## 三. 文件传输copy模块 ![](https://i-blog.csdnimg.cn/direct/2564ab521ed344da97489f55aec5718f.png) ansible all -m copy -a 'src=/home/test/400.txt dest=/home/' ![](https://i-blog.csdnimg.cn/direct/65966186a180410ba9ee2d1d19edf076.png) 如果当遇到同名的文件 可以备份一下:添加参数:**backup=yes** ansible all -m copy -a 'src=/home/test/400.txt dest=/home/ backup=yes' 执行后备份路径为: ![](https://i-blog.csdnimg.cn/direct/65e50559f73846bf97cc5904eaad8f18.png) 备份了原先的内容: ![](https://i-blog.csdnimg.cn/direct/48da0a0b59484103a5ac42123c4a2eb5.png) ## 四.服务管理模块 涉及到命令:systemctl 主要就是 服务启动 关闭 开机自/不启动。。。。 #### 1.模块名:systemctld ![](https://i-blog.csdnimg.cn/direct/07cc0667711747d98b81a91e7d794b27.png) #### 2.案例: firewalld ##### 2.1关闭 ansible all -m systemd -a 'name=firewalld enabled=no state=stopped' ![](https://i-blog.csdnimg.cn/direct/5279672a91394aeebcd9f8a51ff7c3d1.png) 查看是否关闭: ansible all -a 'systemctl status firewalld' 注意这不是报错 服务已经关闭 注意看!!!!! ![](https://i-blog.csdnimg.cn/direct/5d144fa376ce433ebbf9afe0806e3d76.png) ##### 2.2开启 ansible all -m systemd -a 'name=firewalld enabled=yes state=started' ![](https://i-blog.csdnimg.cn/direct/07a097e92f5f4f3ba08ebad27f1d7e8b.png) 查看是否开启: ansible all -a 'systemctl status firewalld' ![](https://i-blog.csdnimg.cn/direct/560a3d3fffc24e7fa11ff175c3dcbd4f.png) ## 五.磁盘挂载模块 \& 定时任务模块 ![](https://i-blog.csdnimg.cn/direct/35d70549dad4495e9bf00f0cf58835b0.png) ##### 1.mount 挂载nfs [NFS搭建指南](https://blog.csdn.net/weixin_47830774/article/details/146312194?spm=1001.2014.3001.5501 "NFS搭建指南") mount模块参数 ![](https://i-blog.csdnimg.cn/direct/0af51758592c414d9d578cb501a6c27a.png) 1.1挂载nfs 1.2卸载nfs挂载 ansible all -m mount -a 'fstype=nfs src="192.168.60.129:/nfs-test" path=/mnt/nfs-gx129 state=absent' ![](https://i-blog.csdnimg.cn/direct/63d643acd67543c0a7c9df3400e5abf7.png) ## 六.用户管理模块 user ##### 6.1参数: ![](https://i-blog.csdnimg.cn/direct/115a1e222748485c996c231d0e34e484.png) ##### 6.2案例: **创建用户xxw** **普通命令** ![](https://i-blog.csdnimg.cn/direct/7fc74e51097740ddb6746b5a6771838f.png) ansible: ansible all -m user -a 'name=xxw uid=8888 shell=/bin/bash home=/home/xxw create_home=yes state=present' ![](https://i-blog.csdnimg.cn/direct/704dffbc4bcf4f15bf13f1437dafdf5f.png) **创建虚拟用户test1(不创建家目录 \&\& 命令解释器/sbin/nologin 指定uid=2004)** **普通命令: useradd -u2004 -s /sbin/nologin -M test1** **ansible:** ansible all -m user -a 'user=test2 uid=10086 shell=/sbin/nologin create_home=no state=present' ![](https://i-blog.csdnimg.cn/direct/cf24f3da01774c539c065908cf3ca6ad.png)

相关推荐
晓夜残歌19 分钟前
安全基线-rm命令防护
运维·服务器·前端·chrome·安全·ubuntu
leeezp3 小时前
配置固定ip绕过ip限制
运维·网络协议
trust Tomorrow5 小时前
Python-docx库详解:轻松实现Word文档自动化生成与图片尺寸控制
python·自动化·word
遇见火星5 小时前
docker-compose 快速搭建日志平台
linux·运维·centos
.m5 小时前
Linux怎样源码安装Nginx
linux·运维·nginx
weixin_433431445 小时前
centos【rockylinux】安装【supervisor】的注意事项【完整版】
linux·运维·centos
suzhou_speeder6 小时前
以太联—Intellinet 562133 6端口PoE++交换机:为多场景网络升级赋能
运维·网络·交换机·poe·poe交换机
Akamai中国6 小时前
应用商店上新:Couchbase Enterprise Server集群
运维·服务器·sql·云原生·云计算·云服务
晚风_END6 小时前
kubernetes|云原生|部署单master的kubernetes 1.25.5版本集群完全记录(使用contained 运行时)
java·运维·开发语言·云原生·容器·golang·kubernetes
三朝看客7 小时前
docker利用ollama +Open WebGUI在本地搭建部署一套Deepseek-r1模型
运维·docker·容器