==========================================================================================
二、Ansible基础安装与配置
==========================================================================================
1、Ansible基础安装
(1)、python2.7安装
https://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgz
# tar xvzf Python-2.7.8.tgz
# cd Python-2.7.8
# ./configure --prefix=/usr/local
# make --jobs=`grep processor/proc/cpuinfo | wc -l`
# make install
## 将python头文件拷贝到标准目录,以避免编译ansible时,找不到所需的头文件
# cd /usr/local/include/python2.7
# cp -a ./* /usr/local/include/
## 备份旧版本的python,并符号链接新版本的python
# cd /usr/bin
# mv python python2.6
# ln -s /usr/local/bin/python
## 修改yum脚本,使其指向旧版本的python,已避免其无法运行
# vim /usr/bin/yum
#!/usr/bin/python --> #!/usr/bin/python2.6
(2)、setuptools模块安装
https://pypi.python.org/packages/source/s/setuptools/setuptools-7.0.tar.gz
# tar xvzf setuptools-7.0.tar.gz
# cd setuptools-7.0
# python setup.py install
安装可能会出现如下报错: "Compression requires the (missing) zlib module" 出错原因: 提示的很清楚,缺少 zlib模块导致安装失败 # yum install zlib # yum install zlib-devel 下载成功后,进入python2.7的目录,重新执行 #make #make install 此时先前执行的 软连接仍旧生效 然后进入 setuptool目录, # python setup.py install 重新安装 参考链接:http://chengjianxiaoxue.iteye.com/blog/2095012 |
(3)、pycrypto模块安装
https://pypi.python.org/packages/source/p/pycrypto/pycrypto-2.6.1.tar.gz
# tar xvzf pycrypto-2.6.1.tar.gz
# cd pycrypto-2.6.1
# python setup.py install
(4)、PyYAML模块安装
http://pyyaml.org/download/libyaml/yaml-0.1.5.tar.gz
# tar xvzf yaml-0.1.5.tar.gz
# cd yaml-0.1.5
# ./configure --prefix=/usr/local
# make --jobs=`grep processor/proc/cpuinfo | wc -l`
# make install
https://pypi.python.org/packages/source/P/PyYAML/PyYAML-3.11.tar.gz
# tar xvzf PyYAML-3.11.tar.gz
# cd PyYAML-3.11
# python setup.py install
(5)、Jinja2模块安装
https://pypi.python.org/packages/source/M/MarkupSafe/MarkupSafe-0.9.3.tar.gz
# tar xvzf MarkupSafe-0.9.3.tar.gz
# cd MarkupSafe-0.9.3
# python setup.py install
https://pypi.python.org/packages/source/J/Jinja2/Jinja2-2.7.3.tar.gz
# tar xvzf Jinja2-2.7.3.tar.gz
# cd Jinja2-2.7.3
# python setup.py install
(6)、paramiko模块安装
https://pypi.python.org/packages/source/e/ecdsa/ecdsa-0.11.tar.gz
# tar xvzf ecdsa-0.11.tar.gz
# cd ecdsa-0.11
# python setup.py install
https://pypi.python.org/packages/source/p/paramiko/paramiko-1.15.1.tar.gz
# tar xvzf paramiko-1.15.1.tar.gz
# cd paramiko-1.15.1
# python setup.py install
(7)、simplejson模块安装
https://pypi.python.org/packages/source/s/simplejson/simplejson-3.6.5.tar.gz
# tar xvzf simplejson-3.6.5.tar.gz
# cd simplejson-3.6.5
# python setup.py install
(8)、ansible安装
https://github.com/ansible/ansible/archive/v1.7.2.tar.gz
# tar xvzf ansible-1.7.2.tar.gz
# cd ansible-1.7.2
# python setup.py install
2、Ansible配置
(1)、SSH免密钥登录设置
## 生成公钥/私钥
# ssh-keygen -t rsa -P ''
[root@node01 ansible-1.7.2]# ssh-keygen -t rsa -P '' Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. 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: ed:c6:c5:bf:bf:56:39:53:5c:54:99:b4:a3:e0:9b:9e [email protected] The key's randomart p_w_picpath is: +--[ RSA 2048]----+ | .o*| | oo| | . +.| | .... . +| | S ..o. o| | o .o. +.| | +o . +| | .. . o | | E ooo| +-----------------+ |
## 写入信任文件(将/root/.ssh/id_rsa.pub分发到其他服务器,并在所有服务器上执行如下指令):
# cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
# chmod 600 /root/.ssh/authorized_keys
或通过如下命令设置信任 ssh-copy-id [email protected] ssh-copy-id [email protected] ssh-copy-id [email protected] |
## 主机组定义
# vim /etc/ansible/hosts
[node_test]
192.168.99.135
192.168.99.136
192.168.99.137
(2)、ansible配置
# mkdir -p /etc/ansible
# cp /data/ansible-1.7.2/examples/ansible.cfg /etc/ansible/
# mv ansible.cfg ansible.cfg.bak
# cat ansible.cfg.bak |grep -v '^#'|grep -v '^$' > ansible.cfg
# vim /etc/ansible/ansible.cfg
……
remote_port = 36000
private_key_file = /root/.ssh/id_rsa
……
(3)、简单测试
# ansible node_test -m command -a 'uptime'
192.168.99.135 | success | rc=0 >>
00:46:23 up 1:40, 2 users, load average: 0.00, 0.01, 0.03
192.168.99.137 | success | rc=0 >>
00:43:43 up 1:44, 2 users, load average: 0.00, 0.01, 0.00
192.168.99.136 | success | rc=0 >>
00:46:24 up 1:40, 2 users, load average: 0.00, 0.00, 0.00
3、常用模块使用
(1)、setup
## 用来查看远程主机的一些基本信息
# ansible node_test -m setup
# ansible node_test -m setup 192.168.99.135 | success >> { "ansible_facts": { "ansible_all_ipv4_addresses": [ "192.168.99.135" ], "ansible_all_ipv6_addresses": [ "fe80::a00:27ff:febb:8c4" ], "ansible_architecture": "x86_64", "ansible_bios_date": "12/01/2006", "ansible_bios_version": "VirtualBox", "ansible_cmdline": { "KEYBOARDTYPE": "pc", "KEYTABLE": "us", "LANG": "en_US.UTF-8", "SYSFONT": "latarcyrheb-sun16", "nomodeset": true, "quiet": true, "rd_LVM_LV": "vg_node01/lv_swap", "rd_NO_DM": true, "rd_NO_LUKS": true, "rd_NO_MD": true, "rhgb": true, "ro": true, "root": "/dev/mapper/vg_node01-lv_root" }, ..............以下代码省略........................ |
(2)、ping
## 用来测试远程主机的运行状态
# ansible node_test -m ping
# ansible node_test -m ping 192.168.99.137 | success >> { "changed": false, "ping": "pong" } 192.168.99.136 | success >> { "changed": false, "ping": "pong" } 192.168.99.135 | success >> { "changed": false, "ping": "pong" } |
(3)、file
## 设置文件的属性
相关选项如下:
force:需要在两种情况下强制创建软链接,一种是源文件不存在,但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no
group:定义文件/目录的属组
mode:定义文件/目录的权限
owner:定义文件/目录的属主
path:必选项,定义文件/目录的路径
recurse:递归设置文件的属性,只对目录有效
src:被链接的源文件路径,只应用于state=link的情况
dest:被链接到的路径,只应用于state=link的情况
state:
directory:如果目录不存在,就创建目录
file:即使文件不存在,也不会被创建
link:创建软链接
hard:创建硬链接
touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
absent:删除目录、文件或者取消链接文件
示例:
## 远程文件信息查看
# ansible node_test -m command -a "ls -al /etc/resolv.conf" 192.168.99.137 | success | rc=0 >> -rw-r--r--. 2 root root 24 Dec 13 21:46 /etc/resolv.conf 192.168.99.136 | success | rc=0 >> -rw-r--r--. 2 root root 24 Dec 13 22:53 /etc/resolv.conf 192.168.99.135 | success | rc=0 >> -rw-r--r--. 2 root root 24 Dec 13 21:46 /etc/resolv.conf |
(4)、copy
## 复制文件到远程主机
相关选项如下:
backup:在覆盖之前,将源文件备份,备份文件包含时间信息。有两个选项:yes|no
content:用于替代“src”,可以直接设定指定文件的值
dest:必选项。要将源文件复制到的远程主机的绝对路径,如果源文件是一个目录,那么该路径也必须是个目录
directory_mode:递归设定目录的权限,默认为系统默认权限
force:如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖,如果为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes
others:所有的file模块里的选项都可以在这里使用
src:被复制到远程主机的本地文件,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用“/”来结尾,则只复制目录里的内容,如果没有使用“/”来结尾,则包含目录在内的整个内容全部复制,类似于rsync。
示例:
## 将本地文件“/etc/ansible/ansible.cfg”复制到远程服务器
# ansible node_test -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=root group=root mode=0644"
# ansible node_test -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=root group=root mode=0644" 192.168.99.136 | success >> { "changed": true, "dest": "/tmp/ansible.cfg", "gid": 0, "group": "root", "md5sum": "911794bbcff5972ab85453b3e908ae2c", "mode": "0644", "owner": "root", "size": 985, "src": "/root/.ansible/tmp/ansible-tmp-1481648746.59-118495159801866/source", "state": "file", "uid": 0 } 192.168.99.137 | success >> { "changed": true, "dest": "/tmp/ansible.cfg", "gid": 0, "group": "root", "md5sum": "911794bbcff5972ab85453b3e908ae2c", "mode": "0644", "owner": "root", "size": 985, "src": "/root/.ansible/tmp/ansible-tmp-1481648746.66-138434971921270/source", "state": "file", "uid": 0 } 192.168.99.135 | success >> { "changed": true, "dest": "/tmp/ansible.cfg", "gid": 0, "group": "root", "md5sum": "911794bbcff5972ab85453b3e908ae2c", "mode": "0644", "owner": "root", "size": 985, "src": "/root/.ansible/tmp/ansible-tmp-1481648746.62-80555080662498/source", "state": "file", "uid": 0 } 验证: # ansible node_test -m command -a "ls -al /tmp/ansible.cfg" 192.168.99.136 | success | rc=0 >> -rw-r--r-- 1 root root 985 Dec 14 01:05 /tmp/ansible.cfg 192.168.99.137 | success | rc=0 >> -rw-r--r-- 1 root root 985 Dec 14 01:03 /tmp/ansible.cfg 192.168.99.135 | success | rc=0 >> -rw-r--r-- 1 root root 985 Dec 14 01:05 /tmp/ansible.cfg |
(5)、command
## 在远程主机上执行命令
相关选项如下:
creates:一个文件名,当该文件存在,则该命令不执行
free_form:要执行的linux指令
chdir:在执行指令之前,先切换到该目录
removes:一个文件名,当该文件不存在,则该选项不执行
executable:切换shell来执行指令,该执行路径必须是一个绝对路径
示例:
# ansible node_test -m command -a "uptime"
# ansible node_test -m command -a "uptime" 192.168.99.136 | success | rc=0 >> 01:09:24 up 2:03, 2 users, load average: 0.00, 0.00, 0.00 192.168.99.137 | success | rc=0 >> 01:06:43 up 2:07, 2 users, load average: 0.00, 0.00, 0.00 192.168.99.135 | success | rc=0 >> 01:09:23 up 2:03, 2 users, load average: 0.32, 0.07, 0.02 |
(6)、shell
## 切换到某个shell执行指定的指令,参数与command相同。
与command不同的是,此模块可以支持命令管道,同时还有另一个模块也具备此功能:raw
示例:
## 先在本地创建一个SHELL脚本
# vim /tmp/test.sh
#!/bin/sh
date +%F_%H:%M:%S
#chmod +x /tmp/rocketzhang_test.sh
## 将创建的脚本文件分发到远程
# ansible node_test -m copy -a "src=/tmp/rocketzhang_test.sh dest=/tmp/rocketzhang_test.sh owner=root group=root mode=0755"
# ansible node_test -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh owner=root group=root mode=0755" 192.168.99.136 | success >> { "changed": true, "dest": "/tmp/test.sh", "gid": 0, "group": "root", "md5sum": "6097884da6d269fc48b4f8110d8ed592", "mode": "0755", "owner": "root", "size": 28, "src": "/root/.ansible/tmp/ansible-tmp-1481649175.38-136149173793514/source", "state": "file", "uid": 0 } 192.168.99.137 | success >> { "changed": true, "dest": "/tmp/test.sh", "gid": 0, "group": "root", "md5sum": "6097884da6d269fc48b4f8110d8ed592", "mode": "0755", "owner": "root", "size": 28, "src": "/root/.ansible/tmp/ansible-tmp-1481649175.37-73834225757834/source", "state": "file", "uid": 0 } 192.168.99.135 | success >> { "changed": false, "dest": "/tmp/test.sh", "gid": 0, "group": "root", "md5sum": "6097884da6d269fc48b4f8110d8ed592", "mode": "0755", "owner": "root", "path": "/tmp/test.sh", "size": 28, "state": "file", "uid": 0 } |
## 远程执行
# ansible node_test -m shell -a "/tmp/rocketzhang_test.sh"
# ansible node_test -m shell -a "hostname;/tmp/test.sh" 192.168.99.136 | success | rc=0 >> node02.example.com 2016-12-14_01:14:32 192.168.99.135 | success | rc=0 >> node01.example.com 2016-12-14_01:14:32 192.168.99.137 | success | rc=0 >> node03.example.com 2016-12-14_01:11:51 |
(7)、更多模块
其他常用模块,比如:service、cron、yum、synchronize就不一一例举,可以结合自身的系统环境进行测试。
service:系统服务管理
cron:计划任务管理
yum:yum软件包安装管理
synchronize:使用rsync同步文件
user:系统用户管理
group:系统用户组管理
更多模块可以参考:
#ansible-doc –l
# ansible-doc -l acl Sets and retrieves file ACL information. add_host add a host (and alternatively a group) to the ansible-playbo airbrake_deployment Notify airbrake about app deployments alternatives Manages alternative programs for common commands apache2_module enables/disables a module of the Apache2 webserver apt Manages apt-packages apt_key Add or remove an apt key apt_repository Add and remove APT repositories apt_rpm apt_rpm package manager arista_interface Manage physical Ethernet interfaces arista_l2interface Manage layer 2 interfaces arista_lag Manage port channel (lag) interfaces arista_vlan Manage VLAN resources assemble Assembles a configuration file from fragments assert Fail with custom message at Schedule the execution of a command or script file via the a authorized_key Adds or removes an SSH authorized key azure create or terminate a virtual machine in azure bigip_facts Collect facts from F5 BIG-IP devices bigip_monitor_http Manages F5 BIG-IP LTM http monitors bigip_monitor_tcp Manages F5 BIG-IP LTM tcp monitors bigip_node Manages F5 BIG-IP LTM nodes bigip_pool Manages F5 BIG-IP LTM pools |
http://docs.ansible.com/modules_by_category.html
http://www.ansible.cn/docs/
(8)、一些概念补充
playbook的组成:playbook是由一个或多个“play”组成的列表,可以让它们联同起来按事先编排的机制执行;所谓task无非是调用ansible的一个module,而在模块参数中可以使用变量;模块执行是幂等的,这意味着多次执行是安全的,因为其结果均一致;
执行模型:task list中的各任务按次序逐个在hosts中指定的所有主机上执行,即在所有主机上完成第一个任务后再开始第二个。在顺序运行某playbook时,如果中途发生错误,所有已执行任务都将回滚,因此,在修改playbook后重新执行一次即可;
task组成:每个task都应该有其name,用于playbook的执行结果输出,建议其内容尽可能清晰地描述任务执行步骤。如果未提供name,则action的结果将用于输出;
notify指定handler的执行机制:“notify”这个action可用于在每个play的最后被触发,在notify中列出的操作称为handler,仅在所有的变化发生完成后一次性地执行指定操作。
==========================================================================================
三、后续工作
==========================================================================================
1、深入学习ansible的playbook以及扩展模块;
2、 结合业务环境,初步实现基础监控,以取代目前调用自动化部署平台API的方式;
3、 尝试自动化运维工具saltstack,并将其与ansible进行对比。
学习资料:
http://www.ansible.com.cn/docs/intro_installation.html
http://blog.xiaorui.cc/category/ansible/
http://lixcto.blog.51cto.com/4834175/d-4
https://github.com/ansible/ansible-examples
http://rfyiamcool.blog.51cto.com/1030776/d-51
http://dl528888.blog.51cto.com/2382721/d-4/p-1
http://edu.51cto.com/course/course_id-2220.html
http://edu.51cto.com/course/course_id-2032.html
http://www.shencan.net/index.php/category/%e8%87%aa%e5%8a%a8%e5%8c%96%e8%bf%90%e7%bb%b4/ansible/
原文链接:http://sofar.blog.51cto.com/353572/1579894/