概述
ansible是常用配置管理工具,使用方便。本文用來介紹centos7如何配置ansible
1 若服務器可以訪問互聯網
1.1 安裝epel源
rpm -ivh https://mirror.webtatic.com/yum/el7/epel-release.rpm
1.2 安裝ansible
yum install ansible -y
1.3 修改配置文件
vi /etc/ansible/ansible.cfg
取消下行注釋:
host_key_checking = False
1.4 配置ansible組信息

2.若服務器不能訪問互聯網
附件百度雲下載地址:鏈接: https://pan.baidu.com/s/1iSONzgqmLKUaU7azkL76vw 密碼: sjar
注:此附件ansible.zip是我在一台能訪問互聯網的服務器配置epel源使用
下載rpm包:yum install --downloadonly --downloaddir=/root/ansible/ ansible
使用createrepo命令進行創建:cd /root/ansible && createrepo .
2.1 上傳附件ansible.zip,並解壓至 /root/下,

2.2 解壓后的內容

2.3 添加ansible.repo 至/etc/yum.repos.d
cat ansible.repo
[ansible]
name=ansible
baseurl=file:///root/ansible
enabled=1
gpgcheck=0
2.4 安裝ansible
yum install ansible -y
2.5 修改配置文件
vi /etc/ansible/ansible.cfg
取消下行注釋:
host_key_checking = False
ansible批量執行程序輸出日志亂碼問題(參考地址:
http://www.mamicode.com/info-detail-2022757.html)
vi /etc/ansible/ansible.cfg
#module_lang = C
#module_set_locale = False
module_lang = zh_CN.UTF-8
module_set_locale = True
2.6 定義ansible組

3 ansible命令示例
3.1 copy模塊使用
ansible mvtech -m copy -a "src=/root/zabbix_agentd.conf dest=/root/ owner=root group=root mode=0700" -f 10
3.2 shell模塊使用
ansible mvtech -m shell -a "rpm -ivh /root/zabbix-agent-2.4.8-1.el6.x86_64.rpm --nodeps" -f 10
3.3 快速配置主機互信
3.3.1 各服務器執行ssh-keygen
ansible mha -m shell -a "ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa" -f 10
3.3.2 生成authorized_keys文件
ansible mha -m shell -a "cat /root/.ssh/id_rsa.pub" -f 100 > /tmp/authorized_keys
3.3.3 去除多余的行
sed -i '/SUCCESS/d' /tmp/authorized_keys
3.3.4 分發authorized_keys至每台服務器
ansible mha -m copy -a "src=/tmp/authorized_keys dest=/root/.ssh/ owner=root group=root mode=0644" -f 10