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'
相关推荐
盐水冰3 小时前
【烘焙坊项目】后端搭建(12) - 订单状态定时处理,来单提醒和顾客催单
java·后端·学习
Hello小赵3 小时前
视频压缩编码学习(一)—— 基础知识大集合
学习
似水明俊德4 小时前
02-C#.Net-反射-学习笔记
开发语言·笔记·学习·c#·.net
adore.9684 小时前
3.18 复试学习
学习
留白_5 小时前
MySQL学习(9)——索引
学习
请你喝好果汁6415 小时前
生信学习笔记:ArchR 处理小麦单细胞 ATAC-seq 中的细胞数差异与 Embedding 报错调试
学习
jinanwuhuaguo6 小时前
OpenClaw、飞书、Claude Code、Codex:四维AI生态体系的深度解构与颗粒化对比分析
大数据·人工智能·学习·飞书·openclaw
萨文 摩尔杰6 小时前
GPS原理学习
学习·fpga开发
fengci.7 小时前
ctfshow(web入门)295-300
java·开发语言·学习
renhongxia18 小时前
多模态融合驱动下的具身学习机制研究
运维·学习·机器人·自动化·知识图谱