设为首页 收藏本站
查看: 937|回复: 0

[经验分享] 安装ansible以及简单使用

[复制链接]

尚未签到

发表于 2018-7-29 07:31:17 | 显示全部楼层 |阅读模式
  笔记内容:安装ansible以及简单使用
  笔记日期:2018-01-26


  • 24.15 ansible介绍
  • 24.16 ansible安装
  • 24.17 ansible远程执行命令
  • 24.18 ansible拷贝文件或目录
  • 24.19 ansible远程执行脚本
  • 24.20 ansible管理任务计划
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:
  

[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命令在服务端上生成密钥对:
  

[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>  
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连接:
  

[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.编辑服务端上的配置文件,配置远程主机组:
  

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

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

[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@  >  
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选项用于指定需要执行的命令,命令需要用单引号引起来
  如果远程执行命令时出现以下错误:
  

"msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"  

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

  yum install -y libselinux-python

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

[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@  >  
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拷贝文件或目录
  拷贝目录:
  

[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是目标机器上已经存在的目录,则会直接把文件拷贝到该目录下面。
  查看客户端上有没有拷贝过去的目录:
  

[root@client ~]# ls /tmp/ansibletest  
ansible
  
[root@client ~]# ls /tmp/ansibletest/ansible/
  
ansible.cfg  hosts  roles
  
[root@client ~]#
  

  拷贝文件:
  

[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文件。
  查看客户端上有没有拷贝过去的文件:
  

[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脚本以作测试:
  

[root@server ~]# vim  /tmp/test.sh  
#!/bin/bash
  
echo `date` > /tmp/ansible_test.txt
  

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

[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脚本:
  

[root@server ~]# ansible testhost -m shell -a "/tmp/test.sh"  
192.168.77.128 | SUCCESS | rc=0 >>
  

  
[root@server ~]#
  

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

[root@client ~]# cat /tmp/ansible_test.txt  
Fri Jan 26 14:48:54 CST 2018
  
[root@client ~]#
  

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

[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模块来管理任务计划:
  

[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 是否已添加该任务计划:
  

[root@client ~]# crontab -l  
#Ansible: test cron
  
* * * * 6 /bin/touch /tmp/ansible_cron.txt
  
[root@client ~]#
  

  注:crontab 中的注释不可以删除或改动,不然就会失去ansible 的管理。
  若要删除该cron 只需要加一个字段 state=absent:
  

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

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

[root@client ~]# crontab -l  
[root@client ~]#
  

  表示时间位的字段:


  • minute 分钟
  • hour 小时
  • day 日期
  • month 月份
  • weekday 周

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-542749-1-1.html 上篇帖子: ansible 用户批量创建与管理 下篇帖子: Ansible核心技术
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表