ansible学习

ansible学习

介绍

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

前置环境准备:

配置密钥认证:

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

安装

yum install -y ansible

配置主机清单

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

测试结果

[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 指定模块的动作

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

[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

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

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

各个模块示例

command模块

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

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

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

[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模块

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

[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 目录

ansible all -m file -a "path=/opt/dev state=directory"
ansible all -m command -a "ls -ld /opt/dev"

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

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

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

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

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

ansible all -m file -a 'path=/opt/dev/test.txt owner=root group=root mode=755 state=touch'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

让美好继续发生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值