Ansible概述、安装、命令基础、批量部署SSH密钥登录

ansible概述

Ansible是2013年推出的一款IT自动化和DevOps软件,目前由Redhat已签署Ansible收购协议。其是基于Python研发,糅合了很多老运维工具的优点。实现了批量操作系统配置,批量程序的部署,批量运行命令等功能。

 

为什么要选择ansible?

• ansible优点

    是仅需要ssh和Python即可使用

    无客户端

• ansible功能强大,模块丰富

• 上手容易门槛低

• 基于 python 开发,做二次开发更容易

• 使用公司比较多,社区活跃

 

ansible安装

环境准备:

virsh制作6台虚拟机(2G内存、2CPU、20G硬盘、root密码为123456)

  • 1台管理机ansible(192.168.4.10)
  • 另外5台虚拟机:
  1.  web1 (192.168.4.11) 
  2.  web2 (192.168.4.12)
  3.  db3    (192.168.4.13)
  4.  db4    (192.168.4.14)
  5.  cache5 (192.168.4.15)

[root@ansible ~]# cat /etc/yum.repos.d/local.repo    //自定义ansible的安装yum源

[public]

name=public

baseurl=ftp://192.168.4.254/public        //真实机ftp有ansible的rpm包及依赖包

enabled=1

gpgcheck=0

[centos7]

name=centos7

baseurl=ftp://192.168.4.254/centos7

enabled=1

gpgcheck=0

[root@ansible ~]# vim /etc/hosts      //设置6台机器IP与主机名对应关系

[root@ansible ~]# yum -y install ansible      //使用yum安装ansible(安装方式有源码编译、yum、pip等)

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

 

 

主机定义与分组

[root@ansible ~]# cd /etc/ansible/

[root@ansible ansible]# vim ansible.cfg       //修改ansible配置文件

inventory = /etc/ansible/hosts     //取消注释

[root@ansible ansible]# vim hosts          //设置需要管理的主机的分组

[web]

web1

web2

[db]

db3

db4

[cache]

cache5

[root@ansible ansible]# ansible web,db --list-hosts       //列出分组中的主机

hosts (4):

web1

web2

db3

db4

[root@ansible ansible]# ansible all --list-hosts           //列出所有主机

hosts (5):

web1

web2

cache5

db3

db4

 

ssh交互式密码登录远程主机

[root@ansible ansible]# vim ansible.cfg

:61

host_key_checking = False     //取消注释

[root@ansible ansible]# ansible web -m ping -k      //交互式登录

SSH password:             //输入密码123456

web2 | SUCCESS => {

"changed": false,

"ping": "pong"

}

web1 | SUCCESS => {

"changed": false,

"ping": "pong"

}

 

ssh非交互式登录远程主机

(先修改了db3、db4的root密码分别为a、b)

[root@ansible ansible]# vim hosts

[db]

db3 ansible_ssh_pass="a"

db4 ansible_ssh_pass="b"

[root@ansible ansible]# ansible db -m ping      //非交互式登录

db4 | SUCCESS => {

"changed": false,

"ping": "pong"

}

db3 | SUCCESS => {

"changed": false,

"ping": "pong"

}

注:ansible建立的是长连接。即客户端和服务端只用一个Socket对象,长期保持Socket的连接。socket文件在/root/.ansible/cp/

[root@ansible ansible]# ls /root/.ansible/cp/

6b13a69203

 

设置参数、子组

[root@ansible ansible]# vim hosts

[web]

web[1:2]

[web:vars]      //参数

ansible_ssh_pass="123456"

[db]

db3

db4

[app:children]   //子组

web

db

[cache]

cache5

 

ansible命令基础

命令格式:

ansible 主机或分组 -m 模块名 -a 模块的参数

  -M 指定模块路径

  -m 使用模块,默认 command 模块

  -a or --args 模块参数

  -i inventory 文件路径,或可执行脚本

  -k 使用交互式登录密码

  -e 定义变量

  -v 详细信息,-vvvv 开启 debug 模式

 

例如:

[root@ansible ~]# ansible web -m command -a 'uptime'

web2 | SUCCESS | rc=0 >>

09:00:52 up 12 min, 1 user, load average: 0.14, 0.15, 0.18

web1 | SUCCESS | rc=0 >>

09:00:52 up 12 min, 1 user, load average: 0.00, 0.13, 0.19

 

 

批量部署SSH密钥登录

原因:

• 交互输入密码较麻烦

• 密码写入配置文件安全性很差

• 不同远程主机如果密码不同,配置文件将很复杂

 

步骤:

给所有主机部署公钥:

[root@ansible ~]# vim  /etc/ansible/hosts    //先删除之前实验设置的密码

 

[root@ansible ~]# cd /root/.ssh/

[root@ansible .ssh]# ssh-keygen -t rsa -b 2048 -N ' ' //生成一对密钥。-t type 指定密钥算法,-b bits 指定密钥长度,对于RSA密钥,最小要求768位,默认是2048位。 -N new_passphrase 提供一个新的密语。

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

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:frfCsFFWF9bjKOrFpyGncn/8NBJTzbwqr1xEPb6BKPg root@ansible

The key's randomart image is:

+---[RSA 2048]----+

| oo    |

| ..o+o|

| . oo+=|

| . o.o.+.o|

| .S+o..= + |

| .+o.=..+o|

| oE*.*+ +|

| ..=+o++o.|

| o .=+... |

+----[SHA256]-----+

[root@ansible .ssh]# ls

authorized_keys   id_rsa   id_rsa.pub   known_hosts

 

[root@ansible .ssh]# ansible  all  -m  authorized_key -a  "user=root  exclusive=true  manage_dir=true key='$(</root/.ssh/id_rsa.pub)'"  -k    //批量给所有主机部署SSH密钥

SSH password:        //输入root密码(所有主机密码一样)

......

 

若报错

"msg": "Using a SSH password instead of a key is

not possible because Host Key checking is

enabled and sshpass does not support this.

Please add this host's fingerprint to your

known_hosts file to manage this host."

解决方法:

修改ansible.cfg

host_key_checking = False

 

posted on 2019-01-07 23:29  iouwenbo  阅读(1181)  评论(0编辑  收藏  举报

导航