> For the complete documentation index, see [llms.txt](https://moluo.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://moluo.gitbook.io/notes/bian-cheng-xue-xi/bu-shu-yun-wei-xu-ni-hua/ansible/3.2.ansible-an-zhuang.md).

# 3.2.Ansible安装

在开始安装之前，为了避免firewalld和selinux的干扰，我们将其关闭。该操作仅限于实验环境，请勿在生产环境操作

* 关闭firewalld防火墙

  ```bash
  $ systemctl stop firewalld
  $ systemctl disable firewalld
  ```
* 关闭selinux

  ```bash
  # 编辑配置，将SELINUX设置为disable
  $ vi /etc/sysconfig/selinux
  SELINUX=disabled
  # 重启虚机，使配置生效
  $ reboot
  # 重启成功后，执行如下命令，若返回Disabled则说明成功关闭SELINUX
  $ getenforce
  Disabled
  ```

## Ansible的常见安装模式(CentOS7)

### Yum包管理安装

```bash
# yum install epel-release
# yum repolist
# yum -y install ansible
```

### python3+pip3+virtualenv安装

1. 安装python3

   ```bash
   $ yum install -y python3
   $ which python3
   /bin/python3
   ```
2. 安装virtualenv

   ```bash
   $ pip3 install virtualenv -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
   ```
3. 创建python3虚拟空间并激活

   ```bash
   $ python3 -m virtualenv ansible  # Create a virtualenv if one does not already exist
   $ source ansible/bin/activate   # Activate the virtual environment
   ```
4. 安装ansible

   ```bash
   $ python -m pip install ansible -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
   ```
5. 验证ansible版本

   ```bash
   $ ansible --version
   ansible 2.10.5
     config file = /etc/ansible/ansible.cfg
     configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
     ansible python module location = /opt/ansible/lib/python3.6/site-packages/ansible
     executable location = /opt/ansible/bin/ansible
     python version = 3.6.8 (default, Nov 16 2020, 16:55:22) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
   ```

### Python3.6+Ansible2.5源代码安装

![](/files/-MYJMF8OmvTg6wZO9zCH)

1. 预先安装Python3.6版本
2. 安装virtualenv

   ```bash
   # pip install virtualenv
   ```
3. 创建Ansible账户并安装python3.6版本virtualenv实例

   ```bash
   # useradd deploy && su - deploy
   # virtualenv -p /usr/local/bin/python3 .py3-a2.5-env
   ```
4. Git源代码安装ansible2.5

   ```bash
   # cd /home/deploy/.py3-a2.5-env
   # git clone https://github.coy/ansible/ansible.git
   # cd ansible && git checkout stable-2.5
   ```
5. 加载python3.6 virtualenv环境

   ```bash
   # source /home/deploy/.py3-a2.5-env/bin/activate
   ```
6. 安装ansible依赖包

   ```bash
   # pip install paramiko PyYAML jinja2
   ```
7. 在python3.6虚拟环境下加载ansible2.5

   ```bash
   # source /home/deployl.py3-a2.5-env/ansible/hacking/env-setup -q
   ```
8. 验证ansible2.5

   ```bash
   # ansible --version
   ```

## 更多安装方式

更多安装方式请见：[Installation Guide](https://docs.ansible.com/ansible/latest/installation_guide/index.html)
