0%

ansible

认识ansible

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

特性

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

安装

pip安装

(官方推荐,我没配置pip3就没测试)

1
2
[root@alitiger ~]# easy_install pip
[root@alitiger ~]# pip install ansible

源码安装

(测试了,虽然后期配置步骤比较繁琐,但是可以方便的更新到git最新的版本,所以我选择yum安装)

1
2
3
4
5
6
7
8
9
[root@alitiger ~]# git clone git://github.com/ansible/ansible.git --recursive
[root@alitiger ~]# cd ./ansible
//使用 Bash:

[root@alitiger ~]# source ./hacking/env-setup
//使用 Fish:

[root@alitiger ~]# . ./hacking/env-setup.fish

yum源安装

1
[root@alitiger ~]# yum install ansible

apt安装

1
2
3
4
5
$ sudo apt-get install software-properties-common
$ sudo apt-add-repository ppa:ansible/ansible
$ sudo apt-get update
$ sudo apt-get install ansible

管理机器(inventory)

编辑机器列表

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

1
2
3
[test]
10.10.100.205:22 ansible_ssh_user=tiger ansible_ssh_pass=1
10.10.100.230:22 ansible_ssh_user=tiger ansible_ssh_pass=1

这里使用了用户名密码连接,官方并不建议这种连接方式,我个人平时用的也比价少,目标机器的密码暴露了,下文介绍证书连接。
如果提示找不到sshpass,需安装。

1
yum install sshpass

你还可以通过组的形式添加,(如果是域名的话,需要本地做解析,或者是可用的域名)如:

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

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

其他参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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' 、'bash'或 'fish'.
ansible_python_interpreter
目标主机的 python 路径.适用于的情况: 系统中有多个 Python, 或者命令路径不是"/usr/bin/python",比如 \*BSD, 或者 /usr/bin/python

基于证书认证添加机器

(官方推荐的,安全性比较高)
在控制主机中生成ssh密钥对
ssh-keygen -t rsa
一路回车下去,然后执行

1
2
3
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及端口号即可,最后测试一下。

###简单使用(ad-hoc)
列出文件
ansible test -a "ls"
测试机器是否通

1
2
3
4
[root@alitiger ~]# ansible test -m ping -o
10.10.100.205 | SUCCESS => {"changed": false, "failed": false, "ping": "pong"}
10.10.100.230 | SUCCESS => {"changed": false, "failed": false,"ping": "pong"}

其他命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 执行远程命令
[root@alitiger ~]# ansible test -m command -a 'uptime'

# 执行主控端脚本
[root@alitiger ~]# ansible test -m script -a '/etc/ansible/script/test.sh'

# 执行远程主机的脚本
[root@alitiger ~]# ansible test -m shell -a 'ps aux|grep zabbix'

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

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

# 删除软链接
[root@alitiger ~]# ansible test -m file -a "path=/tmp/resolv.conf state=absent"

# 复制文件到远程服务器
[root@alitiger ~]# 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是一种比较精简的语法结构,通过空格来控制层级,具体可移步百度。

1
2
3
4
5
6
7
8
包含三种类型:

键值对:key: value
数组
纯量:整型、字符串、布尔型
这个很好理解:如果你熟悉Python, 这三种类型就相当于:map, list, 变量

如果你熟悉golang, 这三种类型就相当于: map, 数组, 变量

检测 apache是否是最新版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
---
- 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=install
- name: write the apache config file
template: src=/srv/httpd.j2 dest=/etc/httpd/conf/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

检查语法
方法一:
ansible-playbook test.yml --syntax-check
方法二(个人比较喜欢第二种,但是第一种不用单独安包):

1
2
先pip install ansible-lint
ansible-lint test.yml

保存为test.yml,运行

ansible-playbook test.yml

yml基本结构

基本结构:

1
2
3
4
5
6
7
8
9
- host: test
remote_user:
tasks:
- task1
module_name: module_args
- task 2
handlers:
- handler1
- handler2

hosts行是一个或多个组或主机模式的列表,以冒号分隔,
每个任务定义可远程用户:

1
2
3
4
5
6
-  hosts : youngfit
remote_user : root
tasks :
- name : test connection
ping :
remote_user : yourname

任务分为service、command、shell等:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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

1
2
3
4
5
6
7
8
9
10
11
12
- hosts: youngfit
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

1
[root@alitiger playbook]# ansible-playbook  nginx-install.yml

到各机器上观察

1
2
[root@alitiger playbook]# ps -ef |grep nginx
tiger 2477 31369 0 13:09 pts/0 00:00:00 grep --color=auto nginx

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