前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >安装ansible以及简单使用

安装ansible以及简单使用

作者头像
端碗吹水
发布2020-09-23 12:28:54
3K0
发布2020-09-23 12:28:54
举报

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已经被redhat公司收购,它在github上是一个非常受欢迎的开源软件,github地址:

https://github.com/ansible/ansible

一本不错的ansible入门电子书:

https://ansible-book.gitbooks.io/ansible-first-book/


24.16 ansible安装

资源有限本示例仅使用两台机器进行演示,角色如下:

  • 192.168.77.130   角色:服务端
  • 192.168.77.128   角色:客户端端

开始安装: 1.只需要在服务端上安装ansible:

代码语言:javascript
复制
[root@server ~]# yum list |grep ansible   # 可以看到自带源里就有2.4版本的ansible
ansible.noarch                            2.4.2.0-1.el7                epel     
ansible-doc.noarch                        2.4.2.0-1.el7                epel     
ansible-inventory-grapher.noarch          2.4.4-1.el7                  epel     
ansible-lint.noarch                       3.4.17-1.el7                 epel     
ansible-openstack-modules.noarch          0-20140902git79d751a.el7     epel     
ansible-review.noarch                     0.13.4-1.el7                 epel     
kubernetes-ansible.noarch                 0.6.0-0.1.gitd65ebd5.el7     epel     
python2-ansible-tower-cli.noarch          3.2.1-2.el7                  epel     
[root@server ~]# yum install -y ansible  # 安装

2.使用ssh-keygen命令在服务端上生成密钥对:

代码语言:javascript
复制
[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 same passphrase again:   # 回车
Your identification has been saved in ansible.
Your public key has been saved in ansible.pub.
The key fingerprint is:
77:8f:bc:a8:e5:6c:1c:5c:b5:76:c4:44:88:95:60:ee root@server
The key's randomart image is:
+--[ RSA 2048]----+
|            o+.*o|
|           o. + o|
|            .. o |
|           .. o .|
|        S...E. . |
|         .oo o   |
|         ...o .  |
|         +o. .   |
|        .o+ .    |
+-----------------+

3.建立服务端与客户端的连接,也就是配置密钥认证的SSH连接:

代码语言:javascript
复制
[root@server ~]# ssh-copy-id root@192.168.77.128  # 拷贝ssh key到客户端
/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.77.128's password:   # 输入客户端的密码

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.77.128'"
and check to make sure that only the key(s) you wanted were added.

[root@server ~]# ssh-keyscan 192.168.77.128 >> ~/.ssh/known_hosts  # 设置ssh的时候不会提示是否保存key
# 192.168.77.128 SSH-2.0-OpenSSH_6.6.1
# 192.168.77.128 SSH-2.0-OpenSSH_6.6.1
[root@server ~]# ssh root@192.168.77.128  # 测试在服务端上能否通过密钥登录客户端
Last login: Fri Jan 26 12:19:20 2018 from server
[root@client ~]# logout   # 登录成功
Connection to 192.168.77.128 closed.
[root@server ~]#

5.编辑服务端上的配置文件,配置远程主机组:

代码语言:javascript
复制
[root@server ~]# vim /etc/ansible/hosts  # 在文件末尾增加以下内容
[testhost]  # 主机组的名称,可自定义,以下的ip为该组内机器的ip
192.168.77.128

24.17 ansible远程执行命令

完成了ssh密钥认证以及主机组的配置之后就可以通过ansible对客户端远程执行命令了:

代码语言:javascript
复制
[root@server ~]# ansible testhost -m command -a 'w'  # 可以对某一组机器执行命令
192.168.77.128 | SUCCESS | rc=0 >>
 13:32:19 up  2:41,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.77.1     10:52    6:27   0.31s  0.31s -bash
root     pts/1    server           13:32    0.00s  0.08s  0.00s w

[root@server ~]# ansible testhost -m command -a 'hostname'
192.168.77.128 | SUCCESS | rc=0 >>
client

[root@server ~]# ansible 192.168.77.128 -m command -a 'hostname'  # 也可以对某个指定的ip执行命令
192.168.77.128 | SUCCESS | rc=0 >>
client

[root@server ~]# 

命令说明:

  • ansible 后面跟的是需要远程执行命令的机器,可以是一个主机组,可以是某个指定的ip或者主机名,如果使用主机名的话,需要先配置hosts
  • -m选项用于指定使用某个模块,在这里我们指定的是command 模块,这个模块可以用于远程执行命令
  • -a选项用于指定需要执行的命令,命令需要用单引号引起来

如果远程执行命令时出现以下错误:

代码语言:javascript
复制
"msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"

可以通过安装libselinux-python来解决:

yum install -y libselinux-python

除了使用command模块外,我们还可以使用shell模块来实现远程执行命令:

代码语言:javascript
复制
[root@server ~]# ansible testhost -m shell -a 'w'
192.168.77.128 | SUCCESS | rc=0 >>
 13:37:41 up  2:46,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.77.1     10:52   11:49   0.31s  0.31s -bash
root     pts/1    server           13:37    0.00s  0.09s  0.00s w

[root@server ~]# 

command与shell的区别:command模块是用于执行单条命令的,而shell模块则即可以用于执行单条命令,也可以用于执行脚本。


24.18 ansible拷贝文件或目录

拷贝目录:

代码语言:javascript
复制
[root@server ~]# ansible testhost -m copy -a "src=/etc/ansible  dest=/tmp/ansibletest owner=root group=root mode=0755"
192.168.77.128 | SUCCESS => {
    "changed": true, 
    "dest": "/tmp/ansibletest/", 
    "src": "/etc/ansible"
}
[root@server ~]# 

命令说明:

  • src指定来源目录路径
  • dest指定目标机器存储该目录的路径
  • owner指定目录的属主
  • group指定目录的属组
  • mode指定目录的权限

注意:源目录会放到目标目录下面去,如果目标指定的目录不存在,它会自动创建。如果拷贝的是文件,dest指定的名字和源如果不同,并且它不是已经存在的目录,相当于拷贝过去后又重命名。但相反,如果dest是目标机器上已经存在的目录,则会直接把文件拷贝到该目录下面。

查看客户端上有没有拷贝过去的目录:

代码语言:javascript
复制
[root@client ~]# ls /tmp/ansibletest
ansible
[root@client ~]# ls /tmp/ansibletest/ansible/
ansible.cfg  hosts  roles
[root@client ~]# 

拷贝文件:

代码语言:javascript
复制
[root@server ~]# ansible testhost -m copy -a "src=/etc/passwd dest=/tmp/passwd"
192.168.77.128 | SUCCESS => {
    "changed": true, 
    "checksum": "ddc434f503b675d6652ee8096b05f27044b944dc", 
    "dest": "/tmp/passwd", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "42426e04b715a72392e94bae51c96351", 
    "mode": "0644", 
    "owner": "root", 
    "size": 1792, 
    "src": "/root/.ansible/tmp/ansible-tmp-1516976982.08-72185336471887/source", 
    "state": "file", 
    "uid": 0
}
[root@server ~]# 

命令说明:

  • src指定来源文件路径
  • dest指定目标机器存储该文件的路径

这里的/tmp/passwd和源机器上的/etc/passwd是一致的,但如果目标机器上存在一个/tmp/passwd目录,则会在/tmp/passwd目录下面创建passwd文件。

查看客户端上有没有拷贝过去的文件:

代码语言:javascript
复制
[root@client ~]# ll /tmp/passwd 
-rw-r--r-- 1 root root 1792 1月  26 14:37 /tmp/passwd
[root@client ~]# 

24.19 ansible远程执行脚本

1.首先在服务端上创建一个简单的shell脚本以作测试:

代码语言:javascript
复制
[root@server ~]# vim  /tmp/test.sh
#!/bin/bash
echo `date` > /tmp/ansible_test.txt

2.然后把该脚本分发到远程机器上:

代码语言:javascript
复制
[root@server ~]# ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"
192.168.77.128 | SUCCESS => {
    "changed": true, 
    "checksum": "1a6e4af02dba1bda6fc8e23031d4447efeba0ade", 
    "dest": "/tmp/test.sh", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "edfaa4371316af8c5ba354e708fe8a97", 
    "mode": "0755", 
    "owner": "root", 
    "size": 48, 
    "src": "/root/.ansible/tmp/ansible-tmp-1516977585.0-265810222310208/source", 
    "state": "file", 
    "uid": 0
}
[root@server ~]# 

说明:脚本文件需要给755的权限,不然无法被直接执行。

3.最后是通过shell模块执行远程机器上的shell脚本:

代码语言:javascript
复制
[root@server ~]# ansible testhost -m shell -a "/tmp/test.sh"
192.168.77.128 | SUCCESS | rc=0 >>

[root@server ~]# 

查看远程机器上,是否执行了这个脚本生成了/tmp/ansible_test.txt文件:

代码语言:javascript
复制
[root@client ~]# cat /tmp/ansible_test.txt
Fri Jan 26 14:48:54 CST 2018
[root@client ~]# 

如上,可以看到脚本被正常执行了。

上面我们也提到了shell模块支持远程执行命令,除此之外可以使用管道符,而command模块则不支持使用管道符:

代码语言:javascript
复制
[root@server ~]# ansible testhost -m shell -a "cat /etc/passwd|wc -l"
192.168.77.128 | SUCCESS | rc=0 >>
38   # 输出的结果

[root@server ~]# 

24.20 ansible管理任务计划

ansible使用cron模块来管理任务计划:

代码语言:javascript
复制
[root@server ~]# ansible testhost -m cron -a "name='test cron' job='/bin/touch /tmp/ansible_cron.txt'  weekday=6"
192.168.77.128 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "test cron"
    ]
}
[root@server ~]# 

命令说明:

  • name指定一个名称,用于作为标识符,会出现在crontab的注释里
  • job指定需要执行的命令
  • weekday表示星期,在这里是指定星期六执行该命令,其他没有设置的时间位默认为 *

到客户端上查看crontab 是否已添加该任务计划:

代码语言:javascript
复制
[root@client ~]# crontab -l
#Ansible: test cron
* * * * 6 /bin/touch /tmp/ansible_cron.txt
[root@client ~]# 

注:crontab 中的注释不可以删除或改动,不然就会失去ansible 的管理。

若要删除该cron 只需要加一个字段 state=absent:

代码语言:javascript
复制
[root@server ~]# ansible testhost -m cron -a "name='test cron' state=absent"
192.168.77.128 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": []
}
[root@server ~]# 

删除后再去客户端查看crontab:

代码语言:javascript
复制
[root@client ~]# crontab -l
[root@client ~]# 

表示时间位的字段:

  • minute 分钟
  • hour 小时
  • day 日期
  • month 月份
  • weekday 周
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-01-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 24.15 ansible介绍
  • 24.16 ansible安装
  • 24.17 ansible远程执行命令
  • 24.18 ansible拷贝文件或目录
  • 24.19 ansible远程执行脚本
  • 24.20 ansible管理任务计划
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档