ansible自动化运维实例

实验环境
1.linux-1 ansible 192.168.10.1    

2.linux-2 client1 192.168.10.2

3.linux-2 client2 192.168.10.3


            ----基础环境部署----
            
1. ansible 安装

安装tree命令---便于查看ansible工作目录
[root@localhost ~]# yum -y install tree


上传ansible相关软件,创建本地yum仓库
[root@localhost ~]# mv  ansiblerepo/ /usr/src/

[root@localhost ~]# du -sh /usr/src/ansiblerepo/
83M    /usr/src/ansiblerepo/

[root@localhost ~]# vim /etc/yum.repos.d/local.repo 

[local]
name=centos7.4
baseurl=file:///usr/src/ansiblerepo
enabled=1
gpgcheck=0


[root@localhost ~]# yum clean all

安装ansible
[root@localhost ~]# yum -y install ansible

查看ansible版本
[root@localhost ~]# ansible --version

ansible 2.3.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]

2.为了便于管理远程主机,配置ssh信任  

[root@localhost ~]# ssh-keygen -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): ##公钥和私钥默认保存位置
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): ##可设置验证密码,默认
Enter same passphrase again:  ##默认
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Jws2KHO5W171BF6gKDN9Oudo0D2VKmYNRJKxKbQfsmA [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|  . o+o   .      |
| . ..* . . o     |
|.E+ B + o + .    |
|.. = O * + o     |
|  + * @ S + .    |
|   + * O * o     |
|    . + +   .    |
|     = .         |
|    . .          |
+----[SHA256]-----+

传输私钥到远程主机
[root@localhost ~]# ssh-copy-id [email protected]

根据提示输入:
yes

输入:root密码

[root@localhost ~]# ssh-copy-id [email protected]


验证:无须输入密码即可远程登录
[root@localhost ~]# ssh 192.168.10.2

查看ip是否是远程主机
[root@localhost ~]# ip a

[root@localhost ~]# exit


[root@localhost ~]# ssh 192.168.10.3

[root@localhost ~]# ip a

[root@localhost ~]# exit


            ----ansilble基础管理------
    yum install -y ansible     --- 需要依赖epel的yum源
    /etc/ansible/ansible.cfg   --- ansible服务配置文件
       /etc/ansible/hosts         --- 主机清单文件   定义可以管理的主机信息
       /etc/ansible/roles         --- 角色目录???
    
            
1. Ansible清单管理/etc/ansible/hosts

inventory文件通常用于定义要管理主机的认证信息, 例如ssh登录用户名、密码以及key相关信息。

主机

1.支持主机名通配以及正则表达式,例如web[1:3].oldboy.com
2.支持基于非标准的ssh端口,例如web1.oldboy.com:6666
3.支持指定变量,可对个别主机的特殊配置,如登陆用户,密码等

主机组

1.支持嵌套组,例如[game:children],那么在game模块下面的组都会被game所包含
2.支持指定变量,例如[game:vars]在下面指定变量

[root@localhost ~]# vim /etc/ansible/hosts

[web]   ## 主机组
192.168.10.2
192.168.10.3

[test01]
www.bdqn.cn:222   ##通过端口222管理设备

[mail]
ly01.mail.cn

[ly]
ly[2:5].test.com  ##正则表达式:表示4台主机ly2,ly3,ly4,ly5..

验证:可以对组或组中成员进行远程操作
--limit 限制为组中成员

只对web组中192.168.10.2主机进行命令操作
[root@localhost ~]# ansible web -m command -a "ip a" --limit "192.168.10.2"

192.168.10.2 | SUCCESS | rc=0 >>
……………………


通过指定主机来远程操作
[root@localhost ~]# ansible 192.168.10.3 -m command -a "ip a"

192.168.10.3 | SUCCESS | rc=0 >>

通过通配符来指定多个主机远程操作
[root@localhost ~]# ansible 192.168.10.* -m command -a "firewall-cmd --state"

192.168.10.2 | FAILED | rc=252 >>
not running

192.168.10.3 | FAILED | rc=252 >>
not running


------------------------------------------
Ansible注意事项->提示颜色信息说明

黄色:对远程节点进行相应修改
绿色:对远程节点不进行相应修改,或者只是对远程节点信息进行查看
红色:操作执行命令有异常
紫色:表示对命令执行发出警告信息(可能存在的问题,给你一下建议)

-----------------------------------------------------

2.ansible命令
ansible应用场景:多用于临时的、无规律的任务操作
--非固化需求
--临时一次性操作
--二次开发接口调用

检查清单中所有主机是否存活
-f  并发线程数为5
-m   调用ping模块(并不是ping命令)

[root@localhost ~]# ansible all -f 5 -m ping

## success---表示成功; => {} 表示返回结果
## changed false 表示没有对主机进行更改
##pong ---ping模块返回结果

192.168.10.3 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.10.2 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}


列出web组主机列表
[root@localhost ~]# ansible web --list
  hosts (2):
    192.168.10.2
    192.168.10.3

批量显示web组磁盘使用情况    
[root@localhost ~]# ansible web -m command -a "df -hT"


3.ansible-doc  可用来查询模块文档的说明,类似于man

列出所有模块信息
[root@localhost ~]# ansible-doc -l

查询ping模块的说明信息
[root@localhost ~]# ansible-doc ping


4.ansible-console 交互式工具。类似于cmd或shell

[root@localhost ~]# ansible-console
Welcome to the ansible console.
Type help or ? to list commands.

root@all (8)[f:5]$ cd web  ##通过cd命令切换主机或分组
root@web (2)[f:5]$ list    ##列出当前设备
192.168.10.2
192.168.10.3
root@web (2)[f:5]$ 


     --------------ansible常用模块------

Shell功能全面但是执行率低
command不支持:逻辑运算符、条件判断符号、重定向命令或者是管道命令


1.command模块---远程执行命令     
chdir---在远程主机运行命令前,要提前进入的目录
creates---创建文件(如文件已存在,则不执行)
removes--移除文件(如文件不存在,则不执行)
executable--更改shell环境(并且执行命令时要使用绝对路径)


在web组主机上运行命令,运行前切换到/root目录
[root@localhost ~]# ansible web -m command -a "chdir=/root  ls ./"

192.168.10.3 | SUCCESS | rc=0 >>
anaconda-ks.cfg
:wq

192.168.10.2 | SUCCESS | rc=0 >>
anaconda-ks.cfg
:wq


2.shell模块---相当于调用远程主机的shell进程,在该shell下打开一个子shell运行命令

[root@localhost ~]# ansible web -m shell -a 'echo "hello ly" >> /tmp/hello.txt'
192.168.10.3 | SUCCESS | rc=0 >>


192.168.10.2 | SUCCESS | rc=0 >>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值