自动化运维工具——Ansible

Ansible基本概述

Ansible是新出现的自动化运维工具,它基于Python开发,集合了众多运维工具(puppet、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
它是一个配置管理系统,我们只需要通过ssh访问服务器或设备就可以帮助我们完成一些批量任务或重复性的工作。

ansible软件特点

  • 部署简单。它不需要单独安装客户端,只需要通过ssh协议就可以对设备进行管理
  • 配置简单。安装完成不需要启动任何服务,仅需要安装对应工具即可
  • 扩展性强。基于Python开发,可自定义Python模块实现批量管理
  • 模块化。所有的操作都基于模块

ansible软件服务意义

  • 提高工作效率
  • 提高工作准确度
  • 减少维护成本
  • 减少重复性工作

ansible基础架构图

图片来自网络
上图模块详解:

  1. (connection plugins)连接插件。用于连接主机(被管理端)
  2. (core modules)核心模块。连接主机实现操作,它依赖于具体的模块来实现具体的事情
  3. (custom modules)自定义模块。根据需求编写模块
  4. (plugins)插件。完成模块功能的补充
  5. (playbooks)剧本。将多个任务定义在剧本中,由Ansible自动执行
  6. (host inventory)主机清单。定义Ansible需要操作主机的范围
    环境准备:
IP OS 描述
192.168.8.41 CentOS7.8 Ansible管理端
192.168.8.51 CentOS7.8 被管理端
  1. 先升级epel源,否则可能找不到软件包

    yum install -y epel-relase
    
  2. 安装软件

    yum install -y ansible
    
  3. ssh部署公钥发至被管理端

    #!/bin/bash
     for i in `cat ip.list`
     do
      ssh-copy-id -i /root/.ssh/id_rsa.pub root@$i "-o StrictHostKeyChecking=no" &>/dev/null
     done 
    
  4. 清单管理
    配置/etc/ansible/hostsinventory文件

    //方法1:通过IP定义主机
    [jxj]
    192.168.8.51
     
    //方法2:iventory文件支持主机名和非标准的ssh端口以及正则表达式和
    [domainname]
    www.web1.com:11111
    www.web2.com
    www.web3.com
    www.web[1:3].com
    //方法3:支持指定变量,可对个别主机进行特别配置,如登录用户、密码等
    [server_ip_list]
    192.168.8.100 ansible_ssh_user_=root ansible_ssh_pass="123456"
    

inventory内置变量

参数 用途 例子
ansible_ssh_host 定义 hosts ssh地址 ansible_ssh_host=192.168.8.100
ansible_ssh_port 定义shh端口 ansible_ssh_port=5201
ansible_ssh_user 定义hosts ssh认证用户 ansible_ssh_user=user
ansible_ssh_pass 定义hosts ssh认证密码 ansible_ssh_pass=pass
ansible_sudo 定义hosts sudo用户 ansible_sudo=sssss
ansible_sudo_pass 定义hosts sudo密码 ansible_sudo_pass=123
ansible_sudo_exe 定义hosts sudo路径 ansible_sudo_exe=/usr/sbin/sudo
ansible_connection 定义hosts连接方式 ansible_connection=local
ansible_ssh_shell_type 定义hosts shell类型 ansible_ssh_shell_type=bash
ansible_python_interpreter 定义hosts任务执行python路径 ansible_python_interpreter=/usr/bin/python3.7
ansible_*_interpreter 定义hosts其它语言解析路径 ansible_*_interpreter=/usr/bin/java
ansible_ssh_private_key_file 定义hosts私钥 ansible_ssh_private_key_file=/root/key

ansible常用模块

  • command模块(默认)

    ansible jxj -a "hostname"
    

    chdir:在执行命令之前切换相应目录

    ansible jxj -m command -a "chdir=/tmp touch aa.txt"
    creates: 		//如果文件存在,不执行命令操作
    ansible jxj -m command -a "creates=c.txt touch bb.txt"
    removes: 		//如果文件存在,执行命令
    ansible jxj -m command -a "removes=c.txt touch bb.txt"
    

注意:command模块不支持< > | ; &等符号

  • shell模块(支持正则表达式以及一些特殊符号)

    ansible jxj -m shell -a "sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config"
    
  • copy模块: 将数据信息进行批量分发(默认覆盖内容)

    ansible jxj -m copy -a "src=/etc/hosts dest=/etc/"
    

    copy模块参数:

    src:本地路径/文件
    dest:目标路径/文件(如存在重命名)
    backup(=yes/no):额外生成一个备份文件
    group:指定组
    content:添加内容
    owner:指定主
    mode:修改权限
    
  • file模块: 设置文件属性信息

    ansible jxj -m file -a "dest=/etc/hosts owner=root group=root mode=777"	
    

    state参数:

    =absent    --- 缺席/删除数据信息
    =directory --- 创建一个目录信息
    =file      --- 检查创建的数据信息是否存在 绿色存在 红色不存在
    =hard      --- 创建一个硬链接文件
    =link      --- 创建一个软链接文件
    =touch     --- 创建一个文件信息
    
  • yum模块:

    ansible jxj -m yum -a "name=httpd state=installed"
    

    name:指定安装软件名称
    state参数:

    =installed    //安装软件
    =present	   //安装软件
    =latest        //安装最新版软件
    =absent     //卸载软件
    =removed   //卸载软件
    
  • service模块: 管理服务器的运行状态
    name: 指定管理的服务名称
    enabled(=yes/no): 指定服务是否开机自启动

    ansible jxj -m service -a "name=sshd state=restarted"
    

    state参数:

    停止stopped
    开启started
    重启restarted
    
  • cron模块: 批量设置多个主机的定时任务信息

    ansible jxj -m cron -a "minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'" 
    

    crontab -e

    minute:               //设置分钟信息( 0-59,  */2)                       
    hour:                  //设置小时信息( 0-23, */2 )                
    day:                   //设置日期信息( 1-31, *, */2)                       
    month:              //设置月份信息 ( 1-12,*/2 )                       
    weekday:          // 设置周信息 ( 0-6 for Sunday-Saturday)                      
    job                 //用于定义定时任务需要干的事情
    

    给计划任务设置注释信息(只能操作ansible设置的)

    ansible jxj -m cron -a "name='jxjcron' minute=3 hour=2 job='/usr/sbin/ntpdate mtp1.aliyum.com &>/dev/null' disabled=yes"
    

    删除指定计划任务(只能操作ansible设置的)

    ansible jxj -m cron -a "name='jxjcron' state=absent"
    
  • mount模块: 批量进行挂载操作

    rc:  需要挂载的存储设备或文件信息 
    path: 指定目标挂载点目录
    fstype: 指定挂载时的文件系统类型
    state=
    present/mounted     //挂载
    present: 	不会立即挂载,修改fstab文件,实现开机自动挂载
    mounted:	//立即挂载, 并且会修改fstab文件,实现开机自动挂载 
    absent/unmounted    //卸载
    absent:     //立即卸载, 并且会删除fstab文件信息,禁止开机自动挂载
    unmounted:  //立即卸载, 但是不会会删除fstab文件信息
    
  • user模块: 实现批量创建用户

    ansible jxj -m user -a "name=beitai_one"
    

    user模块参数:

    name:指定创建用户的名称
    gid:指定gid
    uid:指定uid
    group/groups:指定组信息
    create_home(yes/no):创建家目录,默认yes
    shell:指定用户登录的shell类型
    

    给用户生成密码:

    ansible all -i localhost, -m debug -a "msg={{'123456' | password_hash('sha512','密码口令')}}"
    

    使密码生效:

    ansible jxj -m user -a 'name=jxj1 password=$6$jxj$lb4BpCo33QoczNvSLYD5DagSXHWBgYeNCYx38LxkrjZFWlYEgkVG4SzWgxACy3LEoOps3latRCqoTLQGRVFRL1'
    

更多模块信息请参考Ansible官方文档或使用ansible-doc -l命令查看。

发布了11 篇原创文章 · 获赞 11 · 访问量 5964

猜你喜欢

转载自blog.csdn.net/nmb_jiang/article/details/104826245