Ansible安装

1.配置源

阿里云镜像站地址:镜像源

bash 复制代码
yum install epel-release -y 
[root@myhost ~]# cat /etc/yum.repos.d/epel.repo
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
# It is much more secure to use the metalink, but if you wish to use a local mirror
# place its address here.
#baseurl=http://download.example/pub/epel/7/$basearch
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch&infra=$infra&content=$contentdir
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
# It is much more secure to use the metalink, but if you wish to use a local mirror
# place its address here.
#baseurl=http://download.example/pub/epel/7/$basearch/debug
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-7&arch=$basearch&infra=$infra&content=$contentdir
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1

[epel-source]
name=Extra Packages for Enterprise Linux 7 - $basearch - Source
# It is much more secure to use the metalink, but if you wish to use a local mirror
# place it's address here.
#baseurl=http://download.example/pub/epel/7/source/tree/
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-source-7&arch=$basearch&infra=$infra&content=$contentdir
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1

Centos-Base.repo源

bash 复制代码
[root@myhost ~]# cat /etc/yum.repos.d/CentOS-Base.repo 
[base]
name=CentOS-$releasever
enabled=1
failovermethod=priority
baseurl=https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/
gpgcheck=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

[updates]
name=CentOS-$releasever
enabled=1
failovermethod=priority
baseurl=https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/
gpgcheck=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

[extras]
name=CentOS-$releasever
enabled=1
failovermethod=priority
baseurl=https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/
gpgcheck=0

2.安装ansible

不同版本的ansible需要不同版本的python环境!

bash 复制代码
yum install ansible -y 


1./etc/ansible

这是 Ansible 的主配置目录,包含 Ansible 的主要配置文件和默认的 Inventory 文件。

2./etc/ansible/ansible.cfg

这是 Ansible 的主配置文件,用于定义 Ansible 的行为和默认设置。它包含多个部分,每个部分都有特定的配置选项。以下是一些常见的配置选项:

defaults

inventory:指定 Inventory 文件的路径,默认是 /etc/ansible/hosts。

remote_user:指定远程主机的默认用户。

host_key_checking:是否进行 SSH 主机密钥检查。

log_path:指定日志文件的路径。

privilege_escalation

become:是否启用特权提升(如 sudo)。

become_method:特权提升的方法(如 sudo 或 su)。

ssh_connection

ssh_args:SSH 连接的参数。

control_path:SSH 控制路径。

示例:

bash 复制代码
[defaults]
inventory = /etc/ansible/hosts
remote_user = ansible
host_key_checking = False
log_path = /var/log/ansible.log

[privilege_escalation]
become = True
become_method = sudo

3./etc/ansible/hosts

这是 Ansible 的默认 Inventory 文件,用于定义要管理的主机和组。Inventory 文件可以是简单的主机列表,也可以是复杂的组结构。

示例:

bash 复制代码
# /etc/ansible/hosts
[webservers]
host1.example.com
host2.example.com

[dbservers]
host3.example.com
host4.example.com

4./etc/ansible/roles

这是 Ansible 的默认角色目录,用于存储角色。角色是 Ansible 的基本单位,用于组织和重用 Playbook。角色通常包含任务、变量、文件、模板和子角色等。

示例目录结构:

bash 复制代码
/etc/ansible/roles/
├── my_role/
│   ├── tasks/
│   │   └── main.yml
│   ├── vars/
│   │   └── main.yml
│   ├── files/
│   ├── templates/
│   ├── meta/
│   │   └── main.yml
│   └── defaults/
│       └── main.yml

示例:

bash 复制代码
# /etc/ansible/roles/my_role/tasks/main.yml
---
- name: Install Apache
  yum:
    name: httpd
    state: present

5.其他常见文件和目录

/etc/ansible/plugins:存放 Ansible 插件的目录,如连接插件、回调插件等。

/etc/ansible/facts.d:存放主机事实(facts)的目录,用于定义主机的自定义变量。

/etc/ansible/vars:存放变量文件的目录,用于定义全局变量。

/etc/ansible/inventory:存放 Inventory 文件的目录,可以包含多个 Inventory 文件。

/etc/ansible/vault:存放加密文件的目录,用于存储敏感信息。

相关推荐
2601_9605679638 分钟前
电商套图自动化效率的工程量化分析——从逐张生成到批量套图的架构演进
运维·架构·自动化
吳所畏惧2 小时前
宝塔面板Redis密码修改指南:SSH命令修改 vs 面板UI界面修改,哪个更靠谱?
运维·服务器·数据库·redis·缓存·ssh
DFT计算杂谈2 小时前
无 Root 权限在 Tesla K80 零门槛部署 DeepSeek 大模型
linux·服务器·网络·数据库·机器学习
HiDev_2 小时前
【非标自动化】2、认识元器件(确定目标)
运维·自动化
Zhang~Ling3 小时前
从 fopen 到 struct file:从零开始拆解 Linux 文件 I/O
linux·运维·服务器
一次旅行3 小时前
Python+大模型端到端自动化日报系统
开发语言·python·自动化
DeeplyMind3 小时前
Linux 深入 per-VMA lock:Linux 缺页路径如何摆脱 mmap_lock
linux·per-vma lock
爱写代码的森3 小时前
蒙三方库 | harmony-utils之FileUtil文件重命名与属性查询详解
linux·运维·服务器·华为·harmonyos·鸿蒙·huawei
中微极客3 小时前
2026主流AI Agent框架技术选型与性能对比
运维·网络·人工智能
久曲健的测试窝4 小时前
智能座舱自动化测试落地方案:数字仿真搭配实车实测,解决用例运维与场景失真难题
运维