# ansible的安装与使用

ansible挺好用的,网上有很丰富的教程.

结合最近的使用情况,我也总结下.

ansible的特点:

1.基于ssh运行

2.无需客户端

# 安装ansible

这里提供四种安装方式,根据自己的需要任选一种即可

# 1.1使用yum安装

yum install epel-release -y

yum install ansible -y

# 1.2 使用pip安装

pip install ansible

如果没pip,需先安装好pip.方法如下:

yum install python-setuptools

easy_install pip

# 1.3 源码安装pip

准备工具

yum install git python-setuptools gcc python-devel -y

获取源码

git clone https://github.com/ansible/ansible

安装ansible

cd ansible

python setup.py install

# 1.4 brew安装

在macos下,可使用brew安装

brew install ansible

# 配置服务器

ansible通过文件来定义你要管理的主机,也就是说把你需要的管理的主机ip写到一个文件中即可。

这个文件一般名为hosts,它可以放在多个路径下,也可以自定义名称和路径。

默认我们用/etc/ansible/hosts这个文件即可

如果是macos 10 以上的系统,默认是没有权限写/etc/的,可以通过自定义配置来添加hosts文件

在~下添加.ansible.cfg文件,指定hosts文件的路径,内容如下

[defaults]
hostfile = /usr/local/etc/ansible/hosts

hosts文件的格式为:

[webservers]
10.2.2.121
10.2.2.122

这里webservers是主机组的名称,10.2.2.121和10.2.2.122就是具体的服务器ip了

按照这个格式来自定义自己要管理的主机和主机组即可

如我想定义一组名称test的主机,ip分别是10.2.1.201,10.2.1.202

格式如下

[test]
10.2.1.201
10.2.1.202

默认ssh端口是22,如果主机端口号是其他的,在ip后加:端口号即可,如10.2.1.203的端口是2211,属于test组,格式如下:

[test]
10.2.1.201
10.2.1.202
10.2.1.203:2211

如果要定义登录机器的用户名和密码,比如10.2.1.204的用户名是test,密码是admin@test,端口是2244,

[test]
10.2.1.201
10.2.1.202
10.2.1.203:2211
10.2.1.204:2244 ansible_ssh_user=test ansible_ssh_pass=admin@test

还可以定义主机的别名,如

[test]
web-01 ansible_host=10.2.1.205 ansible_ssh_user=test ansible_ssh_pass=admin@test ansible_ssh_port=2233

建议使用免密码登录来管理服务器,在ansible的服务器上配置一套ssh的key,通过ssh-copy-id把公钥分发到要管理的服务器上。具体步骤如下:

# 1.使用ssh-keygen产生ssh密钥

[root@test-201 ~]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
dd:20:23:7c:1a:2e:01:bf:b1:67:7a:08:87:5f:e6:7e root@test-201
The key's randomart image is:
+--[ RSA 2048]----+
|  .              |
|   o .           |
|    + + + .      |
|   . * = + o     |
|  o = B S . .    |
|   + X           |
|    + o          |
|     o  E        |
|      ..         |
+-----------------+

# 2.将公钥发送到要管理的服务器

使用ssh-copy-id命令

比如要发送到10.2.31.202,使用如下命令:

[root@test-201 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub 10.2.31.202
root@10.2.31.202's password: 
Now try logging into the machine, with "ssh '10.2.31.202'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

# 使用ansible

命令格式如下:

ansible + 主机组名称 + -m + 模块名称 + -a + 参数

主机组名称,即hosts中定义的主机组名称

-m 指使用模块,后加指定的模块名称

-a 指传给模块的参数

在不指定模块时,默认调用command模块。

如我们想看下test组上的服务器的/tmp下面有哪些文件,可以使用如下命令

ansible test -a "ls /tmp"

[root@test-201 ~]# ansible test -a "ls /tmp"
10.2.31.203 | SUCCESS | rc=0 >>
ansible_EMEGZI
testabcdefg

我们可以使用copy模块,将本地文件发送到目标服务器上,如:

ansible test -m copy -a "src=/root/install.log dest=/tmp"

这个命令是将本地的/root/install.log发送到test组的/tmp下,执行的效果如下:

[root@test-201 ~]# ansible test -m copy -a "src=/root/install.log dest=/tmp"
10.2.31.203 | SUCCESS => {
    "changed": true, 
    "checksum": "114ee153946d9cd2e690c405e5796a4fcc400542", 
    "dest": "/tmp/install.log", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "17b18780156a31a65d62a324110d686e", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:admin_home_t:s0", 
    "size": 43688, 
    "src": "/root/.ansible/tmp/ansible-tmp-1466157410.68-191787255687953/source", 
    "state": "file", 
    "uid": 0
}

你可以使用ansible-doc --list查看当前的所有模块

[root@test-201 ~]# ansible-doc  --list                       
….
….                                                        
authorized_key                     Adds or removes an SSH authorized key                                                                                           
azure                              create or terminate a virtual machine in azure                                                                                  
azure_rm_deployment                Create or destroy Azure Resource Manager template deployments                                                                   
azure_rm_networkinterface          Manage Azure network interfaces.                                                                                                
azure_rm_networkinterface_facts    Get network interface facts.                                                                                                    
azure_rm_publicipaddress           Manage Azure Public IP Addresses.                                                                                               
azure_rm_publicipaddress_facts     Get public IP facts.                                                                                                            
azure_rm_resourcegroup             Manage Azure resource groups.                                                                                                   
azure_rm_resourcegroup_facts       Get resource group facts.                                                                                                       
azure_rm_securitygroup             Manage Azure network security groups.
….
…

使用ansible-doc + 模块名,可以看具体某个模块的使用方法,如查看yum模块的使用方法

ansible-doc yum

[root@test-201 ~]# ansible-doc  yum
> YUM

  Installs, upgrade, removes, and lists packages and groups with the `yum' package manager.
  ......
  

ansible自带了很多丰富的模块,详细请看:

http://docs.ansible.com/ansible/list_of_all_modules.html

# Playbooks

顾名思义,playbooks就像剧本一样,将你要做的事情先定义好,然后通过它来执行。这也是ansible一个强大的地方,可以通过它来做些复杂的应用部署。

举个例子:

[root@test-201 ~]# cat test-playbook
- hosts: test
  tasks:
  - name: 确认apache是否在运行
service: name=httpd state=started

这是个很简单的playbooks,首先它指定了要操作的主机组是test,定义了一个名称:确认apache是否在运行,执行pkg=httpd state=latest动作。

执行如下命令

# ansible-playbook test-playbook

效果如下:

[root@test-201 ~]# ansible-playbook test-playbook 

PLAY [test] ********************************************************************

TASK [setup] *******************************************************************
ok: [10.2.31.203]

TASK [确认apache是否在运行] ***********************************************************
ok: [10.2.31.203]

PLAY RECAP *********************************************************************
10.2.31.203                : ok=2    changed=0    unreachable=0    failed=0

由于playbooks涉及的内容较多,这里就不一一赘述了,更多内容请参考文档: http://ansible-tran.readthedocs.io/en/latest/docs/playbooks.html

# 小技巧:

有时候如果想直接操作某台服务器,但又没有在hosts里定义这台服务器时,可以使用如下命令:

ansible all -i '服务器ip,'

注意服务器ip后面要加个,

ansible all -i '10.2.31.201,' -u test -k -a 'uptime'