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'
相关推荐
hxung2 分钟前
MySQL面试学习
学习·mysql·面试
weixin_5025398523 分钟前
rust学习笔记5-所有权机制
笔记·学习·rust
PyAIGCMaster25 分钟前
50周学习go语言:第四周 函数与错误处理深度解析
开发语言·学习·golang
~kiss~33 分钟前
Rust学习~tokio简介
开发语言·学习·rust
StickToForever5 小时前
第4章 信息系统架构(五)
经验分享·笔记·学习·职场和发展
leegong231118 小时前
学习PostgreSQL专家认证
数据库·学习·postgresql
Moonnnn.9 小时前
51单片机学习——动态数码管显示
笔记·嵌入式硬件·学习·51单片机
南宫生10 小时前
力扣每日一题【算法学习day.132】
java·学习·算法·leetcode
技术小齐10 小时前
网络运维学习笔记 016网工初级(HCIA-Datacom与CCNA-EI)PPP点对点协议和PPPoE以太网上的点对点协议(此处只讲华为)
运维·网络·学习
竹言笙熙10 小时前
代码审计初探
学习·web安全