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