centos7使用yum配置使用ansible

概述
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
原文地址:https://www.cnblogs.com/sdhzdtwhm/p/9619169.html