sunage001 发表于 2018-7-29 07:31:17

安装ansible以及简单使用

  笔记内容:安装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:
  

# 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
  
# yum install -y ansible# 安装
  

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

# cd .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连接:
  

# 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.
  

  
# 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
  
# ssh root@192.168.77.128# 测试在服务端上能否通过密钥登录客户端
  
Last login: Fri Jan 26 12:19:20 2018 from server
  
# logout   # 登录成功
  
Connection to 192.168.77.128 closed.
  
#
  

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

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

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

# ansible testhost -m command -a 'w'# 可以对某一组机器执行命令  
192.168.77.128 | SUCCESS | rc=0 >>
  13:32:19 up2: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.31s0.31s -bash
  
root   pts/1    server         13:32    0.00s0.08s0.00s w
  

  
# ansible testhost -m command -a 'hostname'
  
192.168.77.128 | SUCCESS | rc=0 >>
  
client
  

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

  
#
  

  命令说明:


[*]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模块来实现远程执行命令:
  

# ansible testhost -m shell -a 'w'  
192.168.77.128 | SUCCESS | rc=0 >>
  13:37:41 up2: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.31s0.31s -bash
  
root   pts/1    server         13:37    0.00s0.09s0.00s w
  

  
#
  

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

24.18 ansible拷贝文件或目录
  拷贝目录:
  

# ansible testhost -m copy -a "src=/etc/ansibledest=/tmp/ansibletest owner=root group=root mode=0755"  
192.168.77.128 | SUCCESS => {
  "changed": true,
  "dest": "/tmp/ansibletest/",
  "src": "/etc/ansible"
  
}
  
#
  

  命令说明:


[*]src指定来源目录路径
[*]dest指定目标机器存储该目录的路径
[*]owner指定目录的属主
[*]group指定目录的属组
[*]mode指定目录的权限
  注意:源目录会放到目标目录下面去,如果目标指定的目录不存在,它会自动创建。如果拷贝的是文件,dest指定的名字和源如果不同,并且它不是已经存在的目录,相当于拷贝过去后又重命名。但相反,如果dest是目标机器上已经存在的目录,则会直接把文件拷贝到该目录下面。
  查看客户端上有没有拷贝过去的目录:
  

# ls /tmp/ansibletest  
ansible
  
# ls /tmp/ansibletest/ansible/
  
ansible.cfghostsroles
  
#
  

  拷贝文件:
  

# 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
  
}
  
#
  

  命令说明:


[*]src指定来源文件路径
[*]dest指定目标机器存储该文件的路径
  这里的/tmp/passwd和源机器上的/etc/passwd是一致的,但如果目标机器上存在一个/tmp/passwd目录,则会在/tmp/passwd目录下面创建passwd文件。
  查看客户端上有没有拷贝过去的文件:
  

# ll /tmp/passwd  
-rw-r--r-- 1 root root 1792 1月26 14:37 /tmp/passwd
  
#
  

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

# vim/tmp/test.sh  
#!/bin/bash
  
echo `date` > /tmp/ansible_test.txt
  

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

# 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
  
}
  
#
  

  说明:脚本文件需要给755的权限,不然无法被直接执行。
  3.最后是通过shell模块执行远程机器上的shell脚本:
  

# ansible testhost -m shell -a "/tmp/test.sh"  
192.168.77.128 | SUCCESS | rc=0 >>
  

  
#
  

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

# cat /tmp/ansible_test.txt  
Fri Jan 26 14:48:54 CST 2018
  
#
  

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

# ansible testhost -m shell -a "cat /etc/passwd|wc -l"  
192.168.77.128 | SUCCESS | rc=0 >>
  
38   # 输出的结果
  

  
#
  

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

# 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"
  ]
  
}
  
#
  

  命令说明:


[*]name指定一个名称,用于作为标识符,会出现在crontab的注释里
[*]job指定需要执行的命令
[*]weekday表示星期,在这里是指定星期六执行该命令,其他没有设置的时间位默认为 *
  到客户端上查看crontab 是否已添加该任务计划:
  

# crontab -l  
#Ansible: test cron
  
* * * * 6 /bin/touch /tmp/ansible_cron.txt
  
#
  

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

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

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

# crontab -l  
#
  

  表示时间位的字段:


[*]minute 分钟
[*]hour 小时
[*]day 日期
[*]month 月份
[*]weekday 周
页: [1]
查看完整版本: 安装ansible以及简单使用