Ansible

文章详细介绍了在不同操作系统(如macOS,CentOS,Ubuntu,Debian)上安装Ansible的步骤,包括增加系统文件句柄限制、处理SELinux问题、选择Python解释器以及使用pip安装。同时提到了源码安装和命令行补全的配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ansible安装

macOS 系统默认的文件句柄数比较小,如果你希望并发15个或者更多线程,你需要通过如下命令增加系统文件句柄打开上限。 sudo launchctl limit maxfiles unlimited。 不然,过多的线程数会提示报错: Too many open files 。

如果受管机开启了 SELinux,你需要安装 libselinux-python ,不然 copy/file/template 等任何相关联的功能都无法使用。 你可以使用 yum module 或 dnf module 在受管机安装软件。

默认情况下 Ansible 使用 /usr/bin/python 下的Python 解释器运行命令。 但部分 Linux 发行版可能只安装了 Python 3 解释器,可以从 /usr/bin/python3 找到。

你可以设置仓库文件对应亦是 [ansible_python_interpreter] 指明解释器位置,或者干脆安装一个 Python 2的解释器。 如果 Python 2 的解释器没有安装在指定目录 /usr/bin/python 。 那你依然需要修改仓库文件指定解释器的位置。 ansible_python_interpreter

安装Ansible

1. centos

$ sudo yum install ansible
RHEL, CentOS, and Fedora 的最新 RPM 版本获取方式: EPEL(https://docs.fedoraproject.org/en-US/epel/) as well as releases.ansible.com.

# 编译安装
$ git clone https://github.com/ansible/ansible.git
$ cd ./ansible
$ make rpm
$ sudo rpm -Uvh ./rpm-build/ansible-*.noarch.rpm

2. ubuntu

$ sudo apt update
$ sudo apt install software-properties-common
$ sudo apt-add-repository --yes --update ppa:ansible/ansible
$ sudo apt install ansible

# 旧的 Ubuntu 发行版 “software-properties-common” 名字是 “python-software-properties”. 使用 apt-get 而不是 apt 。 同时,只有比较新的发行版才有 (i.e. 18.04, 18.10, etc.) -u or --update 参数。 根据情况调整你的脚本。

# 编译安装
$ make deb

3. debian

# 增加如下行到 /etc/apt/sources.list:
deb http://ppa.launchpad.net/ansible/ansible/ubuntu trusty main

$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 93C4A3FD7BB9C367
$ sudo apt update
$ sudo apt install ansible

# 该方法已在Debian Jessie和Stretch中的Trusty来源中得到验证,但在早期版本中可能不受支持。 旧版本中使用 apt-get 而不是 apt .

4. Pip安装Ansible

Ansible 可以使用 Python 包管理器 pip 安装。 但 macOS 因为 openssl 协议要求的原因, pip*nix 的使用和其它系统会有一些区别, pip 以模块的方式运行。 ( 英文原文: It should be noted that macOS requires a slightly different use of pip than *nix due to openssl requirements, therefore pip must be run as a module. ) 如果 pip 事先没有安装,使用如下命令安装

$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
$ python get-pip.py --user

# 安装ansible
$ pip install --user ansible

# mac
$ python -m pip install --user git+https://github.com/ansible/ansible.git@devel

# 参考文档
https://cn-ansibledoc.readthedocs.io/zh_CN/latest/installation_guide/intro_installation.html

5. 源码运行Ansible(devel)

只有当你正在修改 Ansible 引擎或者尝试修改 Ansible 源码时,你才需要使用 devel 版本。 devel 分支会随时改变,功能也不稳定。

# 下载源码
$ git clone https://github.com/ansible/ansible.git
$ cd ./ansible

# 设置环境变量
# bash
$ source ./hacking/env-setup
# fish
$ source ./hacking/env-setup.fish
# suppress spurious warnings/errors
$ source ./hacking/env-setup -q

# pip工具
$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
$ python get-pip.py --user

# 安装依赖
$ pip install --user -r ./requirements.txt

# 一旦开始运行 env-setup 脚本,默认表示你使用的 inventory 仓库文件是 /etc/ansible/hosts . 当然你也可以参考这篇文件指定 inventory 仓库
$ echo "127.0.0.1" > ~/ansible_hosts
$ export ANSIBLE_INVENTORY=~/ansible_hosts

$ ansible all -m ping --ask-pass
# 或
$ sudo make install

6. ansible命令行补全

# Fedora
$ sudo dnf install python-argcomplete

# rhel and centos
$ sudo yum install epel-release
$ sudo yum install python-argcomplete

# apt
$ sudo apt install python-argcomplete

# pip
$ pip install argcomplete

Ansible社区

https://github.com/ansible/ansible

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值