基于ansible进行运维自动化的研究以及相关的属性

一、ansible-简介

介绍

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

  • 一些Ansible的安装和一些基本概念,然后我们会开始研究一些真正有意思的东西 – playbook,配置管理,部署以及语法编排。

通俗地说,就好比是一种可以帮助我们自动完成重复性、繁琐的工作的工具,类似于一个自动化的管家。
举个例子,假设你是一家公司的运维工程师,每天需要登录到数十台服务器上,更新软件包、配置文件、重启服务等操作。使用Ansible,你可以编写一些自动化脚本(称为playbook),然后让Ansible去执行这些脚本,帮助你快速、高效地完成这些任务,而无需手动操作每一台服务器。
总之,Ansible可以帮助简化系统管理工作,提高工作效率,减少人为失误,让运维工作更加自动化、高效。

请添加图片描述

命令执行过程
  • 1、加载自己(ansible服务器)的配置文件,默认/etc/ansible/ansible.cfg
  • 2、查找对应的主机配置文件,找到要执行的主机或者组。
  • 3、加载自己对应的模块文件,如command
  • 4、通过ansible将模块或命令生成对应的临时py文件,并将该文件传输至远程服务器
  • 5、对应执行用户家目录的.ansible/tmp/XXX/XXX.PY文件
  • 6、给文件+x执行
  • 7、执行并返回结果
  • 8、删除临时py文件,sleep 0 退出。

工作原理
请添加图片描述

二、install-部署

1.dns resolve

  • 环境

    ansible服务器

    • 192.168.145.145

    ansible客户机

    • 192.168.145.141
    • 192.168.145.142
    • 192.168.145.143
    • 192.168.145.144
  • ansible服务器

    域名解析

    [ansible-server]#vim /etc/hosts

    192.168.145.145 ansible

    192.168.145.141 host1

    192.168.145.142 host2

    192.168.145.143 host3

    192.168.145.144 host4

  • ansible客户机

    • 无需配置
      • IP
      • YUM源

2.install ansible

ansible服务器

  • yum install -y epel-release

    • 安装epel源,如果您在非学校环境,请使用下方阿里YUM
    • rm -rf /etc/yum.repos.d/*
    • wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    • wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  • yum install -y ansible

    • 检测部署是否完成

      • ansible --version

        查看ansible版本

      • rpm -ql ansible

        列出所有文件

      • rpm -qc ansible

        查看配置文件

      • ansible --help

        查看ansible帮助

      • ansible-doc -l

        看所有模块(A10,华为,docker,EC2,aws等等广大厂商设备)

      • ansible-doc -s yum

        看yum模块,了解其功能

        • install (present' or installed’, latest'), or remove (absent’ or `removed’)
        • yum list
        • Package name
        • enablerepo

3.ssh-key(可选)

  • 免密码ssh-key的方式。

  • ssh-keygen

  • ssh-copy-id IP地址

    • 推送公钥
  • 尝试免密登录

    • ssh root@ip地址

    例如:

    # ssh-keygen
    # ssh-copy-id host1
    # ssh root@host1
    连接成功后 注意退出远程连接
    # exit
    

4.ansible基础

  • 1.定义主机清单

    • vim /etc/ansible/hosts

    • host1

      host2

      host3

      • 注意此处少一个主机。请留意
  • 2.测试连通性

    • ansible localhost -m ping
      • 测试host1连通性
      • -m 指定模块。什么功能 ping只是其中一个模块。还有shell,yum等等
  • 3.简洁输出

    • ansible host1 -m ping -o
  • 4.know_hosts

    • ansible host2 -m ping
      • 失败了
    • ansible host2 -m ping -u root -k -o
      • 增加用户名选项
      • 增加密码选项
      • -k 表示连接的时候需要输入密码
    • 去掉(yes/no)的询问
      • vim /etc/ssh/ssh_config
      • StrictHostKeyChecking no
      • systemctl restart sshd
    • ansible host2 -m ping -u root -k -o
      • 成功不提示
  • 5.错误示范

    • ansible host4 -m ping -u root -k -o
      • 失败,主机清单未标注主机。
      • 若想要连接,在ansible服务器的/etc/ansible/hosts中添加host4
  • 6.请注意ping和ssh

    • ping
      • ICMP:网际消息管理协议
    • 关闭host1主机的sshd进程,进行ping连通性测试。
    • 再使用ansible对host1进行联通测试时,却是失败的。
    • 结论ansible的ping,是探测ssh程序是否连接。不是icmp协议
    • ansible host1 -m ping -u root -k

5.Inventory -主机清单

  • 含义

    • 清查;存货清单;财产目录;
    • 主机清单,就是ansible将要操作的服务器的集合
  • 1 增加主机组

    • 官方链接

      • http://docs.ansible.com/ansible/intro_inventory.html#
    • vim /etc/ansible/hosts

      [webserver]
      host1
      host2
      host3
      host4
      
    • ansible webserver -m ping -o

      • 输出提示

        [root@localhost ~]# ansible webserver -m ping -u root -k -o
        SSH password: 
        host3 | SUCCESS => {"changed": false, "ping": "pong"}
        host1 | SUCCESS => {"changed": false, "ping": "pong"}
        host4 | SUCCESS => {"changed": false, "ping": "pong"}
        host2 | SUCCESS => {"changed": false, "ping": "pong"}
        
        
  • 2 增加用户名 密码

    • vim /etc/ansible/hosts

      [webserver]
      host1 ansible_ssh_user='root' ansible_ssh_pass='root'
      host2 ansible_ssh_user='root' ansible_ssh_pass='root'
      host3 ansible_ssh_user='root' ansible_ssh_pass='root'
      host4 ansible_ssh_user='root' ansible_ssh_pass='root'
      

      注释:

      ansible_ssh_user ansible连接的用户名

      ansible_ssh_pass ansible连接的密码

      快捷写法:

      [webserver]
      host[1:4] ansible_ssh_user='root' ansible_ssh_pass='root'
      

      注意你的密码与课件中的是否相同,用你自己的密码。

    • # ansible webservers -m ping -o

      • 免用户名和密码成功
    • 请思考主机和主机的用户名密码不同。如何设置?

      [webservers]
      host1 ansible_ssh_user='root' ansible_ssh_pass='777777'
      host[2:4] ansible_ssh_user='root' ansible_ssh_pass='666666'
      
  • 3 增加端口

    • 请将host1的sshd程序端口修改为2222

      # vim /etc/ssh/sshd_config
        Port 2222
      # systemctl restart sshd
      
    • ansible webservers -m ping -o

      • 失败,因为默认端口已更改
    • vim /etc/ansible/hosts

      [webserver]
      host1 ansible_ssh_user='root' ansible_ssh_pass='777777' ansible_ssh_port='2222'
      host[2:4] ansible_ssh_user='root' ansible_ssh_pass='666666'
      
    • 请将用户名密码和端口回复原状

  • 4 组:变量

    • ansible内部变量可以帮助我们简化主机清单的设置

    • vim /etc/ansible/hosts

      [webserver]
      host1
      host2
      host3
      host4
      [webserver:vars]
      ansible_ssh_user='root'
      ansible_ssh_pass='root'
      

      或者:

      [webserver]
      host[1:4]
      [webserver:vars]
      ansible_ssh_user='root'
      ansible_ssh_pass='root'
      

      实验:

      # ansible host2 -m ping
      host2 | SUCCESS => {
          "ansible_facts": {
              "discovered_interpreter_python": "/usr/bin/python3"
          },
          "changed": false,
          "ping": "pong"
      }
      
      
    • 常用变量

请添加图片描述

  • 5 子分组

    • 将不同的分组进行组合

    • vim /etc/ansible/hosts

      [apache]
      host[1:2]
      [nginx]
      host[3:4]
      
      [webserver:children]
      apache
      nginx
      [webserver:vars]
      ansible_ssh_user='root'
      ansible_ssh_pass='root'
      

6 自定义主机列表

  • vim hostlist

    [dockers]
    host1
    host2
    [dockers:vars]
    ansible_ssh_user=‘root’
    ansible_ssh_pass=‘root’

    注意您的计算机密码

  • ansible -i hostlist dockers -m ping -o

  • 综合例子:

    [root@localhost ~]# mv /etc/ansible/hosts /root/
    [root@localhost ~]# ls
    anaconda-ks.cfg  hosts
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]# cat /etc/ansible/hosts
    cat: /etc/ansible/hosts: 没有那个文件或目录
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]# ansible webserver -m ping
    [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'
    [WARNING]: Could not match supplied host pattern, ignoring: webserver
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]# ansible -i ./hosts  webserver -m ping
    
    

    恢复ansible/hosts

# mv ./hosts /etc/ansible/

6.Ad-Hoc-点对点模式

  • 简介

    临时的,在ansible中是指需要快速执行的单条命令,并且不需要保存的命令。对于复杂的命令则为 playbook。

    ansible在日常管理时所用的内部指令,比如创建用户、安装软件、启动服务等。此处为我们接下来第七部分做准备,这里学习的就是接下来编写剧本时所用的参数

  • 1.shell模块

    • 帮助

      ansible-doc shell

    • ansible webserver -m shell -a ‘hostname’ -o

      获取主机名

    • ansible webserver -m shell -a ‘hostname’ -o -f 2

      注释:

    • -f 2 指定线程数

      • f FORKS, --forks=FORKS Ansible一次命令执行并发的线程数。NUM被指定为一个整数,默认是5 specify number of parallel processes to use (default=5)

      • ansible host2 -m shell -a ‘yum -y install httpd’ -o

      • 部署apache

      • ansible host3 -m shell -a ‘uptime’ -o

        • 查询系统负载
  • 2.复制模块

    • 帮助

      • ansible-doc copy
    • 案例

ansible webserver -m copy -a ‘src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777’

如果文件没有改变,又进行拷贝,则不执行

如果文件改变了,进行了拷贝,加上backup='yes',会产生拷贝,同时对老的文件进行备份

如果文件有多份,可以进行备份。

[root@localhost ~]# ls /tmp/ 2.txt  2.txt.17037.2024-07-16@16:23:41~

  • 3.用户模块

    • 帮助

      ansible-doc user

    • 创建用户

      ansible webserver -m user -a 'name=laochen state=present'
      

      注释:

      state=present 在这里的作用是告诉 Ansible,如果用户 laochen不存在,则创建它;如果已存在,则不做任何更改。这种方式确保了配置的一致性和可重复性,符合 Ansible 的声明式配置管理特性。
      

      创建

    • 修改密码

      • 1.生成加密密码

        mkpasswd --method=SHA-512
        密码: //此处手动输入123456
        #下面是生成的加密后的密码
        $6$tu7bPJuzEcPfTSxo$MVLJdfTGdZv7RtJ70RCCHxxWDiR0EHo3epAfGRiVHTR27Z3lQ21Pw3aH00iz3KEBCjDlmbcIPJIDijkk1bFjd1
        
        

        无需用户交互的方式

        mkpasswd --method=SHA-512 --stdin <<< "1234"
        表示将1234生成加密密码
        
      • 2.修改密码

        • ansible webserver -m user -a ‘name=laochen password=“ 6 6 6tu7bPJuzEcPfTSxo$MVLJdfTGdZv7RtJ70RCCHxxWDiR0EHo3epAfGRiVHTR27Z3lQ21Pw3aH00iz3KEBCjDlmbcIPJIDijkk1bFjd1”’
      • 3.可通过ssh测试是否修改成功

        • 随便找一个host主机

          ssh laochen@host2
          laochen@host2's password:
          
          输入密码
          
          记得退出
          exit
          
    • 修改shell

      • ansible webserver -m user -a ‘name=laochen shell=/sbin/nologin append=yes’
        • apend=yes 表示追加
    • 删除用户

      • ansible webserver -m user -a ‘name=laochen state=absent’
        • 删除
        • state=absent 表示确保用户不存在,如果存在,删除,如果不存在,不执行操作
  • 4.软件包管理

    • 帮助

      • ansible-doc yum
    • ansible host1 -m yum -a ‘name=“*” state=latest’

      • 升级所有包
    • ansible host2 -m yum -a ‘name=“httpd” state=latest’

      • 安装apache
    • ansible host2 -m yum -a ‘name=“httpd” state=absent’

  • 5.服务模块

    • 帮助
      • ansible-doc service
    • ansible host2 -m service -a ‘name=httpd state=started’
      • 启动
    • ansible host2 -m service -a ‘name=httpd state=started enabled=yes’
      • 开机启动
    • ansible host2 -m service -a ‘name=httpd state=stopped’
      • 停止
    • ansible host2 -m service -a ‘name=httpd state=restarted’
      • 重启
    • ansible host2 -m service -a ‘name=httpd state=started enabled=no’
      • 开机禁止启动
  • 6.文件模块

    • 帮助

      • ansible-doc file
    • ansible host1 -m file -a ‘path=/tmp/88.txt mode=777 state=touch’

      • 创建文件
    • ansible host1 -m file -a ‘path=/tmp/99 mode=777 state=directory’

      • 创建文件夹
  • 7.收集模块

    用于收集目标主机的系统信息和配置

    • 帮助
      • ansible-doc setup
    • ansible host3 -m setup
      • 查询所有信息
    • ansible host3 -m setup -a ‘filter=ansible_all_ipv4_addresses’
      • 查看ipv4地址
  • 8.fetch

    • fetch 从远程某主机获取文件到本地

    • dest:用来存放文件的目录,例如存放目录为backup,源文件名称为/etc/profile ;那么在主机中,保存为/backup/master/etc/profile

    • src:在远程拉取的文件,并且必须是一个file,不能是目录。

    • 示例

      • # ansible host4 -m file -a ‘path=/root/a.txt state=touch’

        • host4创建a.txt
      • # ansible host4 -m fetch -a “src=/root/a.txt dest=/root/a.txt”

        或者

        # ansible host4 -m fetch -a “src=/root/a.txt dest=/root/”

      • 会在本地生成新的目录

        # tree  host4
        host4
        └── root
            └── a.txt
        
        
  • 9.cron

    • 语法

      • cron 管理crontab计划任务
      • action:cron backup= #如果设置,则创建一个crontab备份[yes|no]
      • cron_file= #如果指定,使用这个文件cron.d,而不是单个用户
      • day= #日应该运行的工作( 1-31, , /2, )
      • hour= #小时 ( 0-23, , /2, )
      • minute= #分钟( 0-145, , /2, )
      • month= #月( 1-12, *, /2, )
      • weekday= #周 ( 0-6 for Sunday-Saturday, )
      • job= #指明运行的命令是什么
      • name= #定时任务描述
      • reboot #任务在重启时运行,不建议使用,建议使用special_time
      • special_time #特殊的时间范围,参数:reboot(重启时),annually(每年),monthly(每月),weekly(每周),daily(每天),hourly(每小时)
      • state #指定状态,present表示添加定时任务,也是默认设置, absent表示删除定时任务
      • user #以哪个用户的身份执行
    • 示例

      • ansible host1 -m cron -a 'name=“sync time from ntpserver” minute=“*/10” job=“/sbin/ntpdate 192.168.145.145 &> /dev/null” ’

        • 每隔十分钟,从192.168.145.145中对照一次时间
      • host1:

        [root@localhost ~]# crontab -l
        no crontab for root
        [root@localhost ~]# crontab -l
        #Ansible: sync time from ntpserver
        */10 * * * * /sbin/ntpdate 192.168.229.59 &> /dev/null
        
      • 清除计划任务

         ansible host1 -m cron -a 'name="sync time from ntpserver" state=absent '   
        
  • 10.group

    • group 用户组模块,添加或删除组
    • gid   #设置组的GID号
    • name= #管理组的名称
    • state  #指定组状态,默认为创建,设置值为absent为删除,present创建
    • system  #设置值为yes,表示创建系统组
    • ansible host1 -m group -a ‘name=hhh state=present’
  • 11.script

    • 在指定节点运行服务端的脚本

    • 示例

      • 将服务器端上的脚本放在节点机器上运行

        # vim wan.sh
        #!/bin/bash
         date > /root/time.txt
        
      • # ansible host1 -m script -a “./wan.sh”

      • 去host1查看

        # ls
        anaconda-ks.cfg  time.txt
        # cat time.txt
        2024年 07月 17日 星期三 09:53:11 CST
        
        
  • 12.unarchive

    • 解压缩

    • 默认情况下,此模块会将本地压缩包拷贝到远程机器上解压,当设置了remote_src=yes选项表示解压远程主机上的压缩包

    • ​ 相关选项:

    • ​ src: 必选项, 要解压的包名

    • ​ dest: 必选项, 解压到哪个目录下

    • ​ remote_src:

    • ​ yes: 解压远程主机上的包

    • ​ no: 将管理机上的包传到远程主机上解压

      tar -jcf 1.tar /etc
      ansible host1 -m unarchive -a 'src=/root/1.tar dest=/tmp/'
      
  • 16
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值