• Ansible自动化运维工具


    一.Ansible自动化运维工具介绍

    1.Ansible简介

    (1)Ansible是基于模块工作的,只是提供了一种运行框架,本身没有完成任务的能力,真正操作的是Anisble的模块。每个模块都是独立的、实现了批量系统配置、批量程序部署、批量运行命令等功能。

    (2)市场的工具有pubbet(ruby),saltstack(python)、chef、fabric都需要安装客户端。

    (3)ansible只用ssh就可以使用

    2.Ansible特点及优势

    (1)特点

    ①部署简单,只需要主控端部署Ansible环境,被控端无需做任何操作;

    ②默认使用SSH协议设备进行管理;

    ③主从集中化管理;

    ④配置简单、功能强大、扩展性强;

    ⑤支持API及自定义模块,可以通过Python轻松扩展

    ⑥通过playbooks来定制强大的配置、状态管理

    ⑦对云平台和大数据都有很好的支持

    (2)优点

    ①轻便性:无需在被控制服务器上安装客户端,Ansible基于ssh协议

    ②幂等性:大部分模块有幂等性,即如果输入systemctl stop firewalld当发现要停止的服务已经停止就不会做任何操作了,多次停止不会改变结果。systemtl restart是非幂等的。

    ③判断性:大部分模块在执行时都会判断目标节点是否要执行任务,所有重复执行某个任务大部分时间不会产生副作用

    ④简介性:一个窗口即可管理所有需要控制的机器,无需开启多个窗口

    3.Ansible核心程序

    ①HostInventory:记录由Ansible管理的主机信息,端口,ip,密码等

    ②playbooks:'剧本’YAML格式文件,多任务定义在一个文件中,定义主机需要调用那些模块完成功能

    ③core modeules:核心模块主要操作通过调用核心模块来完成管理任务

    ④customodules:自定义模块,完成核心模块无法完成的模块,支持多种语言编写

    ⑤connectiontugins:连接插件,ansible和主机通信使用(ssh协议)

    4.Ansible工作原理及流程

    在这里插入图片描述

    ①加载自己的配置文件,默认/etc/ansible/ansible.cfg

    ②查找对应的主机的配置文件,找到要执行的主机或组/etc/ansible/hosts文件

    ③加载自己对应的模块文件,如command、yum、ping、

    ④通过ansible将模块命令生成对应的临时py文件(类似python脚本),并将该文件传输至被管理端

    ⑤传输到在被控制端的对应用户的家目录下.ansible/tmp/xxx/xxx.py

    ⑥被控制端给传输过来的py文件加执行权限

    ⑦执行并返回结果,执行完成后删除py文件并sleep 0退出

    二.部署Ansible自动化运维工具

    管理IP地址安装软件
    管理端192.168.198.11ansible
    被管理端192.168.198.12
    被管理端192.168.198.13

    1.管理端安装 ansible(192.168.198.11)

    yum install  -y epel-release 
    #安装epel-release
    yum install -y  ansible
    #安装ansible
    #配置文件位置:/etc/ansible/ansible
    #hosts文件位置:/etc/ansible/hosts
    #公共角色目录:roles/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    vim /etc/ansible/hosts
    #编辑hosts文件添加被管理的机器,内容如下
    #配置组名
    [webservers]
    #组里包含的被管理的主机IP地址或主机名(主机名需要先修改/etc/hosts文件)
    192.168.198.12
    [dbservers]
    192.168.198.13
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    #配置密钥对验证
    #生成公钥,输入命令输入4个回车即可
    cd ~/.ssh
    ssh-copy-id -i   192.168.198.12
    ssh-copy-id -i   192.168.198.13
    #进入生成的公钥路径将公钥传输给备管理的服务器,传输需要输入每台备管理服务的root密码
    ansible ansible-doc  -l 
    #安装完毕,此命令可以查看有哪些ansible模块,按q退出
    ansible  webservers  -m command  -a 'pwd'
    #安装完成尝试管理webservers组输入pwd命令
    ansible  all   -a 'ls'
    #安装完成尝试管理所有hosts中主机输入ls命令,不指定模块默认为command模块
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述

    2.Ansible 命令行模块

    命令格式:ansible <组名> -m <模块> -a <参数列表>

    #列出所有已安装的模块,按q退出
    ansible-doc -l				
    
    • 1
    • 2

    在这里插入图片描述

    (1)command 模块

    在远程主机执行命令,不支持管道,重定向等shell的特性。

    #-s 列出指定模块的描述信息和操作动作,q退出
    ansible-doc -s command		
    
    • 1
    • 2
    #指定 ip 执行 date
    ansible 192.168.198.12 -m command -a 'date'	
    
    • 1
    • 2
    #指定组执行 date	
    ansible webservers -m command -a 'date'	
    
    • 1
    • 2
    #指定组执行 date			
    ansible dbservers -m command -a 'date'    
    
    • 1
    • 2
    #all 代表所有 hosts 主机   
    ansible all -m command -a 'date'
    
    • 1
    • 2
    #如省略 -m 模块,则默认运行 command 模块				
    ansible all -a 'ls /'	
    
    • 1
    • 2

    在这里插入图片描述
    在这里插入图片描述

    常用的参数:

    参数含义
    chdir在远程主机上运行命令前提前进入目录
    creates判断指定文件是否存在,如果存在,不执行后面的操作
    removes判断指定文件是否存在,如果存在,执行后面的操作
    ansible all -m command -a "chdir=/home  ls ./"
    
    • 1

    在这里插入图片描述

    (2)shell 模块

    在远程主机执行命令,相当于调用远程主机的shell进程,然后在该shell下打开一个子shell运行命令(支持管道符号等功能)

    ansible-doc -s shell
    
    • 1

    举例:

    #给test设置密码之前,查看dbservers模块下的被管控主机是否有test用户
    ansible dbservers -m shell -a 'echo 123456 | passwd --stdin test'
    
    • 1
    • 2

    在这里插入图片描述

    ansible dbservers -m shell -a 'echo $(ifconfig ens33 | awk "NR==2 {print $2}") | cut -d " " -f2'
    ansible dbservers -m shell -a 'echo $(ifconfig ens33 | awk "NR==2 {print \$2}")'
    
    • 1
    • 2

    在这里插入图片描述

    (3)cron 模块

    在远程主机定义任务计划。其中有两种状态(state):present表示添加(可以省略),absent表示移除。

    #cron模块,按 q 退出
    ansible-doc -s cron	
    
    • 1
    • 2

    常用参数:

    参数注释
    minute/hour/day/month/weekday分/时/日/月/周
    job任务计划要执行的命令
    name任务计划的名称

    举例:

    ansible webservers -m cron -a 'minute="*/1" job="/bin/echo helloworld" name="test crontab"'
    
    • 1
    ansible webservers -a 'crontab -l'
    ansible webservers -m cron -a 'name="test crontab" state=absent'		#移除计划任务,假如该计划任务没有取名字,name=None即可
    
    • 1
    • 2

    在这里插入图片描述

    (4)user 模块

    用户管理的模块

    ansible-doc -s user
    
    • 1

    常用的参数:

    参数注释
    name用户名,必选参数
    state=present|absent创建账号或者删除账号,present表示创建,absent表示删除
    system=yes|no是否为系统账号
    uid用户uid
    group用户基本组
    shell默认使用的shell
    move_home=yse|no如果设置的家目录已经存在,是否将已经存在的家目录进行移动
    password用户的密码,建议使用加密后的字符串
    comment用户的注释信息
    remove=yes|no当state=absent时,是否删除用户的家目录

    举例:

    #创建用户test01
    ansible dbservers -m user -a 'name="test01"'				
    
    • 1
    • 2

    在这里插入图片描述

    ansible dbservers -m command -a 'tail /etc/passwd'
    
    • 1

    在这里插入图片描述

    #删除用户test01
    ansible dbservers -m user -a 'name="test01" state=absent'	
    
    • 1
    • 2

    在这里插入图片描述

    (5)group 模块

    用户组管理的模块

    ansible-doc -s group
    
    • 1
    #创建mysql组
    ansible dbservers -m group -a 'name=mysql gid=306 system=yes'	
    
    • 1
    • 2
    ansible dbservers -a 'tail /etc/group'
    
    • 1

    在这里插入图片描述

    #将test01用户添加到mysql组中
    ansible dbservers -m user -a 'name=test01 uid=306 system=yes group=mysql'	
    
    • 1
    • 2
    ansible dbservers -a 'tail /etc/passwd'
    ansible dbservers -a 'id test01'  
    
    • 1
    • 2

    在这里插入图片描述

    在这里插入图片描述

    (6) copy 模块

    用于复制指定主机文件到远程主机的

    ansible-doc -s copy
    
    • 1

    常用的参数:
    在这里插入图片描述
    举例:

    #将etc/fstab下的复制到opt/fstab.bak给予root属主、权限640
    ansible dbservers -m copy -a 'src=/etc/fstab dest=/opt/fstab.bak owner=root mode=640'
    
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    #详细查看opt下的文件
    ansible dbservers -a 'ls -l /opt'
    
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    #查看新创建的文件内容
    ansible dbservers -a 'cat /opt/fstab.bak'
    
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    #将helloworld写入/opt/hello.txt文件中
    ansible dbservers -m copy -a 'content="helloworld" dest=/opt/hello.txt'
    
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    #查看写入的文件
    ansible dbservers -a 'cat /opt/hello.txt'
    
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    (7)file模块

    设置文件属性

    #查看file模块下的功能,按q退出
    ansible-doc -s file
    
    
    • 1
    • 2
    • 3

    举例:

    #修改文件的属主属组权限等
    ansible dbservers -m file -a 'owner=test01 group=mysql mode=644 path=/opt/fstab.bak'
    #查看修改后的信息
    ansible dbservers -m command -a 'chdir=/opt ls -lh ./'
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述
    在这里插入图片描述

    #设置/opt/fstab.link为/opt/fstab.bak的链接文件
    ansible dbservers -m file -a 'path=/opt/fstab.link src=/opt/fstab.bak state=link'
    #查看修改后的信息
    ansible dbservers -m command -a 'chdir=/opt ls -lh ./'
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述
    在这里插入图片描述

    #创建一个文件
    ansible dbservers -m file -a "path=/opt/abc.txt state=touch"
    #查看修改后的信息
    ansible dbservers -m command -a 'chdir=/opt ls -lh ./'
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    #删除一个文件
    ansible dbservers -m file -a "path=/opt/abc.txt state=absent"
    #查看删除后的信息
    ansible dbservers -m command -a 'chdir=/opt ls -lh ./'
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述
    在这里插入图片描述

    (8)hostname模块

    用于管理远程主机上的主机名

    ansible dbservers -m hostname -a "name=mysql01"
    
    • 1

    在这里插入图片描述

    (9)ping模块

    检测远程主机的连通性

    ansible all -m ping
    
    
    • 1
    • 2

    在这里插入图片描述

    (10)yum 模块

    在远程主机上安装与卸载软件包

    ansible-doc -s yum
    
    • 1

    常用参数:
    在这里插入图片描述
    举例:

    #安装hyyps服务
    ansible dbservers -m yum -a "name=httpd"
    
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    #查看安装服务的状态
    nsible dbservers -m command -a 'service httpd status'
    
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    #卸载httpd服务
    ansible dbservers -m yum -a "name=httpd state=absent"
    
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    #查看卸载的httpd服务
    ansible dbservers -m command -a 'service httpd status'
    
    
    • 1
    • 2
    • 3

    在这里插入图片描述
    在这里插入图片描述

    (11)service/system模块

    管理远程被控制主机上的管理服务的运行状态
    常用参数:
    在这里插入图片描述
    举例:

    #下载httpd服务
    ansible dbservers -m yum -a "name=httpd"
    #设置开机自启,服务的状态为开启
    ansible dbservers -m service -a 'enabled=yes  name=httpd state=started'
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    (12)script模块

    实现远程批量运行本地的shell脚本

    #查看模块下的功能
    ansible-doc -s script
    
    
    • 1
    • 2
    • 3
    ansible服务器:
    vim  /test.sh
    #编写/下的test.sh脚本内容如下
    #!/bin/bash
    echo  "this is test"
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    chmod +x /test.sh
    ansible webservers -m script -a "/test.sh"
    
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    (13)setup模块

    setup 模块可以获取这些信息 facts 组件收集d 被管理节点信息

    参数:filter 过滤可配合正则表达式。

    ansible webservers -m setup -a 'filter=*ipv4'
    
    
    • 1
    • 2

    在这里插入图片描述

    3.hostsinverntory主机清单

    hosts配置文件位置:/etc/ansible/hosts;

    Inventory支持对主机进行分组,每个组内可以定义多个主机,每个主机都可以定义在任何一个或多个主机组内

    (1)inventory 中的变量含义

    在这里插入图片描述

    (2)配置

    #新添加一台机器192.168.198.14
    #ansible主机上连接14机器,连接成功
    ssh 192.168.198.14
    
    
    • 1
    • 2
    • 3
    • 4
    #登出test4机器
    exit
    
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    #ansible主机配置
    #如果是名称类似的主机,可以使用列表的方式标识各个主机。
    vim /etc/ansible/hosts
    [webservers]
    192.168.198.12:22		#冒号后定义远程连接端口,默认是 ssh 的 22 端口
    192.168.146.1[2:3]
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    [dbservers]
    db-[a:f].example.org	#支持匹配 a~f
    
    
    • 1
    • 2
    • 3
    (1)主机变量
    [webservers]
    192.168.198.13 ansible_port=22 ansible_user=root ansible_password=000000
    ansible webservers -a 'ls -lh /home'
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    (2)组变量
    [webservers]
    192.168.198.13
    #表示为 webservers 组内所有主机定义变量
    [webservers:vars]			
    ansible_user=root
    ansible_password=000000
    
    [all:vars]					#表示为所有组内的所有主机定义变量
    ansible_port=22
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    ansible webservers -a 'ls -lh /home'
    
    
    • 1
    • 2

    在这里插入图片描述

    (3)组嵌套
    [nginx]
    192.168.198.12
    192.168.198.13
    192.168.198.14
    
    [apache]
    192.168.198.3[0:3]
    
    #表示为 webs 主机组中包含了 nginx 组和 apache 组内的所有主机
    [webs:children]		
    nginx
    apache
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    APP 备案公钥、签名 MD5、SHA-1、SHA-256获取方法。
    lv11 嵌入式开发 ARM指令集中(汇编指令集) 6
    一同走进Linux的“基操”世界
    如何使用 Hotshot 通过文字生成 GIF 动画
    yolov5 优化系列(三):修改损失函数
    动作捕捉技术在四足仿生机器人研究中的应用
    Ubuntu 20.04.05安装ceres-1.14.0
    ResultSet底层和Statement
    一幅长文细学JavaScript(二)——一幅长文系列
    回溯算法 | 子集问题(递增子序列) | leecode刷题笔记
  • 原文地址:https://blog.csdn.net/Zhang110_/article/details/132583193