24.15 ansible介绍
ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供
一种框架。主要包括:
- 连接插件connection plugins:负责和被监控端实现通信;
- host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;
- 各种模块核心模块、command模块、自定义模块;
- 借助于插件完成记录日志邮件等功能;
- playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。
ansible特点:
-
不需要安装客户端,通过sshd去通信
-
基于模块工作,模块可以由任何语言开发
-
不仅支持命令行使用模块,也支持编写yaml格式的playbook,易于编写和阅读
-
安装十分简单,centos上可直接yum安装
-
有提供UI(浏览器图形化)www.ansible.com/tower , 收费的
-
ansible官网:https://www.ansible.com/
-
ansible官方文档地址:http://docs.ansible.com/ansible/latest/index.html
-
ansible在github地址:https://github.com/ansible/ansible
-
一本不错的ansible入门电子书:https://ansible-book.gitbooks.io/ansible-first-book/
24.16 ansible安装
环境:
192.168.222.114 server
192.168.222.113 client
安装:
只需要在服务端上安装ansible:yum install -y ansible
[root@server ~]# yum list |grep ansible # 可以看到自带源里就有2.4版本的ansible ansible.noarch 2.4.2.0-2.el7 extras ansible-doc.noarch 2.4.2.0-2.el7 extras [root@server ~]# yum install -y ansible
使用ssh-keygen命令生成密钥对:
[root@server ~]# cd .ssh/ [root@server .ssh]# ssh-keygen -t rsa # -t指定密钥类型 Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): 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: SHA256:QfYYQO7KFX3lvBa8X/bUKzB0Nz71soQjF9Ri079pEbo root@server The key's randomart image is: +---[RSA 2048]----+ | .o.+ oo | | . + + *+ o. | | o + oo*+.+o| | . . o. .Bo.*| | o S .oB +oX| | . o +oE X+| | o .+..| | . | | | +----[SHA256]-----+
- 建立服务端与客户端的连接,也就是配置密钥认证的SSH连接:
[root@server .ssh]# ssh-copy-id root@192.168.222.113 # 拷贝ssh key到客户端 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@192.168.222.113's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.222.113'" and check to make sure that only the key(s) you wanted were added.
设置ssh的时候不会提示是否保存key
[root@server .ssh]# ssh-keyscan 192.168.222.113 >> ~/.ssh/known_hosts # 192.168.222.113:22 SSH-2.0-OpenSSH_7.4 # 192.168.222.113:22 SSH-2.0-OpenSSH_7.4 # 192.168.222.113:22 SSH-2.0-OpenSSH_7.4
测试在服务端上能否通过密钥登录客户端
[root@server .ssh]# ssh root@192.168.222.113 Last login: Fri Oct 19 13:54:13 2018 from 192.168.222.1 [root@client ~]# logout Connection to 192.168.222.113 closed
编辑服务端上的配置文件,配置远程主机组:
[root@server ~]# vim /etc/ansible/hosts # 在文件末尾增加以下内容 [testhost] # 主机组的名称,可自定义,以下的ip为该组内机器的ip 192.168.222.113
24.17 ansible远程执行命令
完成了ssh密钥认证以及主机组的配置之后就可以通过ansible对客户端远程执行命令
通过主机组执行:
[root@server ~]# ansible testhost -m command -a 'w' 192.168.222.113 | SUCCESS | rc=0 >> 15:54:52 up 2:01, 2 users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 192.168.222.1 13:54 32:52 0.04s 0.04s -bash root pts/1 192.168.222.114 15:54 0.00s 0.13s 0.01s w [root@server ~]# ansible testhost -m command -a 'hostname' 192.168.222.113 | SUCCESS | rc=0 >> client
通过IP地址执行:
[root@server ~]# ansible 192.168.222.113 -m command -a 'hostname' 192.168.222.113 | SUCCESS | rc=0 >> client
注:
- ansible 后面跟的是需要远程执行命令的机器,可以是一个主机组,可以是某个指定的ip或者主机名,如果使用主机名的话,需要先配置hosts
- -m选项用于指定使用某个模块,在这里我们指定的是command 模块,这个模块可以用于远程执行命令
- -a选项用于指定需要执行的命令,命令需要用单引号引起来
除了使用command模块外,还可以使用shell模块来实现远程执行命令
[root@server ~]# ansible testhost -m shell -a 'w' 192.168.222.113 | SUCCESS | rc=0 >> 15:59:36 up 2:06, 2 users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 192.168.222.1 13:54 37:36 0.04s 0.04s -bash root pts/1 192.168.222.114 15:59 0.00s 0.07s 0.01s w
command与shell的区别:command模块是用于执行单条命令;而shell模块可以用于执行单条命令,也可以用于执行脚本。
24.18 ansible拷贝文件或目录
拷贝目录:
[root@server ~]# ansible testhost -m copy -a "src=/etc/ansible dest=/tmp/ansibletest owner=root group=root mode=0755" 192.168.222.113 | SUCCESS => { "changed": true, "dest": "/tmp/ansibletest/", "src": "/etc/ansible" }
注:
- src指定来源目录路径
- dest指定目标机器存储该目录的路径
- owner指定目录的属主
- group指定目录的属组
- mode指定目录的权限
源目录会放到目标目录下面去,如果目标指定的目录不存在,它会自动创建。如果拷贝的是文件,dest指定的名字和源如果不同,并且它不是已经存在的目录,相当于拷贝过去后又重命名。但相反,如果dest是目标机器上已经存在的目录,则会直接把文件拷贝到该目录下面
查看客户端拷贝的目录:
[root@client ~]# ls /tmp/ansibletest ansible [root@client ~]# ls /tmp/ansibletest/ansible/ ansible.cfg hosts roles
拷贝文件:
[root@server ~]# ansible testhost -m copy -a "src=/etc/passwd dest=/tmp/passwd" 192.168.222.113 | SUCCESS => { "changed": true, "checksum": "7b95e8a3e5794f00aab8721e93bcd504a18aa0a0", "dest": "/tmp/passwd", "gid": 0, "group": "root", "md5sum": "92191c868c0df95d909fa2983ee0eb9c", "mode": "0644", "owner": "root", "secontext": "unconfined_u:
本文详细介绍了Ansible的介绍、安装、远程执行命令、拷贝文件、执行脚本、管理任务计划、安装包和管理服务、使用playbook、变量、循环、条件判断、handlers等关键功能,通过实例演示了如何使用Ansible自动化运维,包括安装Nginx和管理配置文件的过程。
最低0.47元/天 解锁文章
7万+

被折叠的 条评论
为什么被折叠?



