ansible是基于模块工做的,自己没有批量部署的能力。真正具备批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:python
(1)、链接插件connection plugins:负责和被监控端实现通讯;shell
(2)、host inventory:指定操做的主机,是一个配置文件里面定义监控的主机;vim
(3)、各类模块核心模块、command模块、自定义模块;bash
(4)、借助于插件完成记录日志邮件等功能;框架
(5)、playbook:剧本执行多个任务时,非必需可让节点一次性运行多个任务。--摘自360百科运维
简而言之ansible有以下的特色:ssh
(一)批量管理工具工具
(二)模块oop
(三)python
(四)无终端,是基于ssh实现管理的
(五)也支持主从模式
(六)也支持playbook
Ansible的安装
能够直接使用yum进行安装,前提是你已经配置了epel源
第一步:yum安装ansible
[root@ken ~]# yum install ansible -y
第二步:查看ansible的版本信息
能够看到个人安装版本是2.6.2的
[root@ken ~]# ansible --version ansible 2.6.2 config file = /etc/ansible/ansible.cfg configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python2.7/site-packages/ansible executable location = /usr/bin/ansible python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
第三步:查看ansible配置文件
咱们接下来各个节点的管理主要是配置/etc/ansible/hosts文件
[root@ken ~]# rpm -qc ansible /etc/ansible/ansible.cfg /etc/ansible/hosts
Ansible使用基本配置
第一步:把须要管理的节点的IP地址加入到/etc/ansible/hosts文件中
在文件末行添加以下三行信息,第一行是定义了一个主机组,下面两行只要把须要管理的IP地址便可填写进去便可
[root@ken ~]# vim /etc/ansible/hosts ... # Here's another example of host ranges, this time there are no # leading 0s: ## db-[99:101]-node.example.com ##############在末行添加以下信息#################
[ken] 10.220.5.138 10.220.5.139
第二步:发送秘钥至被操控节点
ansible是基于sshd服务,因此若是咱们须要管理其余节点的话,须要给各个节点发送秘钥
在主节点生成秘钥,发送至各个被监控节点
使用以下脚本便可进行批量安装
#!/bin/bash . /etc/init.d/functions
#author:技术流ken
#date:2018.11.16
#desc:this script for ssh key #下载expect yum install expect -y &>/dev/null if [ $? -eq 0 ];then echo -n "download expect" success echo "" else echo -n "download expect" failure echo "" exit 8 fi #删除保存的秘钥信息 if [ -f id_rsa -o -f id_rsa.pub -o known_hosts ];then rm -rf /root/.ssh/id* rm -rf /root/.ssh/known* fi #自动生成秘钥对 /usr/bin/expect<<eof spawn ssh-keygen expect { "(/root/.ssh/id_rsa)" {send \r;exp_continue} "passphrase" {send \r;exp_continue} "again" {send \r} } expect eof exit eof #在各个节点分发秘钥 for i in 37 38 39 do ken=10.220.5.1$i /usr/bin/expect<<eof spawn ssh-copy-id $ken expect { "yes/no" {send yes\r;exp_continue} "password" {send o\r} } expect eof exit eof done
Ansible使用基本格式
ansible使用格式
能够输入一个ansible回车便可看到使用格式
[root@ken ~]# ansible
Usage: ansible <host-pattern> [options]
ansible经常使用使用选型
你能够输入ansible回车看到不少的选型,这里选取几个比较经常使用的选项进行说明
-m:指定模块名称 -a:指定模块的具体参数 -s:以sudo的方式运行操做 -i:指定被管理节点的主机列表 -f:一批链接几个主机进行操做(默认是5个主机)
Ansible模块使用帮助
正如咱们前文介绍的,ansible是基于模块来工做的,因此要想使用ansible,必须对ansible的模块有个清晰的认识。
查看模块
可使用以下命令进行查看所支持的模块
[root@ken ~]# ansible-doc -l
获取模块使用帮助
使用-s指定获取shell模块的使用帮助
[root@ken ~]# ansible-doc -s shell - name: Execute commands in nodes. shell: chdir: # cd into this directory before running the command creates: # a filename, when it already exists, this step will *not* be run. executable: # change the shell used to execute the command. Should be an absolute path to the executable. free_form: # (required) The shell module takes a free form command to run, as a string. There's not an actual option named "free form". See the examples! removes: # a filename, when it does not exist, this step will *not* be run. stdin: # Set the stdin of the command directly to the specified value. warn: # if command warnings are on in ansible.cfg, do not warn about this particular line if set to no/false.
Ansible管理节点的三种方法
还记得刚才在hosts文件添加的三行内容吗?
[ken] 10.220.5.138 10.220.5.139
在使用ansible的时候你能够指定主机组名,也能够指定一个IP,也能够用all来表示所有,由于你的配置文件里面可能不止一个主机组名,想要实现批量管理,就要用到了all.
因此这里有三种使用方法
(一)指定主机组名
经过以下的命令就能够获取到了整个主机组节点的信息
[root@ken ~]# ansible ken -m command -a "hostname" 10.220.5.138 | SUCCESS | rc=0 >> ken 10.220.5.139 | SUCCESS | rc=0 >> ken
(二)指定一个特定IP
指定ip 10.220.5.138获取特定节点的信息
[root@ken ~]# ansible 10.220.5.138 -m command -a "ip a" 10.220.5.138 | SUCCESS | rc=0 >> 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 1000 link/ether 00:0c:29:a9:90:16 brd ff:ff:ff:ff:ff:ff inet 10.220.5.138/24 brd 10.220.5.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fea9:9016/64 scope link valid_lft forever preferred_lft forever
(三)使用all
由于在配置文件里面我只定义了一个主机组,因此这里呈现的效果和使用ken是同样的,你们能够尝试定义多个主机组,再使用all.
[root@ken ~]# ansible all -m command -a "ls /tmp" 10.220.5.138 | SUCCESS | rc=0 >> ansible_TpWP26 hsperfdata_root hsperfdata_zabbix systemd-private-495d844cb6f24a5fa04192c973de9274-chronyd.service-SVap94 systemd-private-495d844cb6f24a5fa04192c973de9274-httpd.service-Grw0SF systemd-private-79452c683402427e944cc4959183f774-httpd.service-DENLXJ systemd-private-79452c683402427e944cc4959183f774-ntpd.service-cH4QGP systemd-private-f0243ed42bf34679b61e0687522914f6-chronyd.service-DADZWt systemd-private-f0243ed42bf34679b61e0687522914f6-httpd.service-lCPw92 vmware-root 10.220.5.139 | SUCCESS | rc=0 >> ansible_bxGz8A systemd-private-2e376cd91398450f85a81bc060207ef8-chronyd.service-TxdhUO systemd-private-2e376cd91398450f85a81bc060207ef8-httpd.service-k8IZOZ systemd-private-5c9f32d6cff64520b10075e086d943ab-chronyd.service-iAH3c0 systemd-private-5c9f32d6cff64520b10075e086d943ab-httpd.service-dsAqeg systemd-private-65ded84926e64a90b0a201a805f752ca-chronyd.service-eSj3iR systemd-private-6706ba5361284cd4a0c91f3c8b68c606-chronyd.service-sLgAei systemd-private-6706ba5361284cd4a0c91f3c8b68c606-httpd.service-op5Yg7 vmware-root yum_save_tx.2018-11-15.16-02.KHC9kd.yumtx