自动化运维工具——Ansible介绍

一、ansible的介绍

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

特性

  • 被管理端无需安装代理服务,只要配置满足条件的Python版本和SSH服务
  • 不需要服务端,只需要安装ansible软件,配置完成后,直接运行命令
  • 可以基于任何语言开发新模块
  • 由于被控端没有安装代理服务,只能通过命令端推送任务
  • 模块是幂等性,定义的任务已存在则不会做任何事情,意味着同一台服务器上多次执行同一个playbook和执行一次,效果一样

二、ansible架构图

在这里插入图片描述

组件功能
connection plugins连接插件,负责和被监控端实现通信
host inventory主机库,是一个配置文件里面定义监控的主机
modulesansible自身核心模块、command模块、自定义模块
Plugins借助于插件完成记录日志邮件等功能
playbooks剧本执行多个任务时,非必需可以让节点一次性运行多个任务

Ansible方式
ansible的设计宗旨是工具易用,自动化易写易读,所以在创建自动化时我们应追求简单化

ansible自动化语言围绕简单易读的声明性文本文件来构建。正确编写的ansible playbook可以清楚地记录你的工作自动化

ansible是一种要求状态引擎。它通过表达你所希望系统处于何种状态爱解决如何自动化IT部署的问题。Ansible的目标是通过仅执行必要的更改,使系统处于所需的状态。

三、安装ansible

控制节点

Ansible易于安装。ansible软件只需要安装到需要运行它的一个(或多个)控制节点。由ansible管理的主机不需要安装ansible。

对于控制节点的要求:

  • 控制节点应是linux或UNIX系统。不支持Windows用作控制节点,但Windows系统可以是受管主机
  • 控制节点选装Python3(版本3.5或以上)

受管主机
Ansible的一大优点是受管主机不需要安装特殊代理。Ansible控制节点使用标准的网络协议连接受管主机,从而确保系统处于指定的状态

#  安装epel源
[root@localhost ~]# dnf -y install epel-release
Updating Subscription Management repositories.
Unable to read consumer identity
##过程略
已安装:
  epel-release-8-11.el8.noarch                                                                           

完毕!

#  安装ansible
[root@localhost ~]# dnf -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.
##过程略
已安装:
  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-pyasn1-0.3.7-6.el8.noarch                   python3-pynacl-1.3.0-5.el8.x86_64                 
  sshpass-1.06-9.el8.x86_64                          

完毕!

[root@localhost ~]# ansible --version
ansible 2.9.23
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Dec  5 2019, 15:45:45) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]

#  通过set模块验证localhost上的ansible_python_version
[root@localhost ~]# ansible --version
ansible 2.9.23
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Dec  5 2019, 15:45:45) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
[root@localhost ~]# ansible -m setup localhost | grep ansible_python_version
        "ansible_python_version": "3.6.8",

四、ansible的简单使用

通常将受管主机组织为主机组,通过主机组对一系列运行Ansible。开头以括号括起来的为主机组名称

定义清单

例: [webserver]
     192.168.8.128

     [dbserver]
     10.10.10.10

构建清单

[root@client ansible]# cat inventory 
192.168.8.125

[webserver]
192.168.8.128

[dbserver]
192.168.8.130

当清单中有相同名称的主机和主机组时,ansible命令将以主机为目标,主机组则被忽略

验证清单

[root@client ansible]# ansible 192.168.8.128 --list-hosts
  hosts (1):
    192.168.8.128
   
#  列出属于webserver组的清单 
[root@client ansible]# ansible webserver --list-hosts
  hosts (1):
    192.168.8.128
    
#  列出所有清单
[root@client ansible]# ansible all --list-hosts
  hosts (2):
    192.168.8.128
    192.168.8.130
    
#  列出不属于任何组的清单
[root@client ansible]# ansible ungrouped --list-hosts
  hosts (1):
    192.168.8.125
    
#  控制受管主机
[root@client ansible]# cat inventory 
192.168.8.125

[webserver]
192.168.8.128 ansible_user=root ansible_password=1  ##添加用户名和密码

[dbserver]
192.168.8.130

[root@client ansible]# ansible webserver -m ping
192.168.8.128 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}


# 免密登录
[root@client 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:jPp9aCQTTDuYMTB88I/PAe7PRXDA2xEBFl0/r/OIMT0 root@client
The key's randomart image is:
+---[RSA 3072]----+
| .+o .=+o+.      |
|  .o+.o.o  .     |
|   .oB.+..  o    |
|   .o+*=.    o   |
|    o +oS     .  |
|   . +oo.  . .   |
|    o o+..o E    |
|     + oo .+ =   |
|      +.... . .  |
+----[SHA256]-----+

[root@client ansible]# ssh-copy-id root@192.168.8.128
/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.8.128's password: 
Number of key(s) added: 1
Now try logging into the machine, with:   "ssh 'root@192.168.8.128'"
and check to make sure that only the key(s) you wanted were added.

[root@client ansible]# cat inventory
[webserver]
192.168.8.128
 
[root@client ansible]# ansible webserver -m ping
192.168.8.128 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值