Centos7安装ansible
CentOS下部署Ansible自动化工具
1.确保机器上安装的是 Python 2.6 或者 Python 2.7 版本:
python -V

2.查看yum仓库中是否存在ansible的rpm包
yum list|grep ansible
若不存在或是低版本可更换yum源或者采用源码安装
阿里云的yum源:http://mirrors.aliyun.com/repo/ 备份源文件,然后下载对应的版本至/etc/yum.repos.d/目录即可,如epel-6.repo (通过该yum源安装ansible会依赖python 2.6,如果python版本是2.7以上可能会安装失败)

3.安装ansible服务:
yum install ansible -y
4.修改ansible配置和主机列表hosts:
1)关闭第一次使用ansible连接客户端时输入命令提示:
sed -i "s@\#host_key_checking = False@host_key_checking = False@g" /etc/ansible/ansible.cfg
指定日志路径:
sed -i "s@\#log_path = \/var\/log\/ansible.log@log_path = \/var\/log\/ansible.log@g" /etc/ansible/ansible.cfg
2)将所有主机ip加入到/etc/ansible/hosts文件中:
定义主机组和主机

默认ssh的端口为22端口,如果为其他端口号,可在主机名后面加上端口号,如 192.168.159.131:9604 ,也可以修改配置文件中的remote_port变量值
/etc/ansible/hosts也可以定义一个主机范围,如192.168.159.[100:200] ,表示192.168.159.100 - 192.168.159.200 的主机
5.创建和配置 SSH 公钥认证(免密码登录):
ssh-keygen -t rsa
按提示使用默认配置一路回车即可:

6.通过ansible将公钥分发至各主机上:
ansible all -m authorized_key -a "user=root key='{{ lookup('file', '/root/.ssh/id_rsa.pub') }}' path=/root/.ssh/authorized_keys manage_dir=no" --ask-pass -c paramiko

需要输入主机的密码,若是有的主机密码不一致,那么该主机会分发失败,此时只需再执行一遍命令输入该主机密码即可。或者先将密码相同的主机进行分组,然后依次指定主机组执行命令分批分发公钥。
此命令是通过追加的方式来推送公钥至authorized_keys,所以不用担心原来的文件内容会被覆盖。
到任意一台主机上查看,可以看到公钥已成功推送:

7.修改ansible配置,指定私钥文件路径:
sed -i "s@\#private_key_file = \/path\/to\/file@private_key_file = \/root\/.ssh\/id_rsa@g" /etc/ansible/ansible.cfg
8.测试:

可以在命令后面加上-vvvv参数查看详细的输出结果,尤其是在命令执行失败需要排错的时候非常有用。
9.自动化安装脚本:
注意:
①执行脚本前需要配置/root/hosts主机列表文件,内容如:
[root@localhost ~]# cat /root/hosts
[app]
192.168.159.130
[web]
192.168.159.131
②脚本分发公钥至远程主机时会提示输入远程主机的密码
③脚本通过yum方式安装
脚本内容:
#!/bin/bash
#
#############################################
# author:ellen
# describes:自动化安装和配置ansible
# version:v1.0
# updated:20170531
#############################################
#
# 主机列表文件
hostfile='/root/hosts'
# 错误信息以红色显示
function _err
{
echo -e "\033[1;31m[ERROR] $@\033[0m"
}
# 一般信息以绿色显示
function _info
{
echo -e "\033[1;32m[Info] $@\033[0m"
}
# 仅限指定用户运行本脚本
if [ $EUID != "0" ];then
echo "Please use root run script!!!"
exit 1
fi
rpm -qa|grep ansible
if [ $? -eq 0 ];then
_err "ansible 已存在,无需重复安装!退出..."
exit 1
fi
if [ -e $hostfile ];then
yum list|grep ansible
if [ $? -ne 0 ];then
_err "仓库不存在ansible的rpm包,退出..."
exit 1
else
yum install ansible -y
if [ $? -eq 0 ];then
_info "ansible 安装完毕..."
sed -i "s@\#host_key_checking = False@host_key_checking = False@g" /etc/ansible/ansible.cfg
sed -i "s@\#log_path = \/var\/log\/ansible.log@log_path = \/var\/log\/ansible.log@g" /etc/ansible/ansible.cfg
cp $hostfile /etc/ansible/hosts
_info "$hostfile 已拷贝至 /etc/ansible/目录"
ssh-keygen -t rsa -P '' -f /root/.ssh/id_rsa
_info "请按以下提示输入 ${hostfile} 列表中的主机密码:"
ansible all -m authorized_key -a "user=root key='{{ lookup('file', '/root/.ssh/id_rsa.pub') }}' path=/root/.ssh/authorized_keys manage_dir=no" --ask-pass -c paramiko
sed -i "s@\#private_key_file = \/path\/to\/file@private_key_file = \/root\/.ssh\/id_rsa@g" /etc/ansible/ansible.cfg
_info "ansible 已部署完毕!"
else
_err "ansible 安装失败!"
fi
fi
else
_err "$hostfile 主机列表文件不存在,请检查!"
exit 1
fi
ansible命令:
在另外两台机上执行ping命令:ansible group1 -m ping
复制本机文件到组group1:ansible group1 -m copy -a “src=/etc/hosts dest=/etc/”
帮group1安装软件:ansible group1 -m shell -a “yum install wget -y”
ansible myservers -a 'pwd
ansible myservers -m script -a "/opt/app/target.sh"
参考:https://www.linuxidc.com/Linux/2017-06/144430.htm
Centos7安装ansible的更多相关文章
- [svc][op]pip安装ansible && yum安装python34
相对yum安装,pip安装的好处是jinjia版本到了2.8 pip安装ansible Successfully installed MarkupSafe-1.0 PyYAML-3.12 ansibl ...
- centos7安装与配置ansible
此次测试总共有三台机,分别如下: ansible服务器:10.0.0.20 client01:10.0.0.21 client02:10.0.0.22 一.安装ansible 1. python版本需 ...
- Centos7安装配置ansible运维自动化工具
准备至少两台机器 Centos7,这两台机器都关闭 selinux IP:106.13.118.132 服务端(ansible) masterIP:148.70.60.244 节点 slaver 服务 ...
- centos7下安装ansible
由于centos7预装了python,因此我们可以跳过python的安装环节(记得关闭防火墙) [root@model ~]# [root@model ~]# python --version Pyt ...
- 容器centos7安装部署ansible
容器centos7安装部署ansible centos镜像版本及ansible版本 centos:centos7.5.1804 ansible:2.9.11 启动容器并进入容器 docker run ...
- CentOS7.5安装Ansible
安装ansible: 查看可用的ansible版本: yum list|grep ansible 方法一: 系统可用ansible版本太低,安装epel源: yum install epel-re ...
- Centos7使用yum快速安装ansible
ansible功能简介:ansible可以实现批量系统配置.批量软件部署.批量文件拷贝.批量运行命令等功能.主要基于ssh实现连接各个被控制端 yum默认安装的ansible,常用的配置文件有两个,一 ...
- CentOS7系统 ansible自动化部署多台服务器部署
CentOS7系统 ansible自动化部署多台服务器部署 Ansible工作机制 从图中可以看出ansible分为以下几个部份: 1> Control Node:控制机器2> In ...
- ansible笔记(1)在centos中安装ansible
ansible笔记():ansible的基本概念 一些基础概念 ansible是什么? 它是一个"配置管理工具",它是一个"自动化运维工具",如果你没有使用过任 ...
随机推荐
- 20170928xlVBA自定义分类汇总
SubtotalByCQL Range("A1:E100").Value, "Select 1,2,Sum(4),Count(4) GroupBy 1,2", ...
- es-aggregations聚合分析
聚合分析的格式: "aggregations" : { "<aggregation_name>" : { "<aggregation ...
- json -- dump load dumps loads 简单对比
json.dumps是将一个Python数据类型列表进行json格式的编码解析, 示例如下: >>> import json #导入python 中的json模块>>&g ...
- jquery 根据自定义属性选择
<div myattr="test">text</div> 使用$("div[myattr='test']")进行选择
- 378. Kth Smallest Element in a Sorted Matrix(java,优先队列)
题目: Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the ...
- 百度基础架构组-实习生面试(2016.08 java后台开发)
一.项目 1.Spring MVC与Struts2的区别: 2.MVC三层是如何工作的?比如:要访问一个Url?a=xx&b=xx,怎么找到相应的资源,怎么运算,怎么返回等? 3.数据库myb ...
- ajax被cancel问题(事件冒泡)
发送ajax请求的时候发现ajax请求总是被cancel,但是请求却被执行了,查阅了知识之后,发现问题是:事件冒泡,记录下来,供自己和大家学习借鉴. 1. 前提,发出ajax的请求在form表单中 2 ...
- vue 监听store中的数值
computed: { isFollow () { return this.$store.state.demo.id; //需要监听的数据 } }, watch: { isFo ...
- Git merge && git rebase的用法
Git merge的用法: git merge Dev // Dev表示某分支,表示在当前分支合并Dev分支 git merge -m “Merge from Dev” Dev //-m可以加上m ...
- android开发环境搭建教程
首先安装jdk,然后下载android studio,双击安装即可. 官网:http://www.android-studio.org/ 直接下载链接:https://dl.google.com/dl ...