Ansible安装和基本配置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_37038498/article/details/53640872

Ansible安装和基本配置

环境介绍

版本 hostname IP 端口
Centos6.5 server 192.168.1.1 2333
Centos6.5 client 192.168.1.2 2333

Ansible安装
建议使用python2.4以上版本,这里使用yum安装ansible(版本为2.0)

yum -y install ansible

如果yum源中没有ansible相关软件包,可以考虑使用其他yum源(这里使用阿里的yum源)

wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-6.repo

配置文件
配置文件默认位置为/etc/ansible/ansible.cfg

[defaults]
#指定主机清单所在文件夹(一般我们的主机分为多个类别,如果放到一个host中不方便管理,所以每一类主机最好单独使用一个hosts清单)
inventory      = /etc/ansible/inventory/
#并发数:默认为5
forks          = 5
#ansible默认在连接被控制机时,会在其家目录创建一个隐藏目录,用来执行命令,但是大多数企业是拒绝root用户登录的,所以这里我们使用普通用户,创建的隐藏目录放置在/tmp下
remote_tmp     = /tmp/.ansible/tmp
remote_port    = 2333
remote_user = ansibleuser
#ansible在第一次连接受控节点的时候,会输入yes,这里设置为False,可以忽略
host_key_checking = False
log_path = /var/log/ansible/ansible.log

host清单
常用的清单书写规范

起别名:

# 定义别名在执行ansible命令时便于我们判断和平时维护*
hn_db_master_01    ansible_ssh_host=192.168.1.1

分组:

# 定义分组方便管理*
[db_master]
hn_db_master_01 ansible_ssh_host=192.168.1.1
hn_db_master_03 ansible_ssh_host=192.168.1.3
hn_db_master_05 ansible_ssh_host=192.168.1.5
[db_slave]
hn_db_slave_02 ansible_ssh_host=192.168.1.2
hn_db_slave_04 ansible_ssh_host=192.168.1.4
hn_db_slave_06 ansible_ssh_host=192.168.1.6
[hn_db_all]
db_master
db_slave

我们可以将不同类型的主机写到不同host清单中
:db_hosts 、web_hosts 、 haproxy_hsots

ansible如何管理服务器

如果使用普通用户连接则需要给予sudo权限*

vim /etc/sudoers
......
#不使用tty线路也可以登录
#Defaults    requiretty
......
Defaults   visiblepw
......
#根据具体需求给予相应的权限
ansibleuser ALL=NOPASSWD: ALL

ansible使用ssh连接服务器常用的方法有两种:
1.使用ssh秘钥连接

# ssh-keygen(回车回车再回车)
ssh-copy-id -i ~/.ssh/id_rsa.pub "-p 2333 [email protected]"

2.使用密码连接

# 直接在host清单中添加密码(如下)
hn_db_master_01    ansible_ssh_host=192.168.1.2 ansible_ssh_pass="123456"

猜你喜欢

转载自blog.csdn.net/weixin_37038498/article/details/53640872