设为首页 收藏本站
查看: 1496|回复: 0

[经验分享] 自动化运维工具ansible

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-8-10 09:11:45 | 显示全部楼层 |阅读模式
ansible简介
ansible是一款新出的自动化运维工具,基于Python开发,可实现对多台服务器进行批量配置、程序的部署及指令的运行。大大减少了在运维工程中的工作量。
常见的运维工具的工作模式有两种agent和agentless。ansible属于后者,即在被控制端没有代理运行。ansible基于ssh实现信息的传输,且在运行过程中具有幂等性(同一个操作执行多次,结果相同,不会重复执行)


ansible的安装
rpm包安装
在epel源中就有,添加epel源,直接使用yum下载即可。

1
[iyunv@www ~]# yum install ansible



源码安装
源码的安装包可去https://github.com上下载,各个版本的都有。
1
2
3
4
5
6
7
[iyunv@www ~]# yum -y install python-jinja2 PyYAML python-paramiko python-babel python-crypto
[iyunv@www ~]# tar xf ansible-1.9.2.tar.gz
[iyunv@www ~]# cd ansible-1.9.2
[iyunv@www ~]# python setup.py build
[iyunv@www ~]# python setup.py install
[iyunv@www ~]# mkdir /etc/ansible
[iyunv@www ~]# cp -r examples/* /etc/ansible/





ansible的简单应用
正真实现批量部署的是ansible中的模块,常用的模块有command,user,copy,cron,file,ping,yum,service,shell,script......
命令格式:ansible <host-pattern> [-m module] [-a args]
host-pattern     # 目标主机的地址,一般是配置文件中的组名
-m module       # 指定应用的模块

-a  args             # 指定模块的参数
ansible-doc -l                          # 查看所有可用的模块
ansible-doc moduleName      # 查看指定模块的帮助信息

定义Host Inventory
/etc/ansible/hosts该文件中定义了目标主机的地址,往往以组的形式定义。
1
2
3
4
5
[iyunv@www ~]# vim /etc/ansible/hosts
[myhosts]
192.168.1.118  ansible_ssh_user=root ansible_ssh_pass=passwd
192.168.1.119  ansible_ssh_user=root ansible_ssh_pass=passwd
192.168.1.120  ansible_ssh_user=root ansible_ssh_pass=passwd



在命令行中module指定为myhosts,即可同时操纵这3台主机。在主机地址后面指定主机的用户名和密码,则在命令行可直接进行连接。常见参数还有有:
ansible_ssh_host        # 指定连接的主机名
ansible_ssh_port        # 指定ssh连接的端口
ansible_sudo_pass     # sudo的密码
ansible_connection    # 指定连接方式(local,ssh,paramiko)
上面的认证方式不是很安全(密码直接写在了配置文件上),可基于密钥实现认证:
1
2
[iyunv@vm1 ansible]# ssh-keygen -t rsa -P ''
[iyunv@vm1 ansible]# ansible myhosts -m copy -a 'src=/root/.ssh/id_rsa.pub dest=/root/.ssh/authorized_keys owner=root group=root mode=600'





以下是几个简单的应用:
使用ping模块测试所有主机是否在线:
1
[iyunv@www ansible-1.9.2]# ansible myhosts -m ping



user模块,为每台主机添加用户:
1
2
3
4
[iyunv@CentOS-6 ~]# openssl passwd -1 -salt `openssl rand -hex 8`
Password:
$1$a787e6a4$ySdBzC2krv/EbZQBDJSYl1
[iyunv@CentOS-6 ~]# ansible myhosts -m user -a 'name=fedora password=$1$a787e6a4$ySdBzC2krv/EbZQBDJSYl1'



copy模块,复制文件至每一台主机:

1
[iyunv@CentOS-6 ~]# ansible myhosts -m copy -a 'src=/root/hellodb.sql dest=/tmp owner=fedora group=root mode=440'



file模块,创建文件和链接:
1
2
[iyunv@www ansible-1.9.2]# ansible myhosts -m file -a 'path=/tmp/test.txt state=touch'
[iyunv@www ansible-1.9.2]# ansible myhosts -m file -a 'src=/tmp/test.txt path=/tmp/ptTest state=link'



yum模块,使用yum在所有主机上安装、删除软件:
1
2
[iyunv@www ~]# ansible myhosts -m yum  -a 'name=zsh state=present'
[iyunv@www ~]# ansible myhosts -m yum  -a 'name=zsh state=absent'



service模块,在所有主机上启动、关闭相应的服务。
1
2
[iyunv@www ~]# ansible myhosts -m service -a 'name=httpd state=started enabled=yes'
[iyunv@www ~]# ansible myhosts -m service -a 'name=httpd state=stopped enabled=no'



setup模块,这个模块比较特殊,它可以用来获取目标主机上的一些关于系统环境的变量,例如:
ansible_os_family         # 目标主机的操作系统
ansible_pkg_mgr         # 目标主机的软件包管理器
ansible_machine          # 计算机架构
........
1
[iyunv@www ~]# ansible 192.168.1.108 -m setup





playbook简介

playbook使用YAML语法结构,可读性高。简单的说,playbook就是将需要对多台服务器的操纵过程以一定的格式写在一个配置文件中。然后在执行过程中,目标服务器就会按照实现定义好的机制一个接着一个完成任务。下面是一段playbook内容:
1
2
3
4
5
6
7
8
9
10
- name: web service
  remote_user: root
  hosts: web                           #以上是全局配置
  tasks:
  - name: install httpd
     yum: name=httpd state=present
  - name: configuration
     copy: src=httpd.conf dest=/etc/httpd/conf
  - name: start service
     service: name=httpd state=started enabled=no




hosts               # 用于指定要执行指定任务的主机,往往是/etc/ansible/hosts中定义的组
remote_user   # 用于指定远程主机上的执行任务的用户,也可用于各task中


task格式(每个横线“-”代表一个任务):
name:任务名称                          # (用于playbook的执行结果输出)
模块名称:  参数1=value  参数2=value
task list中的各任务按次序逐个在hosts中指定的所有主机上执行,即在所有主机上完成第一个任务后再开始第二个。在运行自下而下某playbook时,如果中途发生错误,不会回滚,更正playbook后重新执行一次即可,因为模块执行是幂等的,这意味着多次执行是安全的,结果均一致。
如果某个任务执行出现失败,就立即结束,后面的任务将不会被运行。这是可以用如下方式解决:
1
2
3
tasks:
- name: *****
shell: /usr/bin/somecommand || /bin/true



或者使用ignore_errors来忽略错误信息:
1
2
3
4
tasks:
- name: run this command and ignore the result
   shell: /usr/bin/somecommand
   ignore_errors: True                     #若上面的命令执行错误则把它忽略





roles简介
roles用于层次性、结构化地组织playbook,能够根据层次型结构自动装载变量文件、tasks以及handlers等。roles将变量,任务,文件等放置在不同的目录中,然后直接调用某个roles即可完成某些特定的任务。
roles的目录结构:
site.yml                      # 用来描述当前目录是用来做什么的
webservers.yml
roles/
     common/            #第一个角色
         files/                  # 存放由copy或script等模块调用的文件
         tasks/                # 定义了此角色的任务列表
         handlers/          # 用于定义此角色用到的各handler
         vars/                  #用于定义此角色用到的变量
         meta/                # 用于定义此角色的特殊设定及其依赖关系
     webservers/        #第二个角色
      ........
      ........


使用ansible批量部署LAMP

实验环境:使用ansible实现在192.168.1.118,192.168.1.119两台服务器上部署httpd,php服务,在192.168.1.120上部署mysql服务。


配置inventory文件(以实现密钥认证)
1
2
3
4
5
6
7
8
9
10
11
12
[iyunv@www ~]# vim /etc/ansible/hosts
[myhosts]
192.168.1.118
192.168.1.119
192.168.1.120

[web]
192.168.1.118
192.168.1.119

[mysql]
192.168.1.120



创建roles的相关目录(以下是我的roles目录结构):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[iyunv@www ansible]# ls
mysql.yml  remove.yml  roles  site.yml  web.yml
[iyunv@www ansible]# tree
.
├── mysql.yml
├── remove.yml
├── roles
│   ├── mysql
│   │   ├── files
│   │   │   └── my.cnf
│   │   ├── handlers
│   │   │   └── main.yml
│   │   ├── tasks
│   │   │   └── main.yml
│   │   ├── templates
│   │   └── vars
│   ├── php
│   │   ├── files
│   │   │   └── php.ini
│   │   ├── handlers
│   │   ├── tasks
│   │   │   └── main.yml
│   │   ├── templates
│   │   └── vars
│   └── web
│       ├── files
│       │   └── httpd.conf
│       ├── handlers
│       │   └── main.yml
│       ├── tasks
│       │   └── main.yml
│       ├── templates
│       └── vars
├── site.yml
└── web.yml



编辑web与php的playbook,php是以模块的形式添加至httpd中,所以两个服务在同一台服务器上:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[iyunv@www ansible]# cat web.yml
- name: web service
  remote_user: root
  hosts: web
  roles:
  - web
  - php
#########web##########
[iyunv@www ansible]# cat roles/web/tasks/main.yml
- name: install httpd
  yum: name=httpd state=present
  tags: install
- name: configuration
  copy: src=httpd.conf dest=/etc/httpd/conf  #src指定的文件会自动去上一层的files目录中寻找
  tags: conf
  notify:                             #当配置文件发生更改时会触发下面的动作
  - reload httpd                             #会去handlers目录下寻找任务文件,然后执行
- name: start service
  service: name=httpd state=started enabled=no
[iyunv@www ansible]# cat roles/web/handlers/main.yml
- name: reload httpd
  service: name=httpd state=reloaded
#########php##########
[iyunv@www ansible]# cat roles/php/tasks/main.yml
- name: install php
  yum: name=php state=present
  tags: install
- name: configuration
  copy: src=php.ini dest=/etc/php.ini
  tags: conf



编辑mysql的playbook:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[iyunv@www ansible]# cat mysql.yml
- name: install mysql
  remote_user: root
  hosts: mysql
  roles:
  - mysql
#######################
[iyunv@www ansible]# cat roles/mysql/tasks/main.yml
- name: install mysql
  yum: name=mysql-server state=present
  tags: install
- name: configuration
  copy: src=my.cnf dest=/etc/my.cnf
  tags: conf
  notify:
  - restart mysqld
- name: start service
  service: name=mysqld state=started enabled=no
[iyunv@www ansible]# cat roles/mysql/handlers/main.yml
- name: restart mysqld
  service: name=mysqld state=restarted



三者的配置文件均存放在各自的file目录下。
开始批量安装,配置,启动httpd,php服务:
wKiom1XGHtyiVXTHAARkSpxop00912.jpg
mysql的安装,配置,启动

wKioL1XGIYexqATtAANLF0vgs3k315.jpg
检查服务器端的宽口监听状况:

wKioL1XGJLiAf5vcAAEGIATM-5s797.jpg 服务已安装完成,并启动。.................^_^




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-96771-1-1.html 上篇帖子: 自动化运维工具Ansible之Playbooks的when语句 下篇帖子: 自动化运维工具Ansible之Python API
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表