# 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源代码安装

![](https://2035699859-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MF9mjq1C_TjY5Viiqz4%2Fsync%2F5cf43a48c11e2970ab33b3976a707e7d47ada20e.png?generation=1618470571842730\&alt=media)

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)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://moluo.gitbook.io/notes/bian-cheng-xue-xi/bu-shu-yun-wei-xu-ni-hua/ansible/3.2.ansible-an-zhuang.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
