ansible

1.1 介绍

ansible 是一个基于python开发的自动化运维工具

其他功能实现基于SSH远程连续服务

ansible可以实现批量系统配置,批量软件部署,批量软件拷贝,批量进行命令等功能

特点:

 

1.  不需要单独安装客户端(no agents),基于系统自带的sshd服务,sshd就相当于ansible的客户端

2.  不需要服务端(no servers)

3.  需要依靠大量的模块实现批量管理

4.  配置文件/etc/ansible/ansible.cfg

1.2 查看服务器的ssh端口变量

[root@m01 ~]# tree -F /etc/ansible/
/etc/ansible/
  ─ ansible.cfg    ####所有服务器的ssh端口变量
├── hosts        ####被ansible管理的服务器 ip地址列表
└── roles/
1 directory, 2 files
[root@m01 ~]# tree -F /etc/ansible/
/etc/ansible/
  ─ ansible.cfg    ####所有服务器的ssh端口变量
├── hosts        ####被ansible管理的服务器 ip地址列表
└── roles/
1 directory, 2 files
[root@m01 ~]# cp /etc/ansible/hosts{,.bak}
[root@m01 ~]# cat >/etc/ansible/hosts<<EOF
> [oldboy]
> 172.16.1.41
> 172.16.1.31
> 172.16.1.8
> EOF 
[root@m01 ~]# vim /etc/ansible/hosts
[oldboy]
172.16.1.41
172.16.1.31
172.16.1.8                                                        
"/etc/ansible/hosts" 4L, 44C written
[root@m01 ~]# tail /etc/ansible/hosts
[oldboy]
172.16.1.41
172.16.1.31
172.16.1.8

1.3 批量运行命令

[root@m01 ~]# ansible oldboy -a "uptime"
172.16.1.31 | SUCCESS | rc=0 >>
 10:54:15 up 1 day,  5:24,  3 users,  load average: 0.00, 0.00, 0.00
172.16.1.41 | SUCCESS | rc=0 >>
 10:56:59 up 1 day,  5:27,  4 users,  load average: 0.00, 0.00, 0.00
172.16.1.8 | SUCCESS | rc=0 >>
 10:53:45 up 1 day,  4:50,  4 users,  load average: 0.00, 0.00, 0.00
[root@m01 ~]# ansible oldboy -m command -a "whoami"
172.16.1.31 | SUCCESS | rc=0 >>
root
172.16.1.41 | SUCCESS | rc=0 >>SS
root
172.16.1.8 | SUCCESS | rc=0 >>
root

1.4 批量发送文件

[root@m01 ~]# ansible oldboy -m copy -a "src=/etc/hosts dest=/opt"
172.16.1.41 | SUCCESS => {
    "changed": true, 
    "checksum": "74fcddd8ebe76dc65ad8e976e6eb32b086e9b542", 
    "dest": "/opt/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "954fd3707b2c7c40ef4b1e9c293dd6d8", 
    "mode": "0644", 
    "owner": "root", 
    "size": 259, 
    "src": "/root/.ansible/tmp/ansible-tmp-1497915902.79-178637955680571/source", 
    "state": "file", 
    "uid": 0
}
172.16.1.8 | SUCCESS => {
    "changed": true, 
    "checksum": "74fcddd8ebe76dc65ad8e976e6eb32b086e9b542", 
    "dest": "/opt/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "954fd3707b2c7c40ef4b1e9c293dd6d8", 
    "mode": "0644", 
    "owner": "root", 
    "size": 259, 
    "src": "/root/.ansible/tmp/ansible-tmp-1497915902.69-210664837965054/source", 
    "state": "file", 
    "uid": 0
}
172.16.1.31 | SUCCESS => {
    "changed": true, 
    "checksum": "74fcddd8ebe76dc65ad8e976e6eb32b086e9b542", 
    "dest": "/opt/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "954fd3707b2c7c40ef4b1e9c293dd6d8", 
    "mode": "0644", 
    "owner": "root", 
    "size": 259, 
    "src": "/root/.ansible/tmp/ansible-tmp-1497915902.69-35958861409844/source", 
    "state": "file", 
    "uid": 0
}


[root@m01 ~]# ansible oldboy -a "ls -l /opt"
172.16.1.31 | SUCCESS | rc=0 >>
total 8
-rw-r--r--  1 root root  259 Jun 17 11:19 hosts
drwxr-xr-x. 2 root root 4096 Mar 26  2015 rh

172.16.1.41 | SUCCESS | rc=0 >>
total 8
-rw-r--r--  1 root root  259 Jun 17 11:22 hosts
drwxr-xr-x. 2 root root 4096 Mar 26  2015 rh

172.16.1.8 | SUCCESS | rc=0 >>
total 8
-rw-r--r--  1 root root  259 Jun 17 11:19 hosts
drwxr-xr-x. 2 root root 4096 Mar 26  2015 rh



[root@m01 ~]# ansible oldboy -m copy -a "src=/etc/hosts  dest=/opt/oldboy/oldgirl/"
172.16.1.41 | SUCCESS => {
    "changed": true, 
    "checksum": "74fcddd8ebe76dc65ad8e976e6eb32b086e9b542", 
    "dest": "/opt/oldboy/oldgirl/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "954fd3707b2c7c40ef4b1e9c293dd6d8", 
    "mode": "0644", 
    "owner": "root", 
    "size": 259, 
    "src": "/root/.ansible/tmp/ansible-tmp-1497916205.5-173882671966895/source", 
    "state": "file", 
    "uid": 0
}
172.16.1.8 | SUCCESS => {
    "changed": true, 
    "checksum": "74fcddd8ebe76dc65ad8e976e6eb32b086e9b542", 
    "dest": "/opt/oldboy/oldgirl/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "954fd3707b2c7c40ef4b1e9c293dd6d8", 
    "mode": "0644", 
    "owner": "root", 
    "size": 259, 
    "src": "/root/.ansible/tmp/ansible-tmp-1497916205.62-113714054367459/source", 
    "state": "file", 
    "uid": 0
}
172.16.1.31 | SUCCESS => {
    "changed": true, 
    "checksum": "74fcddd8ebe76dc65ad8e976e6eb32b086e9b542", 
    "dest": "/opt/oldboy/oldgirl/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "954fd3707b2c7c40ef4b1e9c293dd6d8", 
    "mode": "0644", 
    "owner": "root", 
    "size": 259, 
    "src": "/root/.ansible/tmp/ansible-tmp-1497916205.32-138567636803912/source", 
    "state": "file", 
    "uid": 0
}
[root@m01 ~]# ansible oldboy  -a "tree /opt"
172.16.1.31 | SUCCESS | rc=0 >>
/opt
├── hosts
├── oldboy
│   └── oldgirl
│       └── hosts
└── rh

3 directories, 2 files

172.16.1.41 | SUCCESS | rc=0 >>
/opt
├── hosts
├── oldboy
│   └── oldgirl
│       └── hosts
└── rh

3 directories, 2 files

172.16.1.8 | SUCCESS | rc=0 >>
/opt
├── hosts
├── oldboy
│   └── oldgirl
│       └── hosts
└── rh
3 directories, 2 files

[root@m01 ~]# ansible oldboy -m copy -a "src=/etc/hosts  dest=/opt/oldboy/oldgirl/ backup=yes"
172.16.1.31 | SUCCESS => {
    "changed": false, 
    "checksum": "74fcddd8ebe76dc65ad8e976e6eb32b086e9b542", 
    "dest": "/opt/oldboy/oldgirl/hosts", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "path": "/opt/oldboy/oldgirl/hosts", 
    "size": 259, 
    "state": "file", 
    "uid": 0
}
172.16.1.41 | SUCCESS => {
    "changed": false, 
    "checksum": "74fcddd8ebe76dc65ad8e976e6eb32b086e9b542", 
    "dest": "/opt/oldboy/oldgirl/hosts", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "path": "/opt/oldboy/oldgirl/hosts", 
    "size": 259, 
    "state": "file", 
    "uid": 0
}
172.16.1.8 | SUCCESS => {
    "changed": false, 
    "checksum": "74fcddd8ebe76dc65ad8e976e6eb32b086e9b542", 
    "dest": "/opt/oldboy/oldgirl/hosts", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "path": "/opt/oldboy/oldgirl/hosts", 
    "size": 259, 
    "state": "file", 
    "uid": 0
}

1.5 批量运行脚本(shell)

运行脚本  命令 特殊符号

[root@m01 ~]#echo 'yum install -y ipvsadm' >/server/scripts/yum.sh
##1.先把脚本发送到对应的服务
[root@m01 ~]#ansible oldboy -m copy -a "src=/server/scripts/yum.sh dest=/server/scripts/ mode=755"
[root@m01 ~]#ansible oldboy -a "ls -l /server/scripts/yum.sh"
##2.运行 /bin/sh 
ansible oldboy -m shell -a "/bin/sh /server/scripts/yum.sh"

1.6 scripts 模块

 

1.6.1.把脚本发送到对应的服务器上面

1.6.2.运行脚本

[root@m01 ~]#nsible oldboy -m script -a "/server/scripts/yuan.sh"
写一个脚本 更改所有服务器的yum源
[root@m01 ~]# cat >>/server/scripts/yuan.sh<<EOF
> wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
> wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
> EOF
[root@m01 ~]# ansible oldboy -m script -a "/server/scripts/yuan.sh"
略
[root@m01 ~]#  ansible oldboy -a "yum repolist"

1.7 批量添加定时任务

[root@m01 ~]# ansible oldboy -m cron -a 'name="restart network" minute=00 hour=00 job="/etc/init.d/network restart >/dev/null 2>&1" state=present'   -C
172.16.1.31 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "hello", 
        "restart network"
    ]
}
172.16.1.8 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "hello", 
        "restart network"
    ]
}
172.16.1.41 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "hello", 
        "restart network"
    ]
}
[root@m01 ~]# ansible oldboy -a "tail -2 /var/spool/cron/root"
172.16.1.31 | SUCCESS | rc=0 >>
#Ansible: hello
*/2 * * * * echo hello >>/tmp/oldboy.log 2>&1

172.16.1.41 | SUCCESS | rc=0 >>
#Ansible: hello
*/2 * * * * echo hello >>/tmp/oldboy.log 2>&1

172.16.1.8 | SUCCESS | rc=0 >>
#Ansible: hello
*/2 * * * * echo hello >>/tmp/oldboy.log 2>&1

[root@m01 ~]# ansible oldboy -m cron -a 'name="restart network" minute=00 hour=00 job="/etc/init.d/network restart >/dev/null 2>&1" state=present'   
172.16.1.8 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "hello", 
        "restart network"
    ]
}
172.16.1.31 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "hello", 
        "restart network"
    ]
}
172.16.1.41 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "hello", 
        "restart network"
    ]
}
[root@m01 ~]# ansible oldboy -a "tail -2 /var/spool/cron/root"
172.16.1.8 | SUCCESS | rc=0 >>
#Ansible: restart network
00 00 * * * /etc/init.d/network restart >/dev/null 2>&1

172.16.1.41 | SUCCESS | rc=0 >>
#Ansible: restart network
00 00 * * * /etc/init.d/network restart >/dev/null 2>&1

172.16.1.31 | SUCCESS | rc=0 >>
#Ansible: restart network
00 00 * * * /etc/init.d/network restart >/dev/null 2>&1	

1.8 批量删除

[root@m01 ~]# ansible oldboy -m cron -a 'name="restart network"  state=absent' -C   
172.16.1.8 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "hello"
    ]
}
172.16.1.41 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "hello"
    ]
}
172.16.1.31 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "hello"
    ]
}
[root@m01 ~]# ansible oldboy -m cron -a 'name="restart network"  state=absent' 
172.16.1.31 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "hello"
    ]
}
172.16.1.41 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "hello"
    ]
}
172.16.1.8 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "hello"
    ]
}
[root@m01 ~]# ansible oldboy -a "tail -2 /var/spool/cron/root"
172.16.1.8 | SUCCESS | rc=0 >>
#Ansible: hello
*/2 * * * * echo hello >>/tmp/oldboy.log 2>&1

172.16.1.41 | SUCCESS | rc=0 >>
#Ansible: hello
*/2 * * * * echo hello >>/tmp/oldboy.log 2>&1

172.16.1.31 | SUCCESS | rc=0 >>
#Ansible: hello
*/2 * * * * echo hello >>/tmp/oldboy.log 2>&1

1.9 总结

 

command(默认的模块):执行命令模块

shell:执行shell脚本模块

script:把脚本发到客户端,然后执行

copy:把本地文件发送到远端

file:设定文件属性模块

service:系统服务管理模块

cron:计划任务管理模块

yum: yum软件包安装管理模块

synchronize: 使用rsync同步文件模块

 

ansibel oldboy -m service -a"name=crond state=started enabled=yes"

ansible 主机组  模块 操作的内容

 

1.10 ansible 特点

 批量管理

批量部署

ad-hoc 批量执行命令

 

ansible oldboy -m command  -a   "hostname"

编写playbook剧本-脚本

 

linux功能

ansible模块

执行脚本

command

执行脚本

shell

把脚本从本地发到客户端,然后在客户端执行

script

远程传送文件(把本地发到别的服务器)

copy

定时任务

cron

 

 

 

 

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值