Ansible安装及配置

ansible安装及配置

1、install the newest 'epel-release' package
 
 el7 : rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
 el6 : rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm

2、install ansible

 sudo yum install ansible

3、ansible 配置及测试

(1)修改主机与组配置
 vi /etc/ansible/hosts

# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
#   - Comments begin with the '#' character
#   - Blank lines are ignored
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or ip addresses
#   - A hostname/ip can be a member of multiple groups

# Ex 1: Ungrouped hosts, specify before any group headers.

## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10
172.16.171.154
172.16.171.155

# Ex 2: A collection of hosts belonging to the 'webservers' group

## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110
172.16.171.154
172.16.171.155

# If you have multiple hosts following a pattern you can specify
# them like this:

## www[001:006].example.com

# Ex 3: A collection of database servers in the 'dbservers' group

## [dbservers]
## 
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57

# Here's another example of host ranges, this time there are no
# leading 0s:

## db-[99:101]-node.example.com

(2)通过ping模块测试主机的连通性,分别对单主机及组进行ping操作

单主机测试:
ansible 172.16.171.154 -m ping -k
由于主控端与被控主机为配置ssh证书信任,-k参数要求提供root账号密码,在提示“SSH password”时输入。

出现如下结果说明测试成功:
172.16.171.154 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}


主机组测试:
ansible webservers -m ping -k

出现如下结果说明测试成功:
172.16.171.155 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
172.16.171.154 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

4、配置Linux主机SSH无密码访问

(1)主控端主机生成秘钥
ssh-keygen -t rsa (有询问直接回车)

执行该命令后会在/root/.ssh/下生成一对秘钥,其中id_rsa为私钥,id_rsa.pub为公钥(需要下发到被控主机用户的.ssh目录,同时重命名为authorized_keys文件)。

(2)同步公钥文件id_rsa.pub到被控主机,使用ssh-copy-id命令,格式:/usr/bin/ssh-copy-id [-i [identity_file]] [user@]host
ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.16.171.154

(3)检验SSH无密码配置是否成功
ssh root@172.16.171.154 
如果直接进入root账户,说明配置成功











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值