自动化运维工具ansible的实践

10,145 阅读3分钟

前言

当服务器越来越多,统一管理起来显得尤为重要。那么选择一款管理工具必不可少,既要能满足管理新部署的机器,还要兼容之前部署的机器,无客户端版的ansible管理工具可能成为优先考虑的工具之一。

认识ansible

ansible 是一款自动化运维工具,能够解决我们在it工作中,一遍又一遍执行相同任务。利用它,我们可以只解决一次问题,然后自动化运行我们的解决方案。 目前,数以千计的公司正在使用简单但功能强大的it自动化引擎,我相信它可以帮我们加速完成DevOps计划。 对了,它是由Red Hat公司出品的。

特性

  1. 简单、强大、无代理
  2. 无客户端、推送式
  3. 任务按顺序执行
  4. 应用程式部署、 配置管理
  5. 工作流编排
  6. 协调应用程序生命周期
  7. 使用OpenSSH和WinRM、无代理架构
  8. 没有代理商利用或更新

框架结构

安装

推荐pip安装

[sl@localhost ~]# easy_install pip
[sl@localhost ~]# pip install ansible

其他安装方式

从项目的checkout中可以很容易运行Ansible,Ansible的运行不要求root权限,也不依赖于其他软件,不要求运行后台进程,也不需要设置数据库.

从源码安装的步骤

$ git clone git://github.com/ansible/ansible.git --recursive
$ cd ./ansible
//使用 Bash:

$ source ./hacking/env-setup
//使用 Fish:

$ . ./hacking/env-setup.fish

yum源安装

$ sudo yum install ansible

apt安装

$ sudo apt-get install software-properties-common
$ sudo apt-add-repository ppa:ansible/ansible
$ sudo apt-get update
$ sudo apt-get install ansible

管理机器

编辑机器列表

修改这个列表/etc/ansible/hosts

[test]
10.10.100.205:22 ansible_ssh_user=sl ansible_ssh_pass=1
10.10.100.230:22 ansible_ssh_user=sl ansible_ssh_pass=1

这里我使用了用户名密码连接,当然官方并不建议这种连接方式,下文介绍证书连接。 如果提示找不到sshpass,需安装。

yum install sshpass

你还可以通过组的形式添加,如:

[test]
10.10.100.[1-10]
server[1-10]
[a-z].company.com

以上,各自代表一组机器。

其他参数

ansible_ssh_host
    将要连接的远程主机名.与你想要设定的主机的别名不同的话,可通过此变量设置.
ansible_ssh_port
    ssh端口号.如果不是默认的端口号,通过此变量设置.
ansible_ssh_user
    默认的 ssh 用户名
ansible_ssh_pass
    ssh 密码(这种方式并不安全,我们强烈建议使用 --ask-pass 或 SSH 密钥)
ansible_sudo_pass
    sudo 密码(这种方式并不安全,我们强烈建议使用 --ask-sudo-pass)
ansible_sudo_exe (new in version 1.8)
    sudo 命令路径(适用于1.8及以上版本)
ansible_connection
    与主机的连接类型.比如:local, ssh 或者 paramiko. Ansible 1.2 以前默认使用 paramiko.1.2 以后默认使用 'smart','smart' 方式会根据是否支持 ControlPersist, 来判断'ssh' 方式是否可行.
ansible_ssh_private_key_file
    ssh 使用的私钥文件.适用于有多个密钥,而你不想使用 SSH 代理的情况.
ansible_shell_type
    目标系统的shell类型.默认情况下,命令的执行使用 'sh' 语法,可设置为 'csh' 或 'fish'.
ansible_python_interpreter
    目标主机的 python 路径.适用于的情况: 系统中有多个 Python, 或者命令路径不是"/usr/bin/python",比如  \*BSD, 或者 /usr/bin/python

基于证书认证添加机器

在控制主机中生成ssh密钥对

ssh-keygen -t rsa

一路回车下去,然后执行

ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.10.100.205
ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.10.100.206
ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.10.100.207

/etc/ansible/hosts 中增加机器ip及端口号即可,最后测试一下。

简单使用

列出文件列表

ansible test -a "ls "

测试机器是否通

[sl@localhost ~]# ansible test -m ping
10.10.100.205 | SUCCESS => {
    "changed": false, 
    "failed": false, 
    "ping": "pong"
}
10.10.100.230 | SUCCESS => {
    "changed": false, 
    "failed": false, 
    "ping": "pong"
}

其他命令

# 执行远程命令
# ansible test -m command -a 'uptime'

# 执行主控端脚本
# ansible test -m script -a '/etc/ansible/script/test.sh'

# 执行远程主机的脚本
# ansible test -m shell -a 'ps aux|grep zabbix'

# 类似shell
# ansible test -m raw -a "ps aux|grep zabbix|awk '{print \$2}'"

# 创建软链接
# ansible test -m file -a "src=/etc/resolv.conf dest=/tmp/resolv.conf state=link"

# 删除软链接
# ansible test -m file -a "path=/tmp/resolv.conf state=absent"

# 复制文件到远程服务器
# ansible test -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=root group=root mode=0644"

使用playbook

Playbook是一种与adhoc任务执行模式完全不同的方式,而且特别强大。 简单地说,Playbooks是一个非常简单的配置管理和多机器部署系统的基础,不同于任何已经存在的配置系统,而且非常适合部署复杂应用程序。

yml语法

yml是一种比较精简的语法结构,通过空格来控制层级,具体可移步百度。

检测 apache是否是最新版

---
- hosts: test
  vars:
    http_port: 80
    max_clients: 200
  remote_user: root
  tasks:
  - name: ensure apache is at the latest version
    yum: name=httpd state=latest
  - name: write the apache config file
    template: src=/srv/httpd.j2 dest=/etc/httpd.conf
    notify:
    - restart apache
  - name: ensure apache is running (and enable it at boot)
    service: name=httpd state=started enabled=yes
  handlers:
    - name: restart apache
      service: name=httpd state=restarted

保存为test.yml,运行

ansible-playbook test.yml

因为所有机器上都没有安装,所有报failed。

yml基本结构

基本结构: - host: test remote_user: tasks: - task1 module_name: module_args - task 2 handlers: - handler1 - handler2 hosts行是一个或多个组或主机模式的列表,以冒号分隔, 每个任务定义可远程用户:

-  hosts : webservers 
  remote_user : root 
  tasks :
    -  name : test connection 
      ping :
      remote_user : yourname

任务分为service、command、shell等:

tasks:
  - name: make sure apache is running
    service: name=httpd state=started
    
tasks:
  - name: enable selinux
    command: /sbin/setenforce 1
    
tasks:
  - name: run this command and ignore the result
    shell: /usr/bin/somecommand || /bin/true
    
tasks:
  - name: run this command and ignore the result
    shell: /usr/bin/somecommand
    ignore_errors: True
    
tasks:
    - name: restart everything
      command: echo "this task will restart the web services"
      notify: "restart web services"

使用playbook安装nginx

写安装nginx的sh脚本vim nginx-install.yml

- hosts: test
  vars:
    http_port: 80
    max_clients: 200
  remote_user: root
  tasks:
  - name: copy nginx-install.sh to client
    copy: src=nginx-install.sh dest=/tmp/nginx-install.sh
  - name: chomd a+x
    shell: chmod +x /tmp/nginx-install.sh
  - name: install nginx
    shell: /tmp/nginx-install.sh

执行ansible-playbook

[shaolei@localhost ~]# ansible-playbook nginx-install.yml 

到各机器上观察

[shaolei@localhost html]# ps -ef |grep nginx
shaolei      2477 31369  0 13:09 pts/0    00:00:00 grep --color=auto nginx

总结

总体来说,ansible功能十分强大,适合大部分公司大部分场景,本文介绍了ansible基本用法,和playbook脚本的一些简单实践。当然如果你熟悉python,你还可以使用python写出各种各样适合自己公司的运维命令来。