不知道大家有没有这样的困扰,公司在上升期一下子采集了N多台服务器需要你初始化配置。并且老板说过两天就要进集群中投入使用,如果光靠一台一台的操作我估计你就别睡觉了…不过你会首先想到安装服务器可以使用cobbler,不知道cobbler看这里。虽然使用cobbler可以自动给服务器通过网络引导方式安装操作系统,但是上面的应用部署怎么办?配置文件如何给?下面给大家一个好的解决方案!
ansible基本介绍:
现在给大家隆重推出一个自动化运维工具:ansible 那么什么是ansible?ansible是在2012年出现的一款自动化运维工具,基于python语言实现,由Paramiko、PyYAML(标记语言)和Jinja2(模块语言)三个关键模块组成。
ansible的特性:
ansible属于无agent工具依赖于ssh服务;ansible里面有许多的小模块通过模块来管理特定任务。由于属于无angent端所以ansible部署简单;支持主从模式;支持自定义模块;支持各种编程语言;支持playbook;有幂等性允许重复执行N次。
什么是agent?有无agent的程序有什么区别?
agnet其实就是一个工具的客户端程序,安装在被管理节点,agent的作用就是用来与主节点(master)通讯得知下一步的命令计划或者上传当前状态。没有agent的工具通常使用ssh连接进行通讯和管理。
有agent工具的程序虽然部署较为麻烦,因为需要给每个被控制的节点安装上agent但是比较安全,即使master节点被攻破但是其他agent节点的密码不可知并且只能对其进行少量范围的操作。有agent的自动运维工具有:puppet、func等。
无agent的工具部署十分简单,只需要将公钥传送给各个被控制的主机就能进行控制,但是坏处在于master主机被攻破之后所有主机的权限也被攻破。有agent的自动运维工具有:ansible、fabric等。
ansible的架构:
上图我又无耻的借鉴了原有的介绍图片…不过下面和大家说说关于图中的架构意思:
- ansible core:ansible模块
- host inventory:主机列表
- core modules:核心模块
- custom modules:第三方自己的模块
- playbook:模板剧本(使用YAML JINJIA2格式填写)
- connect plugin:插件
好了这样基本就把ansible的基本情况给大家介绍完毕,不过如何安装和使用呢?别急!下面和大家讲讲。
如何安装ansible?
安装ansible其实非常简单,当然你是自虐党可以便宜安装…不过ansible现在已经被redhat收购,所以下载还要留下邮件…下载源码比价困难。既然被redhat收购了那么yum安装肯定最合适的。不过在安装之前得先安装epel的源才能安装ansible!
[root@localhost ~] yum install epel-release #首先安装epel源 [root@localhost ~] yum install ansible #再安装ansible #安装ansible的依赖包非常多,很多我在上面已经讲过! Installing: ansible noarch 2.2.0.0-4.el7 epel 4.6 M Installing for dependencies: PyYAML x86_64 3.10-11.el7 base 153 k libtomcrypt x86_64 1.17-23.el7 epel 224 k libtommath x86_64 0.42.0-4.el7 epel 35 k libyaml x86_64 0.1.4-11.el7_0 base 55 k python-babel noarch 0.9.6-8.el7 base 1.4 M python-backports x86_64 1.0-8.el7 base 5.8 k python-backports-ssl_match_hostname noarch 3.4.0.2-4.el7 base 12 k python-httplib2 noarch 0.7.7-3.el7 epel 70 k python-jinja2 noarch 2.7.2-2.el7 base 515 k python-keyczar noarch 0.71c-2.el7 epel 218 k python-markupsafe x86_64 0.11-10.el7 base 25 k python-setuptools noarch 0.9.8-4.el7 base 396 k python-six noarch 1.9.0-2.el7 base 29 k python2-crypto x86_64 2.6.1-10.el7 epel 475 k python2-ecdsa noarch 0.13-4.el7 epel 83 k python2-paramiko noarch 1.16.1-1.el7 epel 257 k python2-pyasn1 noarch 0.1.9-7.el7 base 100 k sshpass x86_64 1.05-5.el7 epel 21 k
安装完毕以后我们还得在配置文件中添加主机才能使用!不过在此之前我首先得说说实验拓扑:
主节点ansible为172.16.1.10,其他被操控的节点为11、12、13.那么接下来看看添加方案。首先打开/etc/ansible/hosts这个文件并不是hosts域名解析的文件,实则为定义被操控主机的文件!
给ansible定义主机清单:
[root@localhost ~] vi /etc/ansible/hosts # This is the default ansible 'hosts' file. # # It should live in /etc/ansible/hosts # # - Comments begin with the '#' character # - Blank lines are ignored # - Groups of hosts are delimited by [header] elements # - You can enter hostnames or ip addresses # - A hostname/ip can be a member of multiple groups [webserver] #这边写上定义的组名 172.16.1.11 #这里写好后端的主机的IP地址,如果你有域名解析的话可以使用主机名形式填写! 172.16.1.12 [testserver] #定义的第二个组 172.16.1.13
好了这样就算将基本的几个主机定义完毕!不过在开始讲解ansible之前还得做一些准备工作。
使用ansible之前的准备工作:
还记得ansible是无agent类的自动化运维工具不?还有还记得没有angent是如何工作的?要是忘了赶快返回上面复习一下。首先ssh是需要通过账号和密码才能执行的,所以为了能够让ansible更好的没有障碍的控制被控制节点建议使用密钥的方式来控制后端主机。还记得我以前讲过的openssh的博文吗?现在可派到大用处了,不记得了?看这里!
创建密钥对的过程我就不详细说明了,随后别忘了将公钥放到被控制主机的.ssh节点。然后这样操作:
[root@localhost ~] scp ansible.ssh.pub root@172.16.1.11:/root root@172.16.1.11's password: ansible.ssh.pub 100% 408 0.4KB/s 00:00 #其他的后端主机都按照这样将公钥传送过去 [root@localhost ~] mkdir .ssh [root@localhost ~] mv ansible.ssh.pub .ssh [root@localhost ~] vi /etc/ssh/sshd_config AuthorizedKeysFile .ssh/ansible.ssh.pub #找到AuthorizedKeysFile这句话将去除注释改为我上面的样子! [root@localhost ~] service sshd restart #重启ssh Stopping sshd: [ OK ] Starting sshd: [ OK ] #接下来的12、13两台主机我就不详细说明了!
ansible命令基本语法:
ansible <host-patten> [-f Focks] [-m Moudle_Name] [-a ARGS]
host-patten:主机名,可以是主机组、主机名、IP地址、all
-f #:填写执行操作的并发数量默认是5个
-i PATH:主机清单在什么地方,默认不用定义在/etc/ansible/hosts
-m NAME:指明模块的名称,就是使用的模块
-a ARGS:指明模块的具体参数 语法:KEY=VALUE
-k:基于请求密码认证
-s:使用SUDO运行命令
在ansible最重要的就是使用模块了,模块才是ansible的核心使用。如果需要了解有哪些模块可是使用命令:ansible-doc -l列出所有模块:
[root@localhost ~] ansible-doc -l a10_server Manage A10 Networks AX/SoftAX/Thunder/vThunder devices a10_service_group Manage A10 Networks devices' service groups a10_virtual_server Manage A10 Networks devices' virtual servers acl Sets and retrieves file ACL information. add_host add a host (and alternatively a group) to the ansible-playbook in-memo... #以上只是显示了一点点的模块,更多的模块可以自己查看。 #查看模块就像使用man文档一样可以翻页,退出按q即可。
这里的博客我就简单说一些常用的模块!
ansible的常用模块以及使用示例:
①command:
此模块为远程运行命令模块,但是不能使用高级用法比如使用变量、参数、管道、重定向的等。
[root@localhost ~] ansible-doc -s command #查看模块的简单帮助 - name: Executes a command on a remote node action: command chdir # cd into this directory before running the command creates # a filename or (since 2.0) glob pattern, when it already exists, this step will *not* be run. executable # change the shell used to execute the command. Should be an absolute path to the executable. free_form= # the command module takes a free form command to run. There is no parameter actually named 'free form'. See the examples! removes # a filename or (since 2.0) glob pattern, when it does not exist, this step will *not* be run. warn # if command warnings are on in ansible.cfg, do not warn about this particular line if set to no/false. #如果需要查看完整单个模块帮助直接使用ansible-doc command无需加-s。 #示例:我们使用command模块查看testserver组的fstab文件。 [root@localhost ~] ansible testserver -m command -a 'cat /etc/fstab' 172.16.1.13 | SUCCESS | rc=0 >> #这边会显示运用主机|执行状态等信息 # # /etc/fstab # Created by anaconda on Fri Sep 2 13:30:59 2016 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/VolGroup-lv_root / ext4 defaults 1 1 UUID=0707c387-1e75-4f9a-b6cb-68b11aec6c51 /boot ext4 defaults 1 2 /dev/mapper/VolGroup-lv_swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0
②user:
此模块为添加、删除用户模块。
[root@localhost ~] ansible testserver -m user -a 'name=test1 state=present system=no group=test1 uid=1005' 172.16.1.13 | SUCCESS => { "changed": true, "comment": "", "createhome": true, "group": 1005, "home": "/home/test1", "name": "test1", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1005 } #这样创建用户就OK了
③group:
此模块为添加、删除组模块。
[root@localhost ~] ansible testserver -m group -a 'name=test1 state=present system=no gid=1005' 172.16.1.13 | SUCCESS => { "changed": true, "gid": 1005, "name": "test1", "state": "present", "system": false } #这样用户组就创建成功了,推荐先创建组
④ping:
探测选用的主机是否存在 | 在线!
[root@localhost ~]# ansible all -m ping 172.16.1.13 | SUCCESS => { "changed": false, "ping": "pong" } 172.16.1.12 | SUCCESS => { "changed": false, "ping": "pong" } 172.16.1.11 | SUCCESS => { "changed": false, "ping": "pong" } #这个模块不需要模块参数,成功显示pong。
⑤setup:
从远程主机收集硬件信息及运行状态
[root@localhost ~]# ansible testserver -m setup #这个模块也不需要参数 172.16.1.13 | SUCCESS => { "ansible_facts": { "ansible_all_ipv4_addresses": [ "172.16.1.13" ], "ansible_all_ipv6_addresses": [ "fe80::a00:27ff:fe57:84a0" ], "ansible_architecture": "i386", "ansible_bios_date": "12/01/2006", "ansible_bios_version": "VirtualBox", "ansible_cmdline": { "KEYBOARDTYPE": "pc", "KEYTABLE": "us", "LANG": "en_US.UTF-8", "SYSFONT": "latarcyrheb-sun16", "quiet": true, "rd_LVM_LV": "VolGroup/lv_root", "rd_NO_DM": true, "rd_NO_LUKS": true, "rd_NO_MD": true, "rhgb": true, "ro": true, "root": "/dev/mapper/VolGroup-lv_root" }, #后面省略很多,主要查看主机当前状态,里面内容很多从查看系统版本到cpu甚至是ssh的公钥都有。
⑥shell:
复杂命令使用模块,支持变量、重定向、管道等复杂命令。
[root@localhost ~]# ansible testserver -m shell -a 'cat /etc/fstab > /root/test && echo "success"' 172.16.1.13 | SUCCESS | rc=0 >> success [root@localhost ~]# ssh 172.16.1.13 Last login: Mon Dec 26 11:18:58 2016 from 172.16.1.10 [root@localhost ~]# cat test # # /etc/fstab # Created by anaconda on Fri Sep 2 13:30:59 2016 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/VolGroup-lv_root / ext4 defaults 1 1 UUID=0707c387-1e75-4f9a-b6cb-68b11aec6c51 /boot ext4 defaults 1 2 /dev/mapper/VolGroup-lv_swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 #将fstab文件重定向到/root/test文件中如果成功显示success。
常用的模块先说到这些,接下来做一个小实验。顺便将剩下的常用模块一起说了!
使用ansible安装LAMP并且架构wordpress服务:
这里我使用刚刚没用过的主机组,就是webserver,首先第一步就是安装LAMP的组合:
[root@localhost ~] ansible webserver -m yum -a 'name=httpd state=present' 172.16.1.12 | SUCCESS => { "changed": true, "msg": "", "rc": 0, "results": [......#此处省略主要显示安装的包和依赖包 172.16.1.11 | SUCCESS => { "changed": true, "msg": "", "rc": 0, "results": [ #上面的是安装httpd接下来安装mysql和php [root@localhost ~] ansible webserver -m yum -a 'name=mysql-server state=present' [root@localhost ~] ansible webserver -m yum -a 'name=php state=present' [root@localhost ~]# ansible webserver -m yum -a 'name=php-mysql state=present'
好了安装上面的步骤就算把LAMP组合给搞定了,接下来第二步开始配置httpd的配置文件:
[root@localhost ~] scp root@172.16.1.11:/etc/httpd/conf/httpd.conf /root httpd.conf 100% 34KB 33.6KB/s 00:00 [root@localhost ~]# vim httpd.conf #添加一个虚拟主机,随后别忘了将DocumentRoot注释并且在DirectoryIndex添加index.php <VirtualHost *:80> DocumentRoot /var/www/wordpress ServerName 172.16.1.11 ErrorLog /var/log/httpd/wordpress.log CustomLog /var/log/httpd/error.log combined <Directory /var/www/wordpress> Options None AllowOverride All </Directory> </VirtualHost> [root@localhost ~] ansible 172.16.1.11 -m copy -a 'dest=/etc/httpd/conf/httpd.conf src=/root/httpd.conf' #将文件拷贝到172.16.1.11主机上去。 172.16.1.11 | SUCCESS => { "changed": true, "checksum": "XXXXXX", "dest": "/etc/httpd/conf/httpd.conf", "gid": 0, "group": "root", "md5sum": "961c6b4bcdc1b27494d8dc094379378c", "mode": "0644", "owner": "root", "size": 34402, "src": "/root/", "state": "file", "uid": 0 } #随后再次修改文件将虚拟主机中的servername改为172.16.1.12然后再次拷贝到12主机上。 [root@localhost ~]# ansible 172.16.1.12 -m copy -a 'dest=/etc/httpd/conf/httpd.conf src=/root/httpd.conf'
现在配置文件已经搞定,接下来第三步开始把wordpress放到两台webserver之中了:
#首先下载wordpress然后解压缩wordpress随后打开配置文件: [root@localhost ~] vim wordpress/wp-config.php #更具要求填写,我将数据库写为wordpress、用户为root、密码空、主机localhost #然后将压缩文件传送过去: [root@localhost ~] ansible webserver -m copy -a 'dest=/root/ src=/root/wordpress-4.5.3-zh_CN.tar.gz' #然后解压到各自的/var/www目录 [root@localhost ~] ansible webserver -m command -a 'tar -xf /root/wordpress-4.5.3-zh_CN.tar.gz -C /var/www' #将改好的wp-config.php放到wordpress目录中 [root@localhost ~] ansible webserver -m copy -a 'src=/root/wordpress/wp-config.php dest=/var/www/wordpress/'
这样wordpress就算搞定了,接下来第四步启动所有服务,并且创建wrodpress数据库:
#率先启动mysql服务: [root@localhost ~] ansible webserver -m service -a 'name=mysqld state=started' #创建wordpress数据库 [root@localhost ~] ansible webserver -m shell -a 'mysql -e "CREATE DATABASE wordpress;"' #查看数据库: [root@localhost ~] ansible webserver -m shell -a 'mysql -e "SHOW DATABASES;"' 172.16.1.11 | SUCCESS | rc=0 >> Database information_schema mysql test wordpress 172.16.1.12 | SUCCESS | rc=0 >> Database information_schema mysql test wordpress #随后检查httpd服务配置文件: [root@localhost ~] ansible webserver -m command -a 'httpd -t' 172.16.1.12 | SUCCESS | rc=0 >> httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName Syntax OK 172.16.1.11 | SUCCESS | rc=0 >> httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName Syntax OK #最后启动httpd服务: [root@localhost ~] ansible webserver -m service -a 'name=httpd state=started' 172.16.1.12 | SUCCESS => { "changed": true, "name": "httpd", "state": "started" } 172.16.1.11 | SUCCESS => { "changed": true, "name": "httpd", "state": "started" }
检查两台主机的wordpress是否已经OK:
首先看第一台主机:172.16.1.11
然后看看第二台主机:172.16.1.12
好了ansible的基本内容就算介绍完毕了,但是有没有想过每次这样输入命令行多么麻烦?这样也就算比原来省力一点点,有没有像脚本一样能够一次编写自动执行这样的功能呢?答案是必须有的,这就是ansible的最优秀的地方就是他的playbook。关于playbook我放到下一次再说!
Comments