自动化运维-Ansible
一、概述
将平常运维过程汇总重复性的重做,比如软件的安装,配置文件的修改,由手工执行转变为自动化操作,以提高工作效率。比如有
100
台Nginx服务器,要修改配置文件,若一台台修改则效率太低,若写shell
,则需要执行100次循环,放到后台则会有100个线程,这个时候就用到了自动化运维工具。
开源自动化运维工具
puppet
基于
Ruby
语言。适合于大型架构,相对于ansible和saltstack会复杂些。
saltstack
基于
python
语言。并发能力比ansible要好, 需要维护被管理端的服务,走的协议是zeromq
ansible
基于
python
语言。简单快捷,被管理端不需要启服务。直接走ssh协议,需要验证所以机器多的话速度会较慢默认使用ssh进行管理,基于python里的
paramiko
模块开发管理端和被管理端不需要启动服务
配置简单,功能强大,扩展性强
安装
#需要配置epel源与CentOS源
yum install ansible
二、主机分组
Ansible
通过一个主机清单功能来实现服务器分组。分组可以通过主机名,IP,主机名范围,IP范围,别名来分组
Ansible
的默认主机清单配置文件为/etc/ansible/hosts.
vim /etc/ansible/hosts
分组示例:
#1.根据IP
[group1]
10.1.1.12
10.1.1.13
10.1.1.14:2222 #指定端口
#2.根据IP范围
[group2]
10.1.1.[11:15] #表示11到15这五台机器
#3.根据主机名分
[group3]
apache[1:10].tigeru.cn
nginx[a:z].tigeru.cn
#4.定义别名
nginx1 ansible_ssh_host=10.1.1.11 ansible_ssh_port=22
[group3]
nginx1
#5.指定用户名与密码
nginx1 ansible_ssh_host=10.1.1.11 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
注意:使用时需要配置免密或指定密码,要不然得通过-k
输入密码
三、Ansible 模块
官方文档: https://docs.ansible.com/ansible/latest/modules/list_of_all_modules.html
结果颜色:红色表示失败,浅黄色表示成功,绿色表示没有进行任何操作
ping
网络测试
ansible group1 -m ping
#所有主机
ansible all -m ping
hostname
修改主机名
ansible 10.1.1.12 -m hostname -a 'name=test.tigeru.cn'
ansible group1 -m hostname -a 'name=test.tigeru.cn'
file
官方文档: https://docs.ansible.com/ansible/latest/modules/file_module.html#file-module
#创建目录
ansible group1 -m file -a "path=/tmp/test state=directory"
#创建文件
ansible group1 -m file -a "path=/tmp/test.txt state=touch"
#递归修改属组属组
ansible group1 -m file -a 'path=/tmp/test recurse=yes owner=tiger group=tiger mode=1777'
#删除文件
ansible group1 -m file -a 'path=/tmp/test.txt state=absent'
#删除目录及目录中文件
ansible group1 -m file -a 'path=/tmp/test state=absent'
#创建软链接文件
ansible group1 -m file -a 'src=/etc/ path=/tmp/fstab state=link'
#创建硬连接文件
ansible group1 -m file -a 'src=/etc/fstab path=/tmp/fstab2 state=hard'
stat
查看文件状态
ansible group1 -m stat -a 'path=/tmp/123123.txt'
copy
拷贝文件
类似的还有
template
,但是整拷贝文件官方文档:https://docs.ansible.com/ansible/latest/modules/copy_module.html#copy-module
#拷贝文件(force覆盖,默认为yes)
ansible group1 -m copy -a 'src=/tmp/1.txt dest=/tmp/2.txt force=yes'
#直接填写内容
ansible group1 -m copy -a 'content="tiger" dest=/tmp/2.txt'
#拷贝时备份
ansible group1 -m copy -a 'src=/tmp/1.txt dest=/tmp/2.txt backup=yes'
#拷贝目录(注意加/和不加/区别,加/表示拷贝目录中内容)
ansible group1 -m copy -a 'src="/etc/yum.repos.d/" dest=/tmp/test3'
fetch
拉取文件到管理机器,
官方文档:https://docs.ansible.com/ansible/latest/modules/fetch_module.html#fetch-module
ansible group1 -m fetch -a "src=/tmp/2.txt dest=/tmp/"
user
用户管理
官方文档: https://docs.ansible.com/ansible/latest/modules/user_module.html#user-module
#1.创建用户
ansible group1 -m user -a 'name=a state=present'
#2.创建系统用户,并制定bash
ansible group1 -m user -a 'name=c state=present system=yes shell="/sbin/nologin"'
#3.创建一个普通用户叫 b ,并产生空密码密钥对
ansible group1 -m user -a 'name=b generate_ssh_key=yes'
#4.创建用户并指定密码
echo 123456 | openssl passwd -1 -stdin
# $1$DpcyhW2G$Kb/y1f.lyLI4MpRlHU9oq0
#注意格式,密码要用双引号引起来
ansible group1 -m user -a 'name=ccc uid=2000 state=present password="$1$DpcyhW2G$Kb/y1f.lyLI4MpRlHU9oq0"'
#5.删除用户并删除家目录
ansible group1 -m user -a 'name=a state=absent remove=yes'
group
组管理
官方文档:https://docs.ansible.com/ansible/latest/modules/group_module.html#group-module
#创建组
ansible group1 -m group -a 'name=test gid=3000 state=present'
#删除组
ansible group1 -m group -a 'name=test state=absent'
cron
管理计划任务任务
官方文档:https://docs.ansible.com/ansible/latest/modules/cron_module.html#cron-module
#创建(不指定的时间默认为*)
ansible group1 -m cron -a 'name="test cron1" user=root job="touch /tmp/1.txt" minute=*/2'
#删除
ansible group1 -m cron -a 'name="test cron1" state=absent'
yum_repository
用于配置yum仓库。
官方文档:https://docs.ansible.com/ansible/latest/modules/yum_repository_module.html
#创建 或修改
ansible group1 -m yum_repository -a "name=local2 description=localyum baseurl=file:///mnt/ enabled=yes gpgcheck=no"
#删除
ansible group1 -m yum_repository -a "name=local2 state=absent"
yum
软件包管理
官方文档:https://docs.ansible.com/ansible/latest/modules/yum_module.html#yum-module
#安装
ansible group1 -m yum -a 'name=vsftpd state=present'
#安装多个
ansible group1 -m yum -a 'name=httpd,httpd-devel'
#卸载
ansible group1 -m yum -a 'name=vsftpd state=present'
service
用于控制服务的启动,关闭,开机自启动等
官方文档:https://docs.ansible.com/ansible/latest/modules/service_module.html#service-module
#启动服务
ansible group1 -m service -a 'name=vsftpd state=started'
#关闭服务
ansible group1 -m service -a 'name=vsftpd state=stopped'
#设置开机启动
ansible group1 -m service -a 'name=vsftpd enabled=on'
#关闭开机启动
ansible group1 -m service -a 'name=vsftpd enabled=false'
script
官方文档:https://docs.ansible.com/ansible/latest/modules/script_module.html#script-module
vim /tmp/test.sh
#!/bin/bash
mkdir /tmp/test
touch /tmp/test/{1..10}.txt
#执行脚本
ansible group1 -m script -a '/tmp/test.sh'
command与shell
执行
linux
命令的shell模块与command模块差不多(command模块不能执行一些类似$HOME,>,<,|等符号,但shell可以)
https://docs.ansible.com/ansible/latest/modules/command_module.html
https://docs.ansible.com/ansible/latest/modules/shell_module.html
ansible group1 -m command -a "useradd u1"
ansible group1 -m command -a "id u1"
ansible group1 -m command -a "touch /tmp/111.txt"
ansible group1 -m shell -a "cat /etc/passwd |wc -l"
四、Playbook
Playbook,剧本,用于
ansible
操作的编排,优点:灵活,可以显示执行过程与错误提示,
官方文档:https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html
YMAL格式
以
.yaml
或.yml
结尾文件的第一行以 "---"开始,表明
YMAL
文件的开始(可选的)以
#
号开头为注释不要使用
tab
键通一层级缩进必须对齐
键值对使用
键: 值
的形式组成(冒号后面必须是一个空格
)
语法
hosts:
用于指定要执行任务的主机,其可以是一个或多个由冒号分隔主机组
remote_user:
用于指定远程主机上的执行任务的用户
tasks:
任务列表, 按顺序执行任务.
如果一个task失败, 则执行将会终止;重新执行将会继续执行,执行过的会绿色通过
handlers:
类似task,但需要使用notify通知调用
不管有多少个通知者进行了
notify
,等到play
中的所有task
执行完成之后,handlers
也只会被执行一次.handlers最佳的应用场景是用来重启服务
实例1
yum
安装httpd
,启动并设置为开机启动
创建yml
编排任务
mkdir /etc/ansible/playbook
vim /etc/ansible/playbook/httpd.yml
编辑内容如下
---
- hosts: group1
remote_user: root
tasks:
- name: 安装 apache
yum: name=httpd,httpd-devel state=latest
- name: 拷贝配置文件
copy: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restart apache
- name: 开启服务并设置开机启动
service: name=httpd state=started enabled=yes
handlers:
- name: 重启服务
service: name=httpd state=restarted
执行编排
ansible-playbook /etc/ansible/playbook/httpd.yml
实例2
修改httpd
的端口为8080
---
- hosts: group1
remote_user: root
vars:
- port: 8088
tasks:
- name: 修改httpd端口
shell: sed -i "s/^Listen.*/Listen {{port}}/" /etc/httpd/conf/httpd.conf
- name: 重启服务
service: name=httpd state=restarted
实例3
服务端安装nfs
,客户端挂载nfs
---
- hosts: 10.1.1.51
remote_user: root
tasks:
- name: 安装软件包
yum: name=nfs-utils,rpcbind state=present
- name: 创建共享目录
file: path=/share state=directory
- name: 修改配置文件
copy: content="/share *(ro)" dest=/etc/exports
- name: 开启服务并设置开机启动
service: name=nfs state=started enabled=on
- hosts: 10.1.1.52
remote_user: root
tasks:
- name: 安装软件包
yum: name=nfs-utils state=latest
- name: 创建挂载目录
file: path=/test state=directory
- name: 挂载
shell: mount 10.1.1.51:/share /test
五、roles
类似于开发中的封装,把需要重复使用的代码进行封装,方便调用
文件保存位置:
/etc/ansible/roles/
目录结构
files:用来存放由copy模块或script模块调用的文件。
tasks:至少有一个main.yml文件,定义各tasks。
handlers:有一个main.yml文件,定义各handlers。
templates:用来存放jinjia2模板。
vars:有一个main.yml文件,定义变量。
meta:有一个main.yml文件,定义此角色的特殊设定及其依赖关系。
实例1
编排LAMP
创建目录结构
cd /etc/ansible/roles
mkdir -p {httpd,mysql,php}/{files,tasks,handlers,templates,vars,meta}
touch {httpd,mysql,php}/{tasks,handlers,vars,meta}/main.yml
准备配置文件
echo "test 123" > httpd/files/www/index.html
echo "<?php phpinfo(); ?>" > httpd/files/www/phpinfo.php
编辑配置文件
# 1.编写httpd角色的main.yml文件
# vim /etc/ansible/roles/httpd/tasks/main.yml
- name: 安装Httpd
yum: name=httpd,httpd-devel state=present
- name: 同步配置文件
copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: restart httpd
- name: 同步主页文件
copy: src=www/ dest=/var/www/html/
- name: 启动httpd并开机自启动
service: name=httpd state=started enabled=yes
# 2.编写httpd角色里的handler
# vim /etc/ansible/roles/httpd/handlers/main.yml
- name: restart httpd
service: name=httpd state=restarted
# 3.编写mysql角色的main.yml文件
# vim /etc/ansible/roles/mysql/tasks/main.yml
- name: 安装mysql
yum: name=mariadb,mariadb-server,mariadb-devel state=present
- name: 启动mysql并开机自启动
service: name=mariadb state=started enabled=yes
# 4.编写php角色的main.yml文件**
# vim /etc/ansible/roles/php/tasks/main.yml
- name: 安装php及依赖包
yum: name=php-* state=present
notify: restart httpd
# 5.编写lamp的playbook文件调用前面定义好的三个角
# vim /etc/ansible/playbook/lamp.yaml
---
- hosts: group1
remote_user: root
roles:
- httpd
执行
ansible-playbook /etc/ansible/playbook/lamp.yaml