• Ansible介绍
  • ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
  • ansible是基于 paramiko 开发的,并且基于模块化工作,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。ansible不需要在远程主机上安装client/agents,因为它们是基于ssh来和远程主机通讯的。ansible目前已经已经被红帽官方收购,是自动化运维工具中大家认可度最高的,并且上手容易,学习简单。是每位运维工程师必须掌握的技能之一。
  • Ansible特点

1、部署简单,只需在主控端部署Ansible环境,被控端无需做任何操作; 2、默认使用SSH协议对设备进行管理; 3、有大量常规运维操作模块,可实现日常绝大部分操作。 4、配置简单、功能强大、扩展性强; 5、支持API及自定义模块,可通过Python轻松扩展; 6、通过Playbooks来定制强大的配置、状态管理; 7、轻量级,无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可; 8、提供一个功能强大、操作性强的Web管理界面和REST API接口——AWX平台。

| 主机名| IP |安装软件 |组名| | -------- | -------- | -------- | | 管理主机 | 192.168.200.128 |Ansible || | 被管理主机 | 192.168.200.129 | |abc| | 被管理主机 | 192.168.200.130 | |aaa|

  • ** 安装服务**

  • 在管理主机上安装Ansible

      # yum install -y epel-release  //安装epel源
      # yum install ansible -y
      # ansible --version          //查看ansible版本
      # yum install tree -y
      # tree /etc/ansible/      //树状结构展示文件夹
      	/etc/ansible/
      	├── ansible.cfg    #ansible的配置文件
      	├── hosts         #ansible的主仓库,用于存储需要管理的远程主机的相关信息
      	└── roles     #角色
    
  • 配置主机清单

      # cd /etc/ansible
      # vim hosts       //配置主机清单
      	[abc]                           //自定义一个组名
      	192.168.200.129     //添加被管理主机的IP
      	[aaa]
      	192.168.200.130
    
  • 设置SSH无密码登录

      # ssh-keygen -t rsa
      # ssh-copy-id root@192.168.200.129
      # ssh-copy-id root@192.168.200.130    //配置密钥对验证
      ↓↓免交互代理 
      # ssh-agent bash
      # ssh-add
    

  • Ansible命令

  • command模块 (用于在被管理主机上运行命令)

  • 命令格式:ansible [主机] [-m 模块] [-a args]

      # ansible-doc -l     //列出所有已安装的模块 注:按q退出
      # ansible-doc -s yum   //-s列出yum模块描述信息和操作动作
      # ansible 192.168.200.129 -m command -a 'date'  //指定ip执行date
      # ansible abc -m command -a 'date'       //指定分类执行date
      # ansible all -m command -a 'date'        //所有hosts主机执行date命令
      # ansible all -a 'ls /'      如果不加-m模块,则默认运行command模块
    

  • cron模块

  • 用于定义任务计划

  • 两种状态(state):present表示添加(可以省略),absent表示移除。  

      # ansible-doc -s cron      //查看cron模块信息
      # ansible abc -m cron -a 'minute="*/1" job="/bin/echo heihei" name="test cron job"'
      # ansible abc -a 'crontab -l'
      # ansible abc -m cron -a 'name="test cron job" state=absent'    //移除计划任务,假如该计划任务没有取名字,name=None即可
    

  • user模块

  • 用于创建新用户和更改、删除已存在的用户

  • user模块是请求的是useradd, userdel, usermod三个指令

      # ansible-doc -s user
      # ansible abc -m user -a 'name="test01"'    //创建用户test01
      # ansible abc -m command -a 'tail /etc/passwd'
      # ansible abc -m user -a 'name="test01" state=absent'    //删除用户test01
    

  • group模块

  • 对用户组进行管理

  • group模块请求的是groupadd, groupdel, groupmod 三个指令。

      # ansible-doc -s group
      # ansible abc -m group -a 'name=mysql gid=306 system=yes'    //创建mysql组 将mysql用户添加进去
      # ansible abc -a 'tail /etc/group'
      # ansible abc -m user -a 'name=test01 uid=306 system=yes group=mysql'
      # ansible abc -a 'tail /etc/passwd'
      # ansible abc -a 'id test01'   
    

  • copy模块

  • 用于实现文件复制和批量下发文件(src:本地路径 dest:被管理主机文件路径)

      # ansible-doc -s copy
      # ansible abc -m copy -a 'src=/etc/fstab dest=/opt/fstab.back owner=root mode=640'
      //(属主root  权限640)
      # ansible abc -a 'ls -l /opt'
      # ansible abc -a 'cat /opt/fstab.back'
      # ansible abc -m copy -a 'content="hello heihei!"dest=/opt/fstab.back' 
      //将hello heihei!写入/opt/fstab.back
      # ansible abc -a 'cat /opt/fstab.back' 
    

  • file模块

  • 用于设置文件属性 (path:文件路径 src:定义源文件路径 )

      # ansible-doc -s file
      # ansible abc -m user -a 'name=mysql system=yes'
      # ansible abc -m group -a 'name=mysql system=yes'
      # ansible abc -m file -a 'owner=mysql group=mysql mode=644 path=/opt/fstab.back'        //修改文件的属主属组权限等
      # ansible abc -m file -a 'path=/opt/fstab.link src=/opt/fstab.back state=link'      //设置/opt/fstab.link为/opt/fstab.back的链接文件
      # ansible abc -m file -a "path=/opt/fstab.back state=absent"               //删除一个文件
      # ansible abc -m file -a "path=/opt/test state=touch"             创建一个文件
    

  • ping模块

  • 用于检测指定主机的连通性

      # ansible all -m ping
    

  • shell模块

  • 可以在被管理主机上运行命令,并支持像管道符号等功能的复杂命令

  • 可以创建用户使用无交互模式给用户设置密码

     # ansible-doc -s shell
     # ansible abc -m shell -a 'echo abc123|passwd --stdin mysql'      
    

  

  • service模块

  • 用来控制管理服务的运行状态

  • enabled:开机自启动,取值ture或false ,name:定义服务名称,state指定服务状态取值分别为started 、stoped、restarted

      # yum install -y httpd (到abc组的主机装httpd  abc需要有httpd服务才能控制)
      # ansible-doc -s service
      # ansible abc -m service -a 'enabled=true name=httpd state=started'        //启动httpd服务
      # ansible abc -a 'systemctl status httpd'        //查看web服务器httpd运行状态
    

  • script模块

  • 可以将本地脚本复制到被管理主机上进行执行。需要注意使用相对路径来指定脚本

      # ansible-doc -s script
      # vi test.sh
      	#!/bin/bash
      	echo "hello ansible from script"> /opt/script.txt
      # chmod +x test.sh
      # ansible abc -m script -a 'test.sh'
      # ansible abc -a 'cat /opt/script.txt'
    

  • yum模块

  • 负责在被管理主机上安装与卸载软件包,需要提前在每个节点配置自己的YUM仓库,

  • name:指定安装的软件包,可以带上版本号,否则安装最新版本。

  • state :指定安装的软件包状态 (present、latest 表示安装 , absent 表示卸载)

     # ansible-doc -s yum
     # ansible abc -m yum -a 'name=zsh'           //yum安装zsh
     # ansible abc -a 'rpm -q zsh'
     # ansible abc -m yum -a 'name=zsh state=absent'     //卸载zsh
     # ansible abc -a 'rpm -q zsh'
    

 

  • setup模块

  • 用于收集、查看被管理主机的facts(facts是Ansible采集被管理主机设备信息的一个功能)

      # ansible-doc -s setup
      # ansible abc -m setup