当前位置:   article > 正文

ansible 安装与简单使用_ansible需要被管理端支持pythonn嘛

ansible需要被管理端支持pythonn嘛

ansible自动化运维(一)——基本架构及安装使用

1.介绍

ansible是一种自动化运维工具,基于paramiko开发的,并且基于模块化工作,Ansible是一种集成IT系统的配置管理、应用部署、执行特定任务的开源平台。

它是基于python语言,由Paramiko和PyYAML两个关键模块构建。集合了众多运维工具的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。

ansible是基于模块工作的,本身没有批量部署的能力,真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架.ansible不需要在远程主机上安装client/agents,因为它们是基于ssh来和远程主机通讯的。

ansible被定义为配置管理工具,配置管理工具通常具有以下功能

  • 确保所依赖的软件包已经被安装
  • 配置文件包含正确的内容和正确的权限
  • 相关服务被正确运行
项目puppetSaltStackAnsible
开发语言RubyPythonPython
是否有客户端
是否支持二次开发不支持支持支持
服务器与远程机器是否相互验证
服务器与远程机器的通信是否加密是,标准的SSL协议是,使用AES加密是,使用OpenSSH
平台支持AIX , BSD, HP-UX, Linux , Mac OSX , Solaris, WindowsBSD, Linux , Mac OS X , Solaris, WindowsAIX , BSD , HP-UX , Linux , Mac OS X , Solaris
是否提供Web UI提供提供提供,但是前提是商务版本
配置文件Ruby 语法模式YAMLYAML
命令行执行不支持,但是可以通过配置模块实现支持支持

一、ansible基础架构

ansible系统由控制主机和被管理主机组成,控制主机不支持windows平台

在这里插入图片描述

  • 核心: ansible
  • Core Modules: ansible自带的模块
  • Custom Modules: 核心模块功能不足时,用户可以添加扩展模块
  • Plugins: 通过插件来实现记录日志,发送邮件或其他功能
  • Playbooks: 剧本,YAML格式文件,多个任务定义在一个文件中,定义主机需要调用哪些模块来完成的功能
  • Connectior Plugins: ansible基于连接插件连接到各个主机上,默认是使用ssh
  • Host Inventory: 记录由Ansible管理的主机信息,包括端口、密码、ip等

1.ansible特点
部署简单, 只需要在控制主机上部署ansible环境,被控制端上只要求安装ssh和python 2.5以上版本(python3),这个对于类unix系统来说相当与无需配置。

  • no angents: 被管控节点无需安装agent
  • no server: 无服务端,使用是直接调用命名
  • modules in any languages: 基于模块工作, 可以使用任意语言开发模块
  • 易读的语法: 基于yaml语法编写playbook
  • 基于推送模式: 不同于puppet的拉取模式,直接由调用者控制变更在服务器上发生的时间
  • 模块是幂等性的:定义的任务已存在则不会做任何事情,意味着在同一台服务器上多次执行同一个playbook是安全的
  • 且ansible的工作方式是由多个配置文件共同去完成某一项目(解耦)

ansible程序目录结构:

配置文件: /etc/ansible/
执行文件目录: /usr/bin/
  • 1
  • 2

二、ansible任务执行

1.ansible工作原理:

在这里插入图片描述
2.ansible主要组成部分功能说明:

  • PLAYBOOKS:任务剧本(任务集),编排定义Ansible任务集的配置文件,由Ansible顺序依次执行,通常是JSON格式的YML文件
  • INVENTORY:Ansible管理主机的清单/etc/anaible/hosts
  • MODULES:Ansible执行命令的功能模块,多数为内置的核心模块,也可自定义,ansible-doc –l 可查看模块
  • PLUGINS:模块功能的补充,如连接类型插件、循环插件、变量插件、过滤插件等,该功能不常用
  • API: 供第三方程序调用的应用程序编程接口
  • ANSIBLE:组合INVENTORY、 API、 MODULES、PLUGINS的绿框,可以理解为是ansible命令工具,其为核心执行工具

3.ansible注意事项:

  • 执行ansible的主机一般称为主控端,中控,master或堡垒机
  • 主控端Python版本需要2.6或以上
  • 被控端Python版本小于2.4需要安装python-simplejson
  • 被控端如开启SELinux需要安装libselinux-python
  • windows不能做为主控端

4.ansible任务执行模式

Ansible任务执行模式分为以下两种:

  • ad-hoc模式(点对点模块)
    使用单个模块,支持批量执行单条命令,相当与在bash中执行一句shell命令
  • playbook模式(剧本模式)
    ansible主要的管理方式,通过多个task的集合完成一类功能,可以理解为多个ad-hoc的配置文件

5.ansible执行流程:

在这里插入图片描述

2.安装

一台控制主机:192.168.58.30
一台被管理主机:192.168.58.40

安装要求:

控制服务器:需要安装 Python2.6/2.7
管理服务器:需要安装 Python2.4 以上版本,若低于 Python2.5 需要安装 pythonsimplejson; 若启用了 selinux,则需要安装 libselinux-python。

提供YUM源

[root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2595  100  2595    0     0   3639      0 --:--:-- --:--:-- --:--:--  3639

[root@localhost ~]# yum -y install epel-release
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
上次元数据过期检查:0:04:14 前,执行于 2021年07月13日 星期二 16时17分02秒。
依赖关系解决。
===========================================================================================================
 软件包                       架构                   版本                     仓库                    大小
===========================================================================================================
安装:
 epel-release                 noarch                 8-11.el8                 extras                  24 k

事务概要
===========================================================================================================
安装  1 软件包

总下载:24 k
安装大小:35 k
下载软件包:
epel-release-8-11.el8.noarch.rpm                                            57 kB/s |  24 kB     00:00    
-----------------------------------------------------------------------------------------------------------
总计                                                                        56 kB/s |  24 kB     00:00     
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                                                                            1/1 
  安装    : epel-release-8-11.el8.noarch                                                               1/1 
  运行脚本: epel-release-8-11.el8.noarch                                                               1/1 
  验证    : epel-release-8-11.el8.noarch                                                               1/1 
Installed products updated.

已安装:
  epel-release-8-11.el8.noarch                                                                             

完毕!
[root@localhost ~]# 
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
CentOS-Base.repo   epel-playground.repo  epel-testing-modular.repo  redhat.repo
epel-modular.repo  epel.repo             epel-testing.repo



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

安装ansible

1.控制节点

[root@localhost ~]# yum -y install ansible
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Extra Packages for Enterprise Linux Modular 8 - x86_64                      55 kB/s | 663 kB     00:11    
Extra Packages for Enterprise Linux 8 - x86_64                             339 kB/s |  10 MB     00:30    
上次元数据过期检查:0:00:04 前,执行于 2021年07月13日 星期二 16时24分47秒。
依赖关系解决。
===========================================================================================================
 软件包                          架构        版本                                     仓库            大小
===========================================================================================================
安装:
 ansible                         noarch      2.9.23-1.el8                             epel            17 M
升级:
 platform-python-pip             noarch      9.0.3-19.el8                             base           1.7 M
 platform-python-setuptools      noarch      39.2.0-6.el8                             base           632 k
安装依赖关系:
 libsodium                       x86_64      1.0.18-2.el8                             epel           162 k
 python3-babel                   noarch      2.5.1-5.el8  
 验证    : platform-python-pip-9.0.3-16.el8.noarch                                                  17/19 
  验证    : platform-python-setuptools-39.2.0-6.el8.noarch                                           18/19 
  验证    : platform-python-setuptools-39.2.0-5.el8.noarch                                           19/19 
Installed products updated.

已升级:
  platform-python-pip-9.0.3-19.el8.noarch          platform-python-setuptools-39.2.0-6.el8.noarch         

已安装:
  ansible-2.9.23-1.el8.noarch                   libsodium-1.0.18-2.el8.x86_64                             
  python3-babel-2.5.1-5.el8.noarch              python3-bcrypt-3.1.6-2.el8.1.x86_64                       
  python3-jinja2-2.10.1-2.el8_0.noarch          python3-jmespath-0.9.0-11.el8.noarch                      
  python3-markupsafe-0.23-19.el8.x86_64         python3-paramiko-2.4.3-1.el8.noarch                       
  python3-pip-9.0.3-19.el8.noarch               python3-pyasn1-0.3.7-6.el8.noarch                         
  python3-pynacl-1.3.0-5.el8.x86_64             python3-pytz-2017.2-9.el8.noarch                          
  python3-setuptools-39.2.0-6.el8.noarch        python36-3.6.8-2.module_el8.4.0+790+083e3d81.x86_64       
  sshpass-1.06-9.el8.x86_64                    

完毕!
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

2.被控节点

    //安装网络yum源
[root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2595  100  2595    0     0   3639      0 --:--:-- --:--:-- --:--:--  3639

//安装epel源
[root@localhost ~]# yum -y install epel-release 
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Repository AppStream is listed more than once in the configuration
CentOS-8 - Base - mirrors.aliyun.co 1.3 MB/s | 3.6 MB     00:02    
CentOS-8 - Extras - mirrors.aliyun.  20 kB/s | 9.8 kB     00:00    
CentOS-8 - AppStream - mirrors.aliy 924 kB/s | 8.1 MB     00:08    
.....

//安装ansible
[root@localhost ~]# yum -y install ansible
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Repository AppStream is listed more than once in the configuration
Extra Packages for Enterprise Linux 172 kB/s | 663 kB     00:03    
Extra Packages for Enterprise Linux 1.7 MB/s |  10 MB     00:05    
.......

已安装:
  ansible-2.9.23-1.el8.noarch                                       
  libsodium-1.0.18-2.el8.x86_64                                     
  python3-babel-2.5.1-5.el8.noarch                                  
  python3-bcrypt-3.1.6-2.el8.1.x86_64                               
  python3-jinja2-2.10.1-2.el8_0.noarch                              
  python3-jmespath-0.9.0-11.el8.noarch                              
  python3-markupsafe-0.23-19.el8.x86_64                             
  python3-paramiko-2.4.3-1.el8.noarch                               
  python3-pip-9.0.3-19.el8.noarch                                   
  python3-pyasn1-0.3.7-6.el8.noarch                                 
  python3-pynacl-1.3.0-5.el8.x86_64                                 
  python3-pytz-2017.2-9.el8.noarch                                  
  python3-pyyaml-3.12-12.el8.x86_64                                 
  python3-setuptools-39.2.0-5.el8.noarch                            
  python36-3.6.8-2.module_el8.4.0+790+083e3d81.x86_64               
  sshpass-1.06-9.el8.x86_64                                         

完毕!
[root@localhost ~]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

3.部署ansible

3.1构建ansible清单

[root@localhost ~]# cd /etc/ansible/ #进入到ansible配置文件目录中
[root@localhost ansible]# ls
ansible.cfg  hosts  roles
[root@localhost ansible]# 
[root@localhost ansible]# touch inventory
[root@localhost ansible]# ls
ansible.cfg  hosts  inventory  roles

//添加受管主机
[root@localhost ansible]# vim inventory
192.168.58.30
[webservers]
192.168.58.40

//使用命令列出默认清单文件中所有受管主机
[root@localhost ansible]# ansible all --list-hosts
  hosts (2):
    192.168.58.30
    192.168.58.40
    
//使用以下命令列出不属于任何组的受管主机
[root@localhost ansible]# ansible ungrouped --list-hosts
  hosts (1):
    192.168.58.40
    
//使用以下命令列出属于某组的受管主机
[root@localhost ansible]# ansible webservers --list-hosts
  hosts (1):
    192.168.58.30
    
//添加用户 密码
[root@localhost ansible]# vim inventory 
[webs]
192.168.58.30 ansible_user=root ansible_password=1

//ping 主机
[root@localhost ansible]# ansible all -m ping
192.168.58.30 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

// 做ssh 互信
[root@localhost ansible]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:ZX7EsseGIyy6NzFuxt2irfhyJc7lAy+ZqZ2bxSfP3U root@localhost.localdomain
The key's randomart image is:
+---[RSA 3072]----+
|      .=.+. o    |
|     .. O .= .   |
|      .+ +o B    |
|      ..*+.+ o   |
|      =+SO.oo    |
|     =. X B.. . E|
|    o .= = = . . |
|   . . .o . o    |
|       ..    .   |
+----[SHA256]-----+
[root@localhost ansible]# ssh-copy-id root@192.168.58.30
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/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.58.30's password: 

Number of key(s) added: 1

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

//删除用户和密码 ping主机
[root@localhost ansible]# vim inventory
[webs]
192.168.58.30
[root@localhost ansible]# ansible all -m ping
192.168.58.30 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

//移动清单文件ping主机需要指定移动到的目录
[root@localhost ansible]# mv inventory /opt/
[root@localhost ansible]# cd /opt/
[root@localhost opt]# ls
inventory
[root@localhost opt]# ansible all -m ping
[WARNING]: Unable to parse /etc/ansible/inventory as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all' // 这个时候ping不通需要指定
[root@localhost opt]# ansible all -i /opt/inventory -m ping
192.168.58.30 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}   // 指定后可以ping通

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/478611
推荐阅读
相关标签
  

闽ICP备14008679号