【Ansible】01

自动化运维

Ansible

  • Ansible首发于2012年 , 2015年被收购

  • Ansible是一款自动化运维工具 , 基于 Python 开发

  • 基于ssh 进行管理 , 不需要在被管理端安装任何软件

  • Ansible主要是通过各种模块进行操作

    • 批量系统配置

    • 批量程序部署

    • 批量运行命令等功能

环境准备

  • 控制节点主机 : 192.168.88.240
  • 被控制节点主机 : 192.168.88.{11,12,13}
配置网络yum源

/var/ftp/dvd 挂载

创建 /var/ftp/rpms 放新包

# 在192.168.88.240上配置yum源

# 将/linux-soft/s2/目录下的ansible目录中rpm包拷贝到yum服务器的rpms目录

[root@myhost ~]# scp /linux-soft/s2/zzg/ansible_soft/* 192.168.88.240:/var/ftp/rpms

# 更新yum服务器仓库源

[root@pubserver ~]# createrepo /var/ftp/rpms/


# 4台主机的yum全为以下配置

[root@pubserver ~]# vim /etc/yum.repos.d/local.repo

[BaseOS]
name = BaseOS
baseurl = ftp://192.168.88.240/dvd/BaseOS
enabled = 1
gpgcheck = 0
[AppStream]
name = AppStream
baseurl = ftp://192.168.88.240/dvd/AppStream
enabled = 1
gpgcheck = 0
[rpms]
name = rpms
baseurl = ftp://192.168.88.240/rpms
enabled = 1
gpgcheck = 0
安装 Ansible
yum install -y ansible
  • 配置名称解析 , 能够通过名字访问所有节点

  • 配置可以通过ssh到所有节点免密登录

    配置名称解析

    [root@pubserver ~]# echo -e "192.168.88.240\tpubserver" >> /etc/hosts

    [root@pubserver ~]# for i in 1 2

    do
    echo -e "192.168.88.1i\twebi" >> /etc/hosts
    done

    [root@pubserver ~]# echo -e "192.168.88.13\tdb1" >> /etc/hosts

    [root@pubserver ~]# tail -4 /etc/hosts

    192.168.88.240 pubserver
    192.168.88.11 web1
    192.168.88.12 web2
    192.168.88.13 db1

    配置免密登陆

    [root@pubserver ~]# ssh-keygen # 三个问题都直接回车,使用默认值

    执行以下命令时,可能需要回答yes和远程用户的密码,请按提示操作

    [root@pubserver ~]# for i in web{1..2} db1

    do
    ssh-copy-id $i
    done

配置Ansible管理环境
  • 使用一套ansible软件 , 可能需要管理多种环境.

  • 通过创建不同的工作目录 , 来实现响应的管理需求

  • 将某一环境下的主机写入到对应工作目录的主机清单文件

  • 进入对应的工作目录执行管理任务 , 就可以管理相应环境的主机

    创建ansible工作目录,目录名自己定义,不是固定的。

    [root@pubserver ~]# mkdir ansible
    [root@pubserver ~]# cd ansible
    [root@pubserver ansible]# touch ansible.cfg inventory

    创建配置文件。默认的配置文件是/etc/ansible/ansible.cfg,但是一般不使用它,而是在工作目录下创建自己的配置文件

    [root@pubserver ansible]# vim ansible.cfg #文件名必须是ansible.cfg
    [defaults]
    host_key_checking = false # 不检查主机密钥,=号两边空格可有可无。
    inventory = inventory # 定义主机清单文件为当前目录的inventory

    创建主机清单文件。写在[]里的是组名,[]下面的是组内的主机名

    [root@pubserver ansible]# vim inventory
    [webservers]
    web[1:2] # web1和web2的简化写法,表示从1到2

    [dbs]
    db1

    cluster是组名,自定义的;:children是固定写法,表示下面的组名是cluster的子组。

    [cluster:children]
    webservers
    dbs

    查看被管理的所有的主机。注意,一定在工作目录下执行命令。

    [root@pubserver ansible]# ansible all --list-hosts
    hosts (3):
    web1
    web2
    db1

    查看webservers组中所有的主机

    [root@pubserver ansible]# ansible webservers --list-hosts
    hosts (2):
    web1
    web2

Ansible 管理( adhoc )

  • ansible 进行远程管理的方法有两个
    • adhoc临时命令
      • 在命令行上执行管理命令
    • playbook剧本
      • 把管理任务用特定格式写到文件中
  • 无论哪种方式 , 都是通过模块加参数进行管理
adhoc 临时命令
1. ansible 语法

ansible 主机或组列表 -m 模块 -a "参数" # -a 是可选的

例:

所有输出hello ansible all -a "echo hello"

通过ping模块测试到远程主机的连通性 ansible all -m ping

#该命令检查是否可以管理远程主机。如果结果为绿色的SUCCESS,则表示成功。

#如果是红色的UNREACHABLE,则检查网络是否可通,是否已经可以免密登陆

2. ansible 模块
  • 在ansible中 , 通过模块来完成某一特定任务

  • 模块的使用方式都一样 , 主要是查看该模块的参数

1) 查看模块
# 列出全部可用模块,按空格键向下翻页,按q退出
[root@pubserver ansible]# ansible-doc -l

# 列出模块数量
[root@pubserver ansible]# ansible-doc -l | wc -l  # 如有WARNING提示,可忽略
7214

# 查看包含yum的模块名
[root@pubserver ansible]# ansible-doc -l | grep yum

# 查看yum模块的帮助文档,按空格键向下翻页,按q退出
[root@pubserver ansible]# ansible-doc yum
2) command 模块
  • ansible 默认模块 , 用于在远程主机上执行任意命令

  • command 不支持shell特性 , 如管道 , 重定向

    在所有被管主机上创建目录/tmp/demo

    [root@pubserver ansible]# ansible all -a "mkdir /tmp/demo"

    查看we1的ip地址

    [root@pubserver ansible]# ansible web1 -a "ip a s"
    [root@pubserver ansible]# ansible web1 -m command -a "ip a s"
    [root@pubserver ansible]# ansible web1 -a "ip a s | head" # 报错

3) shell模块
  • 与 command 模块类似 , 但支持 shell 特性 , 如管道 , 重定向

    查看web1的ip地址,只显示前10行

    [root@pubserver ansible]# ansible web1 -m shell -a "ip a s | head"

4) script 模块
  • 用于在远程主机上执行脚本

    在控制端创建脚本即可

    [root@pubserver ansible]# vim test.sh
    #!/bin/bash
    for user in user{1..5}
    do
    useradd $user
    echo '123456' | passwd --stdin $user
    done

    在webservers组的主机上执行脚本

    [root@pubserver ansible]# ansible webservers -m script -a "test.sh"

5) file 模块
  • 可以创建文件 , 目录 , 链接 等 , 还可以修改权限 , 属性 等
  • 选项
    • path # 指定文件路径

    • owner # 设置文件所有者

    • group # 设置文件所属组

    • state # 状态

      • touch # 创建文件
      • directory # 表示创建目录
      • link # 创建软连接
      • hard # 创建硬连接
      • absent # 表示删除
    • mode # 设置权限

    • src # source的简写 , 源

    • dest # destination的简写 , 目标

      查看使用帮助

      [root@pubserver ansible]# ansible-doc file
      ... ...
      EXAMPLES:

      • name: Change file ownership, group and permissions # 忽略
        ansible.builtin.file: # 模块名。以下是它的各种参数
        path: /etc/foo.conf # 要修改的文件的路径
        owner: foo # 文件所有者
        group: foo # 文件的所有组
        mode: '0644' # 权限
        ... ...

      根据上面的example,-m file -a的内容就是doc中把各参数的冒号换成=号

      在webservers组的主机上创建/tmp/file.txt

      [root@pubserver ansible]# ansible webservers -m file -a "path=/tmp/file.txt state=touch"

      touch是指如果文件不存在,则创建;如果存在则改变它的时间戳

      在webservers组的主机上创建/tmp/demo目录

      [root@pubserver ansible]# ansible webservers -m file -a "path=/tmp/demo state=directory"

      将webservers组的主机上/tmp/file.txt的属主改为sshd,属组改为adm,权限改为0777

      [root@pubserver ansible]# ansible webservers -m file -a "path=/tmp/file.txt owner=sshd group=adm mode='0777'"
      [root@pubserver ansible]# ansible webservers -a "ls -l /tmp/file.txt"

      删除webservers组的主机上/tmp/file.txt

      [root@pubserver ansible]# ansible webservers -m file -a "path=/tmp/file.txt state=absent" # absent英文缺席的、不存在的

      删除webservers组的主机上/tmp/demo

      [root@pubserver ansible]# ansible webservers -m file -a "path=/tmp/demo state=absent"

      在webservers组的主机上创建/etc/hosts的软链接,目标是/tmp/hosts.txt

      [root@pubserver ansible]# ansible webservers -m file -a "src=/etc/hosts dest=/tmp/hosts.txt state=link"

6) copy 模块
  • 控制端 ---------------> 被控制端
  • 用于将文件从控制端拷贝到被控制端
  • 上传
  • 选项
    • src # 源 , 控制端的文件路径

    • dest # 目标 , 被控制端的文件路径

    • contest # 内容 , 需要写到文件中的内容

      [root@pubserver ansible]# echo "AAA" > a3.txt

      将a3.txt拷贝到webservers主机的/root/

      [root@pubserver ansible]# ansible webservers -m copy -a "src=a3.txt dest=/root/"

      在目标主机上创建/tmp/mytest.txt,内容是Hello World

      [root@pubserver ansible]# ansible webservers -m copy -a "content='Hello World' dest=/tmp/mytest.txt"

7) fetch 模块
  • 被控制端 -------------> 控制端
  • 下载
  • 选项
    • src # 源 , 被控制端文件路径

    • dest # 目标 , 控制端的文件路径

      将webservers主机上的/etc/hostname下载到本地用户的家目录下

      [root@pubserver ansible]# ansible webservers -m fetch -a "src=/etc/hostname dest=~/"

      [root@pubserver ansible]# ls /root/web1/etc/
      hostname
      [root@pubserver ansible]# ls /root/web2/etc/
      hostname

8) lineinfile 模块
  • 用于确保目标文件中有某一行内容
  • 选项
    • path # 待修改的文件路径

    • line # 写入文件的一行内容

    • regexp # 正则表达 , 用于查找文件中的内容

      webservers组中的主机,/etc/issue中一定要有一行Hello World。如果该行不存在,则默认添加到文件结尾

      [root@pubserver ansible]# ansible webservers -m lineinfile -a "path=/etc/issue line='Hello World'"

      webservers组中的主机,把/etc/issue中有Hello的行,替换成chi le ma

      [root@pubserver ansible]# ansible webservers -m lineinfile -a "path=/etc/issue line='chi le ma' regexp='Hello'"

      替换整行

9) replace 模块
  • replace替换单词
  • 选项
    • path # 文件路径

    • replace # 将查找到的内容替换

    • regexp # 正则 , 查找文件中的内容

      把webservers组中主机上/etc/issue文件中的chi,替换成he

      [root@pubserver ansible]# ansible webservers -m replace -a "path=/etc/issue regexp='chi' replace='he'"

10) user模块
  • 实现Linux用户管理
  • 选项
    • name # 待创建用户名

    • uid # 用户ID

    • group # 设置主组

    • groups # 设置附加组

    • home # 设置家目录

    • password # 设置用户密码

    • state #状态

      • present # 创建 , 默认选项
      • absent # 删除
    • remove # 删除家目录, 邮箱等. 值为 yes 或 true 都可以

      在webservers组中的主机上,创建zhangsan用户

      [root@pubserver ansible]# ansible webservers -m user -a "name=zhangsan"

      在webservers组中的主机上,创建lisi用户。设置其uid为1010,主组是adm,附加组是daemon和root,家目录是/home/lisi

      [root@pubserver ansible]# ansible webservers -m user -a "name=lisi uid=1010 group=adm groups=daemon,root home=/home/lisi"

      设置zhangsan的密码是123456

      {{}}是固定格式,表示执行命令。password_hash是函数,sha512是加密算法,则password_hash函数将会把123456通过sha512加密变成zhangsan的密码

      [root@pubserver ansible]# ansible webservers -m user -a "name=zhangsan password={{'123456'|password_hash('sha512')}}"

      删除zhangsan用户,不删除家目录

      [root@pubserver ansible]# ansible webservers -m user -a "name=zhangsan state=absent"

      删除lisi用户,同时删除家目录

      [root@pubserver ansible]# ansible webservers -m user -a "name=lisi state=absent remove=yes"

11) group 模块
  • 创建 , 删除组
  • 选项
    • name # 待创建的组名

    • gid # 组的ID号

    • state # 状态

      • present # 创建 , 默认选项
      • absent # 删除

      在webservers组中的主机上创建名为devops的组

      [root@pubserver ansible]# ansible webservers -m group -a "name=devops"

      在webservers组中的主机上删除名为devops的组

      [root@pubserver ansible]# ansible webservers -m group -a "name=devops state=absent"

12) yum_repository
  • 用于配置yum

  • 选项

    • file # 指定文件名

    • name # yum内配置名

    • description #name=

    • baseurl # baseurl

    • gpgcheck # yes/no

    • enabled #yes

      [root@pubserver ansible]# ansible webservers -m yum_repository -a "file=myrepo
      name=myApp
      description='My App'
      baseurl=ftp://192.168.88.240/dvd/AppStream
      gpgcheck=no
      enabled=yes"

      [root@web1 ~]# cat /etc/yum.repos.d/myrepo.repo
      [myApp]
      async = 1
      baseurl = ftp://192.168.88.240/dvd/AppStream
      enabled = 1
      gpgcheck = 0
      name = My App

13) yum模块
  • 用于rpm软件包的管理 , 安装 , 升级 , 卸载
  • 选项
    • name # 包名

    • state # 状态

      • present # 安装
      • latest # 安装或升级到最新版本
      • absent # 卸载

      在webservers组中的主机上安装tar

      [root@pubserver ansible]# ansible webservers -m yum -a "name=tar state=present"

      在webservers组中的主机上安装wget、net-tools

      [root@pubserver ansible]# ansible webservers -m yum -a "name=wget,net-tools"

      在webservers组中的主机上卸载wget

      [root@pubserver ansible]# ansible webservers -m yum -a "name=wget state=absent"

14) service 模块
  • 用于控制服务.启动 , 关闭 , 重启 , 开机自启
  • 常用选项:
    • name # 控制的服务名

    • state # 状态

      • started # 启动
      • stopped # 关闭
      • restarted # 重启
    • enabled # yes 开机自启 , no 开机不自启

      在test主机上安装nginx

      [root@pubserver ansible]# ansible webservers -m yum -a "name=nginx state=latest"

      在test主机上启动nginx,并设置它开机自启

      [root@pubserver ansible]# ansible webservers -m service -a "name=nginx state=started enabled=yes"

15) lvg模块
  • 创建 , 删除卷组 , 修改卷组大小
  • 选项
    • vg # 定义卷组名

    • pvs # 由哪些物理卷构成

      在web1上安装lvm2,state不写,默认是present

      [root@pubserver ansible]# ansible web1 -m yum -a "name=lvm2"

      手工在web1上对vdb进行分区

      [root@web1 ~]# fdisk /dev/vdb
      Command (m for help): g # 创建GPT分区表
      Command (m for help): n # 新建分区
      Partition number (1-128, default 1): # 回车,使用1号分区
      First sector (2048-41943006, default 2048): # 起始位置,回车
      Last sector, +sectors or +size{K,M,G,T,P} (2048-41943006, default 41943006): +5G # 结束位置+5G
      Command (m for help): n # 新建分区
      Partition number (2-128, default 2): # 回车,使用2号分区
      First sector (10487808-41943006, default 10487808): # 起始位置,回车
      Last sector, +sectors or +size{K,M,G,T,P} (10487808-41943006, default 41943006): # 结束位置,回车,分区到结尾
      Command (m for help): w # 存盘

      [root@web1 ~]# lsblk # vdb被分出来了两个分区
      NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
      sr0 11:0 1 1024M 0 rom
      vda 253:0 0 30G 0 disk
      -vda1 253:1 0 20G 0 part / vdb 253:16 0 20G 0 disk |-vdb1 253:17 0 5G 0 part -vdb2 253:18 0 15G 0 part
      vdc 253:32 0 20G 0 disk

      在web1上创建名为myvg的卷组,该卷组由/dev/vdb1组成

      [root@pubserver ansible]# ansible web1 -m lvg -a "vg=myvg pvs=/dev/vdb1"

      在web1上查看卷组

      [root@web1 ~]# vgs
      VG #PV #LV #SN Attr VSize VFree
      myvg 1 0 0 wz--n- <5.00g <5.00g

      扩容卷组。卷组由PV构成,只要向卷组中加入新的PV,即可实现扩容

      [root@pubserver ansible]# ansible web1 -m lvg -a "vg=myvg pvs=/dev/vdb1,/dev/vdb2"

      [root@web1 ~]# vgs # 在web1上查看卷组
      VG #PV #LV #SN Attr VSize VFree
      myvg 2 0 0 wz--n- 19.99g 19.99g

16) lvol 模块
  • 创建 , 删除逻辑卷 , 修改逻辑卷大小
  • 选项
    • vg # 指定在哪个卷组上创建逻辑卷

    • lv # 创建的逻辑卷名

    • size # 逻辑卷大小 , 不写单位 , 以M为单位

      在web1上创建名为mylv的逻辑卷,大小为2GB

      [root@pubserver ansible]# ansible web1 -m lvol -a "vg=myvg lv=mylv size=2G"

      在web1上查看逻辑卷

      [root@web1 ~]# lvs
      LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
      mylv myvg -wi-a----- 2.00g

      mylv扩容至4GB

      [root@pubserver ansible]# ansible web1 -m lvol -a "vg=myvg lv=mylv size=4G"

      [root@web1 ~]# lvs # 在web1上查看逻辑卷
      LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
      mylv myvg -wi-a----- 4.00g

17) filesystem模块
  • 格式化 , 创建文件系统
  • 选项
    • fstype # 指定文件系统类型

    • dev # 指定要格式化的设备 , 可以是分区 , 可以是逻辑卷

      在web1上,把/dev/myvg/mylv格式化为xfs

      [root@pubserver ansible]# ansible web1 -m filesystem -a "fstype=xfs dev=/dev/myvg/mylv"

      在web1上查看格式化结果

      [root@web1 ~]# blkid /dev/myvg/mylv
      /dev/myvg/mylv: UUID="46c0af72-e517-4b15-9e53-ec72fbe1d96e" TYPE="xfs"

18) mount 模块
  • 用于挂载文件系统

  • 选项

    • path # 挂载点 , 可自动创建

    • src # 待挂载的设备

    • fstype # 文件系统类型

    • state # 状态

      • mounted # 表永久挂载

      在web1上,把/dev/myvg/mylv永久挂载到/data

      [root@pubserver ansible]# ansible web1 -m mount -a "path=/data src=/dev/myvg/mylv state=mounted fstype=xfs"

      在web1上查看

      [root@web1 ~]# tail -1 /etc/fstab
      /dev/myvg/mylv /data xfs defaults 0 0
      [root@web1 ~]# df -h /data/
      Filesystem Size Used Avail Use% Mounted on
      /dev/mapper/myvg-mylv 4.0G 61M 4.0G 2% /data

      在web1上,卸载/dev/myvg/mylv

      [root@pubserver ansible]# ansible web1 -m mount -a "path=/data state=absent"

      在web1上,强制删除/dev/myvg/mylv

      [root@pubserver ansible]# ansible web1 -m lvol -a "lv=mylv state=absent vg=myvg force=yes" # force是强制

      在web1上,删除myvg卷组

      [root@pubserver ansible]# ansible web1 -m lvg -a "vg=myvg state=absent"

相关推荐
honey ball11 分钟前
仪表放大器AD620
运维·单片机·嵌入式硬件·物联网·学习
秋已杰爱14 分钟前
进程间关系与进程守护
运维·服务器
Flying_Fish_roe30 分钟前
linux-软件包管理-包管理工具(Debian 系)
linux·运维·debian
BLEACH-heiqiyihu1 小时前
红帽9中nginx-源码编译php
运维·nginx·php
大广-全栈开发1 小时前
centos 7 安装gitlab
linux·git·centos
666786661 小时前
Mysql高级篇(中)—— SQL优化
linux·运维·服务器·数据库·sql·mysql
企业管理8MSaaS1 小时前
了解CRM销售自动化:类型、优势、策略和工具
运维·自动化
创小董2 小时前
智能机巢+无人机:自动化巡检技术详解
运维·自动化·无人机
henanxiaoman2 小时前
SaltStack自动化运维部署
运维·自动化·saltstack
悲伤的创可贴2 小时前
Docker安装以及简单使用
linux·docker·centos