本篇文章使用 Anaconda3 创建python虚拟环境,基于python3安装测试ansible。
环境说明
本次环境搭建使用VMware虚拟机,以下是具体环境:
- CentOS 7
- 内存:1G
- 处理器:1
- 硬盘:20G
- 网络连接:NAT模式 保证虚机可连通互联网
安装Anaconda3
- 下载并安装anaconda
# 下载
wget https://repo.anaconda.com/archive/Anaconda3-2020.07-Linux-x86_64.sh
# 安装
sh Anaconda3-2020.07-Linux-x86_64.sh
- 配置环境变量
vi /etc/profile
# 添加配置:
PATH=$PATH:/opt/anaconda3/bin
export PATH
# 使环境变量配置生效
source /etc/profile
# 查看
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/opt/anaconda3/bin
优化下载(非必须步骤):可以自行查找修改Anaconda源
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/pkgs/main
conda config --set show_channel_urls yes
或写入:~/.condarc
查看命令:conda info
创建python3虚拟环境
使用Anaconda创建虚拟环境
conda create -n py37-ansible python=3.7
优化下载速度修改pip源
# 在 ~/.pip中修改源,如没有该文件,则新建
[root@localhost ~]# cd ~/.pip
-bash: cd: /root/.pip: No such file or directory
[root@localhost ~]# mkdir ~/.pip
[root@localhost ~]# cd ~/.pip
[root@localhost .pip]# touch pip.conf
[root@localhost .pip]# vim ~/.pip/pip.conf
# 写入如下配置
[global]
timeout = 6000
index-url = https://mirrors.aliyun.com/pypi/simple/
trusted-host = mirrors.aliyun.com
基于python3安装ansible
# 进入新建的虚拟环境
source activate py37-ansible
# 使用pip3安装ansible
pip3 install ansible
ansible环境基础配置
# 查找ansible文件
[root@localhost ~]# find / -name "ansible"
/root/anaconda3/envs/py37-ansible/bin/ansible
/root/anaconda3/envs/py37-ansible/lib/python3.7/site-packages/ansible
/root/anaconda3/envs/py37-ansible/lib/python3.7/site-packages/ansible_test/_data/injector/ansible
/root/anaconda3/envs/py37-ansible/lib/python3.7/site-packages/ansible_collections/ansible
/root/anaconda3/envs/py37-ansible/lib/python3.7/site-packages/ansible_collections/netbox/netbox/docs/js/ansible
# 创建软链接
ln -s /root/anaconda3/envs/py37-ansible/bin/ansible /usr/bin/ansible
查看ansible版本
(py37-ansible) [root@localhost ~]# ansible --version
ansible 2.10.3
config file = None
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /root/anaconda3/envs/py37-ansible/lib/python3.7/site-packages/ansible
executable location = /root/anaconda3/envs/py37-ansible/bin/ansible
python version = 3.7.9 (default, Aug 31 2020, 12:42:55) [GCC 7.3.0]
需要注意:pip安装ansible无config file,需手动创建
参考配置:https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg
创建hosts文件:
参考配置:https://raw.githubusercontent.com/ansible/ansible/devel/examples/hosts
以上两个文件默认位置:/etc/ansible
至此安装完成:
(py37-ansible) [root@localhost ~]# ansible --version
ansible 2.10.3
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /root/anaconda3/envs/py37-ansible/lib/python3.7/site-packages/ansible
executable location = /root/anaconda3/envs/py37-ansible/bin/ansible
python version = 3.7.9 (default, Aug 31 2020, 12:42:55) [GCC 7.3.0]