- Ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、chef、func、fabric)的优势,实现了批量系统配置、批量程序部署、批量运行命令等功能。
- Ansible是基于 paramiko 开发的,而且基于模块化工做,自己没有批量部署的能力。真正具备批量部署的是ansible所运行的模块,Ansible只是提供一种框架。Ansible不须要在远程主机上安装client/agents,由于它们是基于ssh来和远程主机通信的。Ansible目前已经已经被红帽官方收购,是自动化运维工具中你们承认度最高的,而且上手容易,学习简单。是每位运维工程师必须掌握的技能之一。
一、部署简单,只需在主控端部署Ansible环境,被控端无需作任何操做;
二、默认使用SSH协议对设备进行管理;
三、有大量常规运维操做模块,可实现平常绝大部分操做。
四、配置简单、功能强大、扩展性强;
五、支持API及自定义模块,可经过Python轻松扩展;
六、经过Playbooks来定制强大的配置、状态管理;
七、轻量级,无需在客户端安装agent,更新时,只需在操做机上进行一次更新便可;
八、提供一个功能强大、操做性强的Web管理界面和REST API接口——AWX平台。
mysql
主机名 | 操做系统 | IP地址 |
---|---|---|
Ansible Server | Centos 7.3 X86_64 | 192.168.6.117 |
Client1 | Centos 7.3 X86_64 | 192.168.1.130 |
Client2 | Centos 7.3 X86_64 | 192.168.1.129 |
在 Ansible Server 上安装 Ansible
Ansible是经过ssh远程链接的,因此不须要在被管理端安装Clientweb
# 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 #角色
配置主机清单sql
# cd /etc/ansible # vim hosts //配置主机清单 [www] //自定义一个组名 192.168.6.130 //添加被管理主机的IP [wzn] 192.168.6.129
设置SSH无密码登陆shell
# ssh-keygen -t rsa # ssh-copy-id root@192.168.6.129 # ssh-copy-id root@192.168.6.130 //配置密钥对验证 ↓↓免交互代理 # ssh-agent bash # ssh-add
命令格式:ansible [主机] [-m 模块] [-a args]vim
# 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模块
两种状态(state):present表示添加(能够省略),absent表示移除。bash
# ansible-doc -s cron //查看cron模块信息 # ansible www -m cron -a 'minute="*/1" job="/bin/echo heihei" name="test cron job"' # ansible www -a 'crontab -l' # ansible www -m cron -a 'name="test cron job" state=absent' //移除计划任务,假如该计划任务没有取名字,name=None便可
user模块是请求的是useradd, userdel, usermod三个指令服务器
# ansible-doc -s user # ansible www -m user -a 'name="test01"' //建立用户test01 # ansible www -m command -a 'tail /etc/passwd' # ansible www -m user -a 'name="test01" state=absent' //删除用户test01
group模块请求的是groupadd, groupdel, groupmod 三个指令框架
# ansible-doc -s group # ansible www -m group -a 'name=mysql gid=306 system=yes' //建立mysql组 将mysql用户添加进去 # ansible www -a 'tail /etc/group' # ansible www -m user -a 'name=test01 uid=306 system=yes group=mysql' # ansible www -a 'tail /etc/passwd' # ansible www -a 'id test01'
用于实现文件复制和批量下发文件(src:本地路径 dest:被管理主机文件路径)运维
# ansible-doc -s copy # ansible www -m copy -a 'src=/etc/fstab dest=/opt/fstab.back owner=root mode=640' //(属主root 权限640) # ansible www -a 'ls -l /opt' # ansible www -a 'cat /opt/fstab.back' # ansible www -m copy -a 'content="hello heihei!"dest=/opt/fstab.back' //将hello heihei!写入/opt/fstab.back # ansible www -a 'cat /opt/fstab.back'
用于设置文件属性 (path:文件路径 src:定义源文件路径 )ssh
# ansible-doc -s file # ansible www -m user -a 'name=mysql system=yes' # ansible www -m group -a 'name=mysql system=yes' # ansible www -m file -a 'owner=mysql group=mysql mode=644 path=/opt/fstab.back' //修改文件的属主属组权限等 # ansible www -m file -a 'path=/opt/fstab.link src=/opt/fstab.back state=link' //设置/opt/fstab.link为/opt/fstab.back的连接文件 # ansible www -m file -a "path=/opt/fstab.back state=absent" //删除一个文件 # ansible www -m file -a "path=/opt/test state=touch" 建立一个文件
用于检测指定主机的连通性
# ansible all -m ping
能够建立用户使用无交互模式给用户设置密码
# ansible-doc -s shell # ansible www -m shell -a 'echo abc123|passwd --stdin mysql'
enabled:开机自启动,取值ture或false ,name:定义服务名称,state指定服务状态取值分别为started 、stoped、restarted
# ansible-doc -s service # ansible www -m service -a 'enabled=true name=httpd state=started' //启动httpd服务 # ansible www -a 'systemctl status httpd' //查看web服务器httpd运行状态
能够将本地脚本复制到被管理主机上进行执行。须要注意使用相对路径来指定脚本
# ansible-doc -s script # vi test.sh #!/bin/bash echo "hello ansible from script"> /opt/script.txt # chmod +x test.sh # ansible www -m script -a 'test.sh' # ansible www -a 'cat /opt/script.txt'
# ansible-doc -s yum # ansible www -m yum -a 'name=zsh' //yum安装zsh # ansible www -a 'rpm -q zsh' # ansible www -m yum -a 'name=zsh state=absent' //卸载zsh # ansible www -a 'rpm -q zsh'
# ansible-doc -s setup # ansible www -m setup