# ansible 安装

ansible 是由python语言编写的一个自动化运维管理工具。它有以下的特点：

* 不需要安装客户端，通过sshd去通信
* 基于模块工作，模块可以由任何语言开发
* 不仅支持命令行使用模块，也支持编写yaml格式的playbook，易于编写和阅读
* 安装十分简单，centos上可直接yum安装
* 有提供UI（浏览器图形化）[www.ansible.com/tower，收费的](http://www.ansible.com/tower，收费的)\
  官方文档 <http://docs.ansible.com/ansible/latest/index.html>
* ansible已经被redhat公司收购，它在github上是一个非常受欢迎的开源软件，github地址<https://github.com/ansible/ansible>

**安装**

准备两台机器

* 192.168.127.128 ：服务端
* 192.168.127.129 ：客户端

只需要在服务段上安装ansible

```bash
yum list |grep ansible # 可以看到自带源里就有2.4版本的ansible


yum install -y ansible # 安装ansible
```

服务端上生成密钥对，上传到客户端

```bash
ssh-keygen -t rsa 
 # 一直回车，生成密钥对

ssh-copy-id root@192.168.127.129  # 将公钥上传至客户端
```

修改配置文件

```bash
# /etc/ansible/hosts
[test]          # 主机分组
192.168.127.129 # 已经做过密钥认证的主机地址，或主机名

# ansible_ssh_host 连接目标主机的地址

# ansible_ssh_port 连接目标主机的端口，默认 22 时无需指定

# ansible_ssh_user 连接目标主机默认用户

# ansible_ssh_pass 连接目标主机默认用户密码

# ansible_ssh_connection 目标主机连接类型，可以是 local 、ssh 或 paramiko

# ansible_ssh_private_key_file 连接目标主机的 ssh 私钥

# ansible_*_interpreter 指定采用非 Python 的其他脚本语言，如 Ruby 、Perl 或其他类似 ansible_python_interpreter 解释器
```

测试

```bash
[root@localhost ~]# ansible test -m ping
192.168.127.129 | SUCCESS => { 
    "changed": false, 
    "ping": "pong"
}

# -i          指定 hosts 文件位置
# -u username 指定 SSH 连接的用户名
# -k          指定远程用户密码
# -f          指定并发数
# -s          如需要 root 权限执行时使用 ( 连接用户不是 root 时 )
# -K          -s 时，-K 输入 root 密码
```
