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'
相关推荐
程序员清洒2 小时前
Flutter for OpenHarmony:GridView — 网格布局实现
android·前端·学习·flutter·华为
喜欢吃燃面2 小时前
Linux:环境变量
linux·开发语言·学习
代码游侠2 小时前
ARM开发——阶段问题综述(二)
运维·arm开发·笔记·单片机·嵌入式硬件·学习
云边散步6 小时前
godot2D游戏教程系列二(4)
笔记·学习·游戏开发
jrlong6 小时前
DataWhale大模型基础与量化微调task4学习笔记(第 2 章:高级微调技术_RLHF 技术详解)
笔记·学习
Darkershadow6 小时前
蓝牙学习之Time Set
python·学习·蓝牙·ble·mesh
好奇龙猫7 小时前
【日语学习-日语知识点小记-日本語体系構造-JLPT-N2前期阶段-第一阶段(9):単語文法】
学习
AI浩7 小时前
约束模型下的目标检测置信学习
学习·目标检测·目标跟踪
m0_748229997 小时前
ThinkPHP快速入门:从零到实战
c语言·开发语言·数据库·学习
風清掦8 小时前
【江科大STM32学习笔记-04】0.96寸OLED显示屏
笔记·stm32·学习