Ansible基础认识及安装使用详解(一)--技术流ken,ansibleken


Ansible简介

ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。

ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:

(1)、连接插件connection plugins:负责和被监控端实现通信;

(2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;

(3)、各种模块核心模块、command模块、自定义模块;

(4)、借助于插件完成记录日志邮件等功能;

(5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。--摘自360百科

 

简而言之ansible有如下的特点:

(一)批量管理工具

(二)模块

(三)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

郑州包皮手术医院

郑州最好不孕不育医院

郑州不孕不育医院

郑州哪家不孕不育医院好

相关内容

    暂无相关文章