ansible的三种安装方法

方法一、通过本地yum源安装

centos 使用默认的yum源安装 ——> ansible-core

rhel   yum 源安装   ——>  ansible

这里实验用的是2.9的版本

ansible的安装文件 Index of /ansiblehttps://releases.ansible.com/ansible/ ansible依赖与python它是通过python进行开发的

[root@rhce ~]# lsblk                 //查看是否连接成功
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0          11:0    1  9.4G  0 rom  /media
sr1          11:1    1 22.6M  0 rom  /run/media/root/ytlab-ansible2.9_for-RHEL8.
nvme0n1     259:0    0  100G  0 disk 
├─nvme0n1p1 259:1    0  512M  0 part /boot
├─nvme0n1p2 259:2    0    6G  0 part [SWAP]
└─nvme0n1p3 259:3    0 93.5G  0 part /

[root@rhce ~]# mkdir /ansible_repo                //建立挂载点 

[root@rhce ~]# mount /dev/sr1 /ansible_repo/

[root@rhce ~]# vim /etc/yum.repos.d/rhel.repo 

[ansible]
name=ansible
baseurl=file:///ansible_repo/ansible
enabled=1
gpgcheck=0
[root@rhce ~]# yum clean all

[root@rhce ~]# yum makecache 

[root@rhce ~]# yum -y install ansible                //安装

[root@rhce ~]# ansible --version                //查看安装的版本信息
ansible 2.9.11
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Mar 18 2021, 08:58:41) [GCC 8.4.1 20200928 (Red Hat 8.4.1-1)]

方法二、使用源码包编译安装

提取ansible.cfg的方法

通过此方法和方法三安装后都没有配置文件。

1、如果需要可以去github中搜索ansible.cfg找到后下载到/etc/ansible下使用就行了(手动安装目录都没有需要自己建)。

2、安装ansible包进入 安装目录中的ansible目录中找到ansible-2.9.11-1.el8ae.noarch.rpm

使用该命令提取rpm包中的文件:rpm2cpio ansible-2.9.11-1.el8ae.noarch.rpm | cpio -id

 最后提取得到的就是下图中的ansible.cfg文件。

 注:此处是将需要提取的rpm包拷贝到tmp中操作的,此处的etc目录也是提取文件后的目录并不是根下的etc目录。

 

ansible社区:  https://github.com/ansible/ansible

下载地址:https://github.com/ansible/ansible/tree/v2.9.0

[root@test~]#wget https://ghproxy.com/https://github.com/ansible/ansible/archive/refs/tags/v2.9.0.zip 

[root@test ~]# ls
anaconda-ks.cfg  Documents  initial-setup-ks.cfg  Pictures  Templates   Videos
Desktop          Downloads  Music                 Public    v2.9.0.zip

[root@test ~]# du -sh v2.9.0.zip                 //查看下载的大小
27M    v2.9.0.zip
[root@test ~]# unzip v2.9.0.zip                 //解压

[root@test ~]# cd ansible-2.9.0/                //进入解压后的目录

[root@test ansible-2.9.0]# python3 setup.py build       //构建包,不能直接进入setup.py修改
执行后会生成build的目录进入

[root@test ansible-2.9.0]# cd build/
[root@test build]# ls
lib  scripts-3.6
[root@test build]# cd scripts-3.6/
[root@test scripts-3.6]# ls               //这些都是ansible的命令文件要把这些编译到系统中
ansible             ansible-console  ansible-inventory  ansible-test
ansible-config      ansible-doc      ansible-playbook   ansible-vault
ansible-connection  ansible-galaxy   ansible-pull

这些文件就是一个一个的模块包

回到前两级目录解决这些文件的依赖关系

[root@test ansible-2.9.0]# python3 setup.py install               //安装时会报一些错缺少相关包需要安装缺少的包后再进行安装

[root@test ansible-2.9.0]# yum install rust python3-jinja2 python3-cryptography  -y

//根据安装时所报错的信息安装这几个包然后再重新安装(红帽8是这三个包每个版本报错不一样)

[root@test ansible-2.9.0]# python3 setup.py install 
最后出现安装版本信息则成功

Finished processing dependencies for ansible==2.9.0

[root@test ~]# ansible --version                //查看安装的ansible信息
ansible 2.9.0
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.6/site-packages/ansible-2.9.0-py3.6.egg/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.6.8 (default, Mar 18 2021, 08:58:41) [GCC 8.4.1 20200928 (Red Hat 8.4.1-1)]

方法三、使用python的pip包管理器进行安装

官方pypi的仓库:https://pypi.org/project/ansible

[root@test ~]# yum install python3-cryptography  -y            //安装依赖包(配置本地yum源)

[root@test ~]# pip3 install ansible==2.9.0 -i Simple Index               //使用python的pypi的源安装ansible,==指定版本,如果不指定则是源中的最新版,后面的链接是指定的清华大学加速镜像站(红帽8需要指定pip版本)

### Ansible 安装教程 #### 一、安装Ansible 对于Redhat/Centos Linux而言,由于Ansible位于epel源内,因此需先安装epel源再进行Ansible安装[^1]。 ```bash yum -y install epel-release yum -y install ansible ``` 上述命令会自动完成Ansible及其依赖包的下载与安装过程[^3]。 #### 二、配置管理节点和远程主机间的连接 为了实现自动化运维任务,在成功安装之后,还需确保管理节点能够无障碍地访问目标机器。这通常涉及到SSH密钥对的创建以及分发公钥至各台被控服务器上,从而建立无需密码验证即可登录的状态。 #### 三、自定义Ansible行为 通过调整`ansible.cfg`文件内的参数选项可以改变Ansible的工作模式。此配置文件可能存在于多个位置,按照优先级顺序依次为: - 环境变量指定路径下的ANSIBLE_CONFIG; - 当前项目根目录下名为`./ansible.cfg`的本地化设定; - 用户家目录里的隐藏文件`.ansible.cfg`; - `/etc/ansible/ansible.cfg`作为全局默认配置项存在; 当以上任意一处未找到有效配置时,则采用内置的标准设置运行程序[^4]。 ### 基本用法示例 假设已经完成了必要的前期准备工作,下面给出几个简单的例子来展示Ansible的强大功能。 测试连通性: ```bash ansible "webservers&dbservers" -m ping # 或者使用正则表达式匹配组名 ansible "~(web|db)servers" -m ping ``` 这两条指令分别向属于`webservers`或`dbservers`群组的目标发送ping请求以确认其在线状态[^2]。 执行shell命令: ```bash ansible all -a "/bin/echo hello" ``` 这条语句将在所有已知主机上打印字符串“hello”。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值