ansible<!>
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ansible<!>相关的知识,希望对你有一定的参考价值。
ansible:更加简洁的自动化运维工具,不需要在客户端安装agent,是基于python开发的。可以实现批量操作系统配置,批量程序部署,批量运行命令!
1:ansible不需要客户端,使用sshd通信;
2:基于模块工作,模块可由任何语言开发;
3:不仅支持命令行使用模块,也支持yaml格式的playbook;
4:支持sudo
5:提供ui,www.ansible.com/tower 10台以内免费!
一:在服务端安装ansible,安装ansible时候需要安装epel扩展源!
安装ansible特别简单就是两条命令
[[email protected] ~]# yum install -y epel-release [[email protected] ~]# yum install -y ansible
二:ansible采用ssh通信,所以我们要在主服务器与客户端之间建立信任ssh通信渠道!
[[email protected] ~]# ssh-keygen /在主服务器上操作,过程中直接回车就好! Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory ‘/root/.ssh‘. Enter passphrase (empty for no passphrase): Enter same passphrase again: 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: 90:27:8e:4f:fb:5a:0a:6b:0b:e6:00:03:d9:ab:95:29 [email protected] The key‘s randomart image is: +--[ RSA 2048]----+ | | | o . | |o . + . | |. + o + | |E = . o S | |.= o . | |o o . o . | | + ..o + | | ..o.o.. | +-----------------+ [[email protected] ~]#
然后把产生的公钥放入客户端
[[email protected] ~]# scp .ssh/id_rsa.pub 192.168.1.112:~ /把产生的公钥拷贝到客户端
在客户端检查一下是否拷贝成功:
[[email protected] ~]# ls anaconda-ks.cfg id_rsa.pub install.log install.log.syslog [[email protected] ~]#
继续在客户端操作:
[[email protected] ~]# mkdir .ssh [[email protected] ~]# cat id_rsa.pub >> .ssh/authorized_keys /这个文件名不可以改动 [[email protected] ~]# chmod 700 .ssh/ /修改目录权限 [[email protected] ~]# chmod 644 .ssh/authorized_keys /修改文件权限
然后验证一下看是否能登录上;(在服务端登录客户端)
[[email protected] .ssh]# ssh 192.168.1.112 Nasty PTR record "192.168.1.112" is set up for 192.168.1.112, ignoring Last login: Mon Mar 7 04:16:03 2016 from 192.168.1.12 [[email protected] ~]# exit logout Connection to 192.168.1.112 closed. [[email protected] .ssh]#
在本机上也进行此操作,即把产生的公钥放在 .ssh/authorized_keys目录里面;
[[email protected] ~]# cat .ssh/id_rsa.pub >> /root/.ssh/authorized_keys [[email protected] ~]# ssh 127.0.0.1 /验证一下是否有问题 The authenticity of host ‘127.0.0.1 (127.0.0.1)‘ can‘t be established. RSA key fingerprint is d4:aa:18:c4:d6:bf:54:3e:e4:d8:3b:99:30:56:f5:d2. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added ‘127.0.0.1‘ (RSA) to the list of known hosts. Last login: Mon Mar 7 04:16:00 2016 from 192.168.1.12
接下来我们设置ansible的配置,在ansible中我们可以设置组,也就是把ip地址分成组,然后我们可以针对组做一些操作:
[[email protected] ~]# vim /etc/ansible/hosts [testhosts] /设置testhosts组 127.0.0.1 192.168.1.112
ansible远程执行命令:
[[email protected] ~]# ansible testhosts -m command -a "w" /远程执行命令,ansible后面是testhosts这个组,-m后面跟要执行的模块,-a后面跟要执行的命令! 127.0.0.1 | success | rc=0 >> 05:09:31 up 56 min, 2 users, load average: 0.37, 0.08, 0.03 USER TTY FROM [email protected] IDLE JCPU PCPU WHAT root pts/0 192.168.1.12 04:16 12.00s 0.16s 0.04s ssh 127.0.0.1 root pts/1 localhost 05:03 12.00s 10.24s 0.35s /usr/bin/python 192.168.1.112 | success | rc=0 >> 05:09:33 up 56 min, 1 user, load average: 0.00, 0.00, 0.00 USER TTY FROM [email protected] IDLE JCPU PCPU WHAT root pts/0 192.168.1.12 04:16 7:13 0.10s 0.10s -bash [[email protected] ~]# ansible testhosts -m command -a "hostname" /执行命令的模块可以换成shell 127.0.0.1 | success | rc=0 >> master 192.168.1.112 | success | rc=0 >> client [[email protected] ~]#
[[email protected] ~]# ansible 127.0.0.1 -m shell -a ‘w‘ /还可以换成shell远程执行,组名也可以换成具体的ip地址 127.0.0.1 | success | rc=0 >> 05:13:49 up 1:00, 2 users, load average: 0.09, 0.07, 0.03 USER TTY FROM [email protected] IDLE JCPU PCPU WHAT root pts/0 192.168.1.12 04:16 5.00s 0.18s 0.06s ssh 127.0.0.1 root pts/1 localhost 05:03 5.00s 5.12s 5.06s /usr/bin/python [[email protected] ~]#
shell与command/所有command能够执行的命令shell都可以,shell能执行command有些不能执行,另外shell还可以执行脚本:
[[email protected] ~]# ansible 127.0.0.1 -m shell -a ‘cat /etc/passwd |grep root‘ 127.0.0.1 | success | rc=0 >> root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [[email protected] ~]# ansible 127.0.0.1 -m command -a ‘cat /etc/passwd|grep root‘ /command模块不支持管道符 127.0.0.1 | FAILED | rc=1 >> cat: /etc/passwd|grep: No such file or directory cat: root: No such file or directory [[email protected] ~]#
ansible远程拷贝目录或者文件:
[[email protected] ~]# ansible 192.168.1.112 -m copy -a "src=/etc/passwd dest=/tmp/test1" 192.168.1.112 | FAILED >> { "checksum": "6e6de716b005a43606eff5b34fb6d7b7b3289992", "failed": true, "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren‘t installed!" } /解决这个问题就是安装一个libselinux-python包,但是这个包要安装在命令中ip地址对应的那个主机中: [[email protected] ~]# yum install -y libselinux-python /主机名是不一样的,这个是client [[email protected] ~]# ansible 192.168.1.112 -m copy -a "src=/etc/passwd dest=/tmp/test1" 192.168.1.112 | success >> { "changed": true, "checksum": "6e6de716b005a43606eff5b34fb6d7b7b3289992", "dest": "/tmp/test1", "gid": 0, "group": "root", "md5sum": "cfe49aad18c3daec49a677338692758f", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:admin_home_t:s0", "size": 1338, "src": "/root/.ansible/tmp/ansible-tmp-1457300754.4-42160875185545/source", "state": "file", "uid": 0 } [[email protected] ~]#
我们来检测一下,看是否拷贝成功!自然是在客户端检测!
[[email protected] ~]# cd /tmp [[email protected] tmp]# ls ansibletest test1 yum.log [[email protected] tmp]# cat test1 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin oprofile:x:16:16:Special user account to be used by OProfile:/home/oprofile:/sbin/nologin [[email protected] tmp]#
拷贝中应该注意:源目录会放到目标目录下面去,如果目标指定的目录不存在,它会自动创建。如果拷贝的是文件,dest指定的名字和源如果不同,并且它不是已经存在的目录,相当于拷贝过去后又重命名。但相反,如果desc是目标机器上已经存在的目录,则会直接把文件拷贝到该目录下面。
[[email protected] ~]# ansible 192.168.1.112 -m copy -a "src=/etc/passwd dest=/tmp/test1 owner=root group=root mode=0755" /命令后面也可以加上文件的属性,文件主,文件组,权限
ansible远程执行脚本,执行脚本当然要用到shell模块:
[[email protected] ~]# vim /tmp/1.sh /在客户端创建一个简单的脚本 #!/bin/bash echo `date` > /tmp/test1
在服务器端上执行脚本:
[[email protected] ~]# ansible 192.168.1.112 -m shell -a ‘/bin/bash /tmp/1.sh‘ /若给脚本加上执行权限,则可以直接执行,不用再加/bin/bash! 192.168.1.112 | success | rc=0 >> [[email protected] ~]#
在客户端上检查看脚本是否被执行:
[[email protected]nt ~]# cat /tmp/test1 Mon Mar 7 06:01:27 CST 2016
本文出自 “自定义” 博客,请务必保留此出处http://zidingyi.blog.51cto.com/10735263/1748189
以上是关于ansible<!>的主要内容,如果未能解决你的问题,请参考以下文章