Ansible 运维自动化(一)

Ansible只需要在一台普通的服务器上运行即可,不需要在被管控的服务器上安装客户端。因为它是基于SSH的,Linux服务器离不开SSH,所以Ansible不需要为配置工作添加额外的支持。 你可以通过命令行来使用Ansible,运行Ansible的服务器这里俗称“管理节点”;通过Ansible进行管理的服务器俗称“受控节点”。

Ansible优点: 1) 轻量级,不需要去客户端安装agent,更新时,只需要在操作机上进行一次更新即可,采用SSH协议。 
2) 批量任务执行可以写成脚本,而且不用分发到远程就可以执行。 
3) 使用python编写的,维护更简单。 
4) 支持sudo普通用户命令。

1. 安装ansible

CentOS直接使用yum安装即可,安装之前先安装epel源码。

rpm -Uvh http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm 

yum -y install

2. 配置ansible

ansible默认的配置文件:ll /etc/ansible/ total 20 -rw-r--r-- 1 root root 8347 Jul 8 11:14 ansible.cfg -rw-r--r-- 1 root root 106 Jul 8 16:45 hosts drwxr-xr-x 2 root root 4096 Jul 8 11:13 rolesansible.cfg ansible的配置文件

hosts 定义主机,支持IP和域名,支持分组。有静态和动态之分。动态hosts后续介绍

简单的hosts: [test] 172.20.9.141 ansiblesshuser=root ansiblesshpass=xxxxx [test2]
172.20.9.145 ansible
ssh
user=root ansiblesshpass=xxxxx ansiblesshport=22222 [local] 127.0.0.1 ansiblesshuser=root ansiblesshpass=xxxxx [test3] 172.16.10.125:55536 ansiblesshuser=root ansiblesshpass=xxxxx [test4] 172.16.10.126:55536 ansiblesshuser=root ansiblesshpass=xxxxx

test 组信息 172.20.9.141 这个组里的ip ansiblesshuser=root ansible ssh使用root用户 ansiblesshpass 密码 ansiblesshport=22222 远程端口 免密钥配置: 做ssh密钥认证 将你ssh用户的密钥拷到组里的机器即可 这里不做演示了

3. ansible 常用模块演示

常用参数介绍

-u 以什么用户在远程主机执行命令。默认是root -i 用来指定inventory文件即主机清单文件 默认是hosts -m 模块 指定用哪个模块运行 默认用command模块 -a 指定模块的参数,每个模块都有相应的模块参数 -f 10指定并发数,并发量大的时候提高该值 可以修改配置文件 
-k 提示输入密码

ansible -i /etc/ansible/hosts test -u root -m command -a ‘ls -l /home‘ -k

172.20.9.141 | success | rc=0 >> total 28 drwxr-xr-x. 6 git git 4096 Jul 18 21:33 git drwxr-xr-x. 2 gitlabci gitlabci 4096 Jul 18 18:10 gitlab_ci drwx------. 2 root root 16384 Jul 18 02:13 lost+found drwx------. 5 www www 4096 Jul 18 06:44 www

ansible test -a ‘ls -l /home‘ 这是上一条命令的简写

ansible-doc -l 查看所有自带的模块

3.1 file 模块

设置文件的属性 file模块包含如下选项: force:需要在两种情况下强制创建软链接,一种是源文件不存在但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no group:定义文件/目录的属组 mode:定义文件/目录的权限 owner:定义文件/目录的属主 path:必选项,定义文件/目录的路径 recurse:递归的设置文件的属性,只对目录有效 src:要被链接的源文件的路径,只应用于state=link的情况 dest:被链接到的路径,只应用于state=link的情况 state: directory:如果目录不存在,创建目录 file:即使文件不存在,也不会被创建 link:创建软链接 hard:创建硬链接 touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间 absent:删除目录、文件或者取消链接文件 示例:ansible test -m file -a ‘src=/etc/fstab dest=/tmp/fstab state=link‘ 172.20.9.141 | success >> { "changed": true, "dest": "/tmp/fstab", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "size": 10, "src": "/etc/fstab", "state": "link", "uid": 0 }

ansible test -m file -a ‘path=/tmp/abc state=directory‘ 172.20.9.141 | success >> { "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/tmp/abc", "size": 4096, "state": "directory", "uid": 0 }

3.2 copy模块

复制文件到远程主机 copy模块包含如下选项: backup:在覆盖之前将原文件备份,备份文件包含时间信息。有两个选项:yes|no content:用于替代"src",可以直接设定指定文件的值 dest:必选项。要将源文件复制到的远程主机的绝对路径,如果源文件是一个目录,那么该路径也必须是个目录 directory_mode:递归的设定目录的权限,默认为系统默认权限 force:如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖,如果为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes others:所有的file模块里的选项都可以在这里使用 src:要复制到远程主机的文件在本地的地址,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用"/"来结尾,则只复制目录里的内容,如果没有使用"/"来结尾,则包含目录在内的整个内容全部复制,类似于rsync。 validate :The validation command to run before copying into place. The path to the file to validate is passed in via ‘%s‘ which must be present as in the visudo example below. 示例:ansible test -m copy -a "src=/root/is143 dest=/tmp/is143 owner=root group=root mode=0644" 172.20.9.141 | success >> { "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/tmp/is143", "gid": 0, "group": "root", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "mode": "0644", "owner": "root", "size": 0, "src": "/root/.ansible/tmp/ansible-tmp-1445325647.87-130157182838705/source", "state": "file", "uid": 0 }如果文件内容未修改 再次执行命令不会更新文件,因为比对md5值 如果要是文件内容更新 再吃运行命令才会更新文件

3.3 command模块

creates:一个文件名,当该文件存在,则该命令不执行
free_form:要执行的linux指令
chdir:在执行指令之前,先切换到该指定的目录
removes:一个文件名,当该文件不存在,则该选项不执行
executable:切换shell来执行指令,该执行路径必须是一个绝对路径

示例: ansible test -a ‘pwd chdir=/etc‘ 172.20.9.141 | success | rc=0 >> /etc

3.4 shell模块

和command 类似 支持管道 ``` [[email protected] ~]# ansible test -m shell -a "chdir=/etc find ./ -name "hosts" -type f |awk -F‘/‘ ‘{print $2}‘ " 172.20.9.141 | success | rc=0 >> ./hosts ./ansible/hosts

[[email protected] ~]# ansible test -a "chdir=/etc find ./ -name "hosts" -type f |awk -F‘/‘ ‘{print $2}‘ " 
172.20.9.141 | FAILED | rc=1 >> find: paths must precede expression: |awk Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression] ```

3.5 service模块

用于管理服务 该模块包含如下选项: arguments:给命令行提供一些选项 enabled:是否开机启动 yes|no name:必选项,服务名称 pattern:定义一个模式,如果通过status指令来查看服务的状态时,没有响应,就会通过ps指令在进程中根据该模式进行查找,如果匹配到,则认为该服务依然在运行 runlevel:运行级别 sleep:如果执行了restarted,在则stop和start之间沉睡几秒钟 state:对当前服务执行启动,停止、重启、重新加载等操作(started,stopped,restarted,reloaded) 示例:ansible test -m service -a "name=sshd state=started enabled=yes runlevel=3,5" 172.20.9.141 | success >> { "changed": true, "enabled": true, "name": "sshd", "state": "started" }

ansible test -m service -a "name=network state=restarted args=eth0" 172.20.9.141 | success >> { "changed": true, "name": "network", "state": "started" }

3.6 synchronize模块

使用rsync同步文件 archive checksum delete #yes dest 目标目录 src 源目录 destport compress=yes 开启压缩,默认为开启 existingonly: skip createing new files on receiver links owner mode:(push, pull) 推送模式为拉取模式和推送模式 recursive rsync_path times:Preserve modification times 示例:

3.7 get_url模块

下载模块ansible test -m get_url -a "dest=/tmp url=http://nginx.org/download/nginx-1.9.5.tar.gz" 172.20.9.141 | success >> { "changed": true, "checksum": "669f1653f539358ad1d1b8281041f962597ec637", "dest": "/tmp/nginx-1.9.5.tar.gz", "gid": 0, "group": "root", "md5sum": "2562320f1535e3e31d165e337ae94f21", "mode": "0644", "msg": "OK (884023 bytes)", "owner": "root", "sha256sum": "", "size": 884023, "src": "/tmp/tmpe6NYWN", "state": "file", "uid": 0, "url": "http://nginx.org/download/nginx-1.9.5.tar.gz" }

3.8 script模块

远程机器执行 ansible本地脚本172.20.9.141 | success >> { "changed": true, "rc": 0, "stderr": "OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: Applying options for *\r\ndebug1: auto-mux: Trying existing master\r\nControl socket connect(/root/.ansible/cp/ansible-ssh-172.20.9.141-22-root): Connection refused\r\ndebug1: Connecting to 172.20.9.141 [172.20.9.141] port 22.\r\ndebug1: fd 3 clearing O_NONBLOCK\r\ndebug1: Connection established.\r\ndebug1: permanently_set_uid: 0/0\r\ndebug1: identity file /root/.ssh/identity type -1\r\ndebug1: identity file /root/.ssh/identity-cert type -1\r\ndebug1: identity file /root/.ssh/id_rsa type 1\r\ndebug1: identity file /root/.ssh/id_rsa-cert type -1\r\ndebug1: identity file /root/.ssh/id_dsa type -1\r\ndebug1: identity file /root/.ssh/id_dsa-cert type -1\r\ndebug1: identity file /root/.ssh/id_ecdsa type -1\r\ndebug1: identity file /root/.ssh/id_ecdsa-cert type -1\r\ndebug1: Remote protocol version 2.0, remote software version OpenSSH_5.3\r\ndebug1: match: OpenSSH_5.3 pat OpenSSH*\r\ndebug1: Enabling compatibility mode for protocol 2.0\r\ndebug1: Local version string SSH-2.0-OpenSSH_5.3\r\ndebug1: SSH2_MSG_KEXINIT sent\r\ndebug1: SSH2_MSG_KEXINIT received\r\ndebug1: kex: server->client aes128-ctr hmac-md5 [email protected]\r\ndebug1: kex: client->server aes128-ctr hmac-md5 [email protected]\r\ndebug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent\r\ndebug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP\r\ndebug1: SSH2_MSG_KEX_DH_GEX_INIT sent\r\ndebug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY\r\ndebug1: Host ‘172.20.9.141‘ is known and matches the RSA host key.\r\ndebug1: Found key in /root/.ssh/known_hosts:2\r\ndebug1: ssh_rsa_verify: signature correct\r\ndebug1: SSH2_MSG_NEWKEYS sent\r\ndebug1: expecting SSH2_MSG_NEWKEYS\r\ndebug1: SSH2_MSG_NEWKEYS received\r\ndebug1: SSH2_MSG_SERVICE_REQUEST sent\r\ndebug1: SSH2_MSG_SERVICE_ACCEPT received\r\ndebug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password\r\ndebug1: Next authentication method: password\r\ndebug1: Enabling compression at level 6.\r\ndebug1: Authentication succeeded (password).\r\ndebug1: setting up multiplex master socket\r\nControlSocket /root/.ansible/cp/ansible-ssh-172.20.9.141-22-root already exists, disabling multiplexing\r\ndebug1: channel 0: new [client-session]\r\ndebug1: Requesting [email protected]\r\ndebug1: Entering interactive session.\r\ndebug1: Sending environment.\r\ndebug1: Sending env LANG = zh_CN.UTF-8\r\ndebug1: Sending command: LANG=C LC_CTYPE=C /root/.ansible/tmp/ansible-tmp-1445328794.5-82306162559014/h.sh \r\ndebug1: client_input_channel_req: channel 0 rtype exit-status reply 0\r\ndebug1: client_input_channel_req: channel 0 rtype [email protected] reply 0\r\ndebug1: channel 0: free: client-session, nchannels 1\r\ndebug1: fd 1 clearing O_NONBLOCK\r\ndebug1: fd 2 clearing O_NONBLOCK\r\nConnection to 172.20.9.141 closed.\r\nTransferred: sent 1832, received 2048 bytes, in 0.0 seconds\r\nBytes per second: sent 176371.2, received 197166.1\r\ndebug1: Exit status 0\r\ndebug1: compress outgoing: raw data 530, compressed 368, factor 0.69\r\ndebug1: compress incoming: raw data 114, compressed 92, factor 0.81\r\n", "stdout": "141.com\r\n" }还有其他常用模块本人不在一一介绍 ansible-doc 模块 -h 可查看模块帮助信息

时间: 2024-08-24 14:36:56

Ansible 运维自动化(一)的相关文章

Ansible运维自动化

Ansible运维自动化 一.Ansible-playbook的初步使用 playbook的使用,playbook可以把ansible的模块进行组合 ln -s /usr/local/python/bin/ansible-playbook /usr/local/bin/ 1.playbook的简单shell模块使用 [[email protected] scripts]# cat test_shell.yaml  #playbook的执行模板 ---         #开头三个小-开头 - ho

&lt;zz&gt;Ansible 运维自动化 ( 配置管理工具 )

from http://www.cnblogs.com/wangxiaoqiangs/p/5685239.html 简介: 当下有许多的运维自动化工具( 配置管理 ),例如:Ansible.SaltStack.Puppet.Fabric 等. Ansible 一种集成 IT 系统的配置管理.应用部署.执行特定任务的开源平台,是 AnsibleWorks 公司名下的项目,该公司由 Cobbler 及 Func 的作者于 2012 年创建成立. Ansible 基于 Python 语言实现,由 Pa

ansible运维自动化工具

软件包的安装 rpm -ivh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm yum install ansible -y 配置免密钥 在master服务器生成ssh-key,并分发到所有客户端 ssh-keygen -t rsa [一路回车] ssh-copy-id -i ~/.ssh/id_rsa.pub[客户端IP地址] 配置安装完成之后,先来配置下配置项——.ansible.cfg.ans

运维自动化之ansible playbook安装mysql

上次介绍了如何使用ansible playbook安装zabbix客户端(http://dl528888.blog.51cto.com/2382721/1436745),这次介绍一下如何使用playbook安装mysql. 下面是安装mysql的信息: mysql_basedir: /data/mysql/basedir                    源码目录 mysql_datadir: /data/mysql/datadir                    数据目录 mysql

运维自动化之ansible playbook安装ruby环境

本来不想打算写安装ruby的,但看几个puppet的群里有人对安装ruby比较茫然,所以这里简单介绍一下如何安装ruby. ps:话说现在也就gitlab.capistrano.puppet等软件使用ruby,最新2010年的软件好的都是python了,比如ansible.salt等. 下面是安装ruby的信息: ruby_version: 1.9.3 ruby_dir: /usr/local gem_version: 1.8.23 bundle_version: 1.6.3 可以看到ruby的

结合Ansible在AWS云计算平台上实现运维自动化

刚刚看了金山梁晓聪的"在AWS上的运维自动化实践分享",发现技术都是相通的,大家都是用最好的技术.我们的业务平台主要也是AWS云计算平台,尝试了许多自动化运维/配置工具,最后还是选终了Ansible. 下一步在公司运维自动化DevOps要做的工作:增大Ansible在系统中的应用比重,真正跟AWS结合起来.选择 Ansible 主要因为丰富的相关支持,包括很多现有的组件和模块和开源的 Ansible 部署和脚本.笔者也尝试了市面上所有自动化运维和自动化配置工具,发现Ansible是对A

运维自动化之ansible playbook安装node环境

现在介绍如何使用ansible安装node. 下面是安装node的信息: node_dir: /data node_version: 0.10.21 node_port: 3301 可以看到node的版本是0.10.21,测试的node应用服务监听3301端口 备注:此playbook仅能对centos或者redhat的6.x版本进行安装. 下面是安装node的playbook结构 09:33:16 # tree node_* node_delete ├── files ├── handlers

运维自动化之ansible playbook结合docker安装smokeping

本次介绍ansible的paly book结合docker进行虚拟机里安装2.6.8版本smokeping(apache版本是2.4.7). docker版本 09:26:53 # docker version Client version: 0.11.1 Client API version: 1.11 Go version (client): go1.2.1 Git commit (client): fb99f99/0.11.1 Server version: 0.11.1 Server A

运维自动化之ansible playbook安装lamp环境

下面介绍使用ansible playbook安装lamp环境 下面是apache的安装信息: apr_version: 1.5.0 apr_util_version: 1.5.3 libiconv_version: 1.14 apache_version: 2.4.7 apache_web_dir: /data/webroot/apache apache_log: /data/webroot/apache/logs apache_vhost: /data/webroot/apache/vhost