ansible学习

ansible学习

介绍

Ansible是一个基于Python开发的自动化运维工具,它集合了众多运维工具(如puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。

前置环境准备:

配置密钥认证:

bash 复制代码
1.生成密钥
ssh-keygen -t rsa
2.将公钥复制到远程服务器中
ssh-copy-id 用户名@远程服务器IP地址

安装

bash 复制代码
yum install -y ansible

配置主机清单

bash 复制代码
vim /etc/ansible/hosts
[wujie]    ------>组的名称,主机分组
10.0.0.7    -------》被管理的机器ip
10.0.0.41
10.0.0.31
10.0.0.51

测试结果

bash 复制代码
[root@m01 ~]# ansible wujie -m ping
10.0.0.31 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
10.0.0.51 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
10.0.0.41 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
10.0.0.7 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

出现以上结果就说明ansible就安装完成了,并且运行的hello world也是没问题的了

模块

下面就来看一下ansible模块的使用

ansible的命令格式如下

ansible 主机分组 -m 指定模块 -a 指定模块的动作

如查看每台机器的主机名,就可以使用下面的命令

bash 复制代码
[root@m01 ~]# ansible wujie -m command -a 'hostname'
10.0.0.7 | CHANGED | rc=0 >>
web01
10.0.0.51 | CHANGED | rc=0 >>
db01
10.0.0.41 | CHANGED | rc=0 >>
backup
10.0.0.31 | CHANGED | rc=0 >>
nfs01

如果想查看某一台的机器的信息,可以使用如下命令

bash 复制代码
[root@m01 ~]# ansible 10.0.0.7 -m command -a 'hostname'
10.0.0.7 | CHANGED | rc=0 >>
web01

各个模块示例

command模块

仅⽀持简单命令,不⽀持特殊符号,管道

bash 复制代码
获取所有机器主机名
ansible all -a 'hostname'  #相当于省略 -m command
shell模块

与command模块类似,shell模块⽀持特殊符号,执⾏脚本

.bash 复制代码
[root@m01 ~]# ansible all -m shell -a "ip a s ens33|sed -n 3p"
10.0.0.31 | CHANGED | rc=0 >>
    inet 10.0.0.31/24 brd 10.0.0.255 scope global noprefixroute ens33
10.0.0.7 | CHANGED | rc=0 >>
    inet 10.0.0.7/24 brd 10.0.0.255 scope global noprefixroute ens33
10.0.0.51 | CHANGED | rc=0 >>
    inet 10.0.0.51/24 brd 10.0.0.255 scope global noprefixroute ens33
10.0.0.41 | CHANGED | rc=0 >>
    inet 10.0.0.41/24 brd 10.0.0.255 scope global noprefixroute ens33
script模块

传输脚本到被管理端并执⾏脚本,⼀般⽤于执⾏脚本

bash 复制代码
[root@m01 scripts]# cat net-tools-install.sh
yum install net-tools -y
[root@m01 scripts]# ansible all -m script -a "/root/work/scripts/net-tools-install.sh"
file模块

file 创建⽂件,⽬录

file模块中的选项
path 路径(⽬录,⽂件) 必须要写
src (source源) 源⽂件⼀般⽤于link(创建软连接模式) ⽤于指定源⽂件
state 状态(模式) state=directory 创建⽬录 state=file (默认) 更新⽂件,如果⽂件不存在也不创建. state=link 创建软连接 state=touch 创建⽂件 state=absent 删除

案例1:创建目录 /opt/dev 目录

bash 复制代码
ansible all -m file -a "path=/opt/dev state=directory"
ansible all -m command -a "ls -ld /opt/dev"

案例2: 创建文件 /opt/dev/test.txt

bahs 复制代码
ansible all -m file -a 'path=/opt/dev/test.txt state=touch'
ansible all -a "ls -l /opt/dev"

案例3:创建软连接/opt/dev/test.txt到/tmp/test.txt.soft

bash 复制代码
ansible all -m file -a "src=/opt/dev/test.txt path=/tmp/test.txt.soft state=link"

案例4:删除文件,目录,软连接

bash 复制代码
ansible all -m file -a 'path=/opt/dev/test.txt state=absent'  删除文件
ansible all -m file -a 'path=/opt/dev state=absent'   删除目录
ansible all -m file -a 'path=/tmp/test.txt.soft state=absent'  删除软连接

案例五:创建⽂件/opt/dev/test.txt,所有者root,⽤户组root,权限755

bash 复制代码
ansible all -m file -a 'path=/opt/dev/test.txt owner=root group=root mode=755 state=touch'
相关推荐
西岸行者9 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
悠哉悠哉愿意9 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
别催小唐敲代码10 天前
嵌入式学习路线
学习
毛小茛10 天前
计算机系统概论——校验码
学习
babe小鑫10 天前
大专经济信息管理专业学习数据分析的必要性
学习·数据挖掘·数据分析
winfreedoms10 天前
ROS2知识大白话
笔记·学习·ros2
在这habit之下10 天前
Linux Virtual Server(LVS)学习总结
linux·学习·lvs
我想我不够好。10 天前
2026.2.25监控学习
学习
im_AMBER10 天前
Leetcode 127 删除有序数组中的重复项 | 删除有序数组中的重复项 II
数据结构·学习·算法·leetcode
CodeJourney_J10 天前
从“Hello World“ 开始 C++
c语言·c++·学习