目录
一、CentOS
- 设置YUM源
- 备份REPO :mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bk
- 下载对应版本repo文件,存入/etc/yum.repo.d/ 文件夹
- 推荐网易(163),中科大 (ustc),华为(Huawei)的yum源,各个地址以下供参考,如果失效,请再仔细查看网站说明。
- 【163】CentOS5 :http://mirrors.163.com/.help/CentOS5-Base-163.repo
CentOS6 :http://mirrors.163.com/.help/CentOS6-Base-163.repo
CentOS7 :http://mirrors.163.com/.help/CentOS7-Base-163.repo
- 【163】CentOS5 :http://mirrors.163.com/.help/CentOS5-Base-163.repo
更新方法: cd /etc/yum.repo.d/ && wget http://mirrors.163.com/.help/CentOS6-Base-163.repo && mv CentOS6-Base-163.repo CentOS-Base.repo
yum clean all && yum makecache
- 【中科大】只有CentOS 7, 8 的源
-
对于 CentOS 8,使用以下命令替换默认的配置
sudo sed -e 's|^mirrorlist=|#mirrorlist=|g' \ -e 's|^#baseurl=http://mirror.centos.org/$contentdir|baseurl=https://mirrors.ustc.edu.cn/centos|g' \ -i.bak \ /etc/yum.repos.d/CentOS-Linux-AppStream.repo \ /etc/yum.repos.d/CentOS-Linux-BaseOS.repo \ /etc/yum.repos.d/CentOS-Linux-Extras.repo \ /etc/yum.repos.d/CentOS-Linux-PowerTools.repo \ /etc/yum.repos.d/CentOS-Linux-Plus.repo
对于 CentOS 7,使用以下命令替换默认配置
sudo sed -e 's|^mirrorlist=|#mirrorlist=|g' \ -e 's|^#baseurl=http://mirror.centos.org/centos|baseurl=https://mirrors.ustc.edu.cn/centos|g' \ -i.bak \ /etc/yum.repos.d/CentOS-Base.repo
以上命令只替换了默认启用的仓库。替换之后请运行
yum makecache
更新缓存。 -
【华为】CentOS 7, 8 的源
-
CentOS的镜像地址为:https://repo.huaweicloud.com/centos/
1、备份配置文件:
cp -a /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
2、两种方案,请大家自行选取。
方案一:
下载新的CentOS-Base.repo文件到/etc/yum.repos.d/目录下,选择CentOS版本:
CentOS 8
执行如下命令:
wget -O /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-8-reg.repo
或者
wget -O /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-7-reg.repo
方案二:
修改CentOS-Base.repo文件,取消baseurl开头的行的注释,并增加mirrorlist开头的行的注释。将文件中的http://mirror.centos.org替换成https://repo.huaweicloud.com,可以参考如下命令:
sed -i "s/#baseurl/baseurl/g" /etc/yum.repos.d/CentOS-Base.repo
sed -i "s/mirrorlist=http/#mirrorlist=http/g" /etc/yum.repos.d/CentOS-Base.repo
sed -i "s@http://mirror.centos.org@https://repo.huaweicloud.com@g" /etc/yum.repos.d/CentOS-Base.repo
3、执行yum clean all清除原有yum缓存。
4、执行yum makecache(刷新缓存)或者yum repolist all(查看所有配置可以使用的文件,会自动刷新缓存)。
.安装,例如使用网易源:
1)安装epel源
yum install http://mirrors.163.com/centos/7.9.2009/extras/x86_64/Packages/epel-release-7-11.noarch.rpm
2)查看epel源并安装ansible
[root@an ~]ll /etc/yum.repos.d/epel*
[root@an ~]yum install ansible -y
3)查看ansible版本
ansible --version
二、Ubuntu
更换为国内软件源
#Ctrl + Alt + T进入命令行,命令需有sudo以获取权限。
sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup #备份原有配置文件
sudo gedit /etc/apt/sources.list #将原文件内容全部删除,替换为下面任意链接内镜像源(无图形界面建议gedit换成vim)
sudo apt-get update #保存后执行。等待更新完毕
#sudo apt-get update
#sudo apt-get upgrade
#update是更新软件列表,upgrade是更新软件
清华源:https://mirror.tuna.tsinghua.edu.cn/help/ubuntu/
中科大源:https://mirrors.ustc.edu.cn/repogen/
清华源
Ubuntu 的软件源配置文件是 /etc/apt/sources.list。将系统自带的该文件做个备份,将该文件替换为下面内容,即可使用 TUNA 的软件源镜像。
选择你的ubuntu版本:
21.04
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-security main restricted universe multiverse
# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-proposed main restricted universe multiverse
Ubuntu (/etc/apt/sources.list):
HTTPS
IPv4
hh (21.04)
Download
deb https://mirrors.ustc.edu.cn/ubuntu/ hh main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ hh main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ hh-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ hh-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ hh-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ hh-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ hh-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ hh-backports main restricted universe multiverse
## Not recommended
# deb https://mirrors.ustc.edu.cn/ubuntu/ hh-proposed main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ hh-proposed main restricted universe multiverse
APT 安装Ansible
sudo apt-get install software-properties-common
sudo apt-add-repository ppa:ansible/ansible
sudo apt-get update
sudo apt-get install ansible
sudo ansible --version
三、源码
- Ansible安装包下载: 官网(https://releases.ansible.com/ansible/)
- CentOS7源码安装Ansible2.X
-
1、安装Python 3.x环境 [root@ansible ~]# yum install -y python36 python36-pip git [root@ansible ~]# ln -s /usr/bin/pip36 /usr/bin/pip3 [root@ansible ~]# pip install virtualenv 2、配置virtualenv环境(可选) [deploy@ansible ~]$ useradd deploy && su - deploy [deploy@ansible ~]$ virtualenv -p /bin/python3.6 .py3-a2.5-env [deploy@ansible ~]$ source .py3-a2.5-env/bin/activate 3、从Github下载最新稳定版本代码 [root@ansible ~]# git clone -b stable-2.7 https://github.com/ansible/ansible.git 4、安装ansible Python依赖包 [root@ansible ~]# pip3 install paramiko PyYAML jinja2 5、安装ansible [root@ansible ~]# cd ansible/ [root@ansible ansible]# python3 setup.py install 6、创建Ansible配置文件,并开启Ansible日志(Ansible默认不启用日志) [root@ansible ~]# mkdir /etc/ansible [root@ansible ~]# cp ansible/examples/ansible.cfg /etc/ansible/ [root@ansible ~]# sed -i 's/#log_path/log_path/' /etc/ansible/ansible.cfg 7、验证Ansible安装结果 [root@ansiblet ansible]# ansible --version
-
四、Docker镜像
容器centos7安装部署ansible
centos镜像版本及ansible版本
centos:centos7.5.1804
ansible:2.9.11
启动容器并进入容器
docker run -itd centos:centos7.5.1804 /bin/bash
docker exec -it ae10a393f8df /bin/bash
# 注意以下操作均在容器内进行
安装sshpass、openssh-clients
yum -y install sshpass openssh-clients
# "msg": "to use the 'ssh' connection type with passwords, you must install the sshpass program"
# "msg": "Failed to create temporary directory.In some cases, you may have been able to authenticate and did not have permissions\
on the target directory. Consider changing the remote tmp path in ansible.cfg to a path rooted in \"/tmp\", for more error \
information use -vvv. Failed command was: ( umask 77 && mkdir -p \"` echo ~/.ansible/tmp `\"&& mkdir ~/.ansible/tmp/ \
ansible-tmp-1595925588.91-314-228861218215040 && echo ansible-tmp-1595925588.91-314-228861218215040=\"` \
echo ~/.ansible/tmp/ansible-tmp-1595925588.91-314-228861218215040 `\" ), exited with result 3"
安装pip
yum -y install epel-release
yum install python-pip -y
安装ansible
pip install ansible
安装winrm
pip install pywinrm
# 连接windows主机需要安装此模块
# 解决: "msg": "winrm or requests is not installed: No moudle named winrm"
创建目录及hosts文件
mkdir -p /etc/ansible/
touch /etc/ansible/hosts
vi /etc/ansible/hosts
# windows-hosts
[windows]
1.x.x.x
2.x.x.x
[windows:vars]
ansible_user = xxx
ansible_password = xxx
ansible_port = 598x
ansible_connection = winrm
ansible_winrm_server_cert_validation = ignore
# linux-hosts
host1 ansible_ssh_host=192.168.x.x ansible_ssh_user=root ansible_ssh_pass="Password"
host2 ansible_ssh_host=1.x.x.x ansible_ssh_user=appuser ansible_ssh_pass="Passw0rd"
创建配置文件
点击查看详细配置文件
修改配置文件
# 解决第一次连接主机需要询问
# "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not \
support this. Please add this host's fingerprint to your known_hosts file to manage this host."
# host_key_checking = False
修改为
host_key_checking = False
生成镜像
docker commit -m "py2_ansible" ae10a393f8df py2_ansible:0728
# ae10a393f8df:容器ID
验证
启动容器并进入容器
docker run -itd py2_ansible:0728 /bin/bash
docker exec -it 8549dabb93fb /bin/bash
执行ansible命令(修改/etc/ansible/hosts)
# hosts 内容见 windows-hosts
[root@8549dabb93fb ansible]# ansible windows -m win_ping
/usr/lib64/python2.7/site-packages/cryptography/__init__.py:39: CryptographyDeprecationWarning: Python 2 is no longer supported by
the Python core team. Support for it is now deprecated in cryptography, and will be removed in a future release.
CryptographyDeprecationWarning,
1.x.x.x | SUCCESS => {
"changed": false,
"ping": "pong"
}
2.x.x.x | SUCCESS => {
"changed": false,
"ping": "pong"
}
打印执行详细信息
ansible windows -m win_ping -vvv
原地址:https://www.cnblogs.com/fengzi-2016/p/13392343.html