目录
Ansible
Ansible概述:
是一个配置管理系统(configuration management system),当下最流行的批量自动化运维工具之一.
常用的运维工具:
ssh/puppet(ruby)/ansible(无客户端,中小规模)(python)/saltstack(master-minion)(python)大规模
Ansible的作用:
批量部署,服务安装,日常备份
Ansible官方文档:
https://docs.ansible.com/ansible/latest/index.html
Ansible的特性:
无客户端软件,通过ssh远程管理
安装后不需要启动服务
依赖大量的Python模块扩展功能
配置文件:/etc/ansible/ansible.cfg
Ansible基础架构:
连接插件(connecter plugins):用来连接主机,连接被管理端
核心模块(core modules):连接主机,实现操作,依赖于具体模块来执行
自定义模块:用户自己开发的功能模块
剧本(playbook):将多个任务组合成一个剧本,由ansible自动批量执行
主机清单(host inventory):定义ansible管理的客户端主机范围
Ansible的命令格式:
ansible 主机清单名 -m 调用的模块 -a 动作命令
######################################################################################
Ansible的配置:
ansible:192.168.8.10
web:192.168.8.20
nfs:192.168.8.30
rsync:192.168.8.40
ifdown ens33;ifup ens33
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
1.安装ansible
(1)先配epel源:
epel源(扩展包):wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
linux镜像源(组包):wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
(2)安装ansible
yum -y install ansible
查看版本
ansible --version
2.配置清单
vim /etc/ansible/hosts
添加:
[web]
192.168.8.20
[nfs]
192.168.8.30
[rsync]
192.168.8.40
[benet:children]
web
nfs
rsync
保存退出
3.在ansible上配置ssh秘钥对访问
ssh-keygen -t rsa
ssh-copy-id root@192.168.8.20
ssh-copy-id root@192.168.8.30
ssh-copy-id root@192.168.8.40
########################################################################
调用常用模块
安装软件
修改配置
创建程序用户和组
创建目录,修改归属和权限
启动服务
挂载
测试
调用模块颜色显示:
翔黄色 更改成功
帽绿色 没有更改
深红色 错误
亮紫色 警告
列出所有模块
ansible-doc --list
查看ansible模块帮助
ansible-doc yum
1.command #仅支持简单语法命令,但语句中不能包含管道符等复杂元素
ansible web -m command -a "hostname"
ansible web -m command -a "useradd zhangsan"
2.shell #command升级版,支持复杂语句,但不支持别名
ansible web -m shell -a "echo 123 |passwd --stdin zhangsan"
3.yum
ansible web -m yum -a "name=httpd state=installed"
注释:name 安装的软件包名,多个软件","分开
state 服务状态
installed,present 安装软件包
removed,absent 卸载软件包
latest 安装最新软件包
4.copy
ansible benet -m copy -a "src=/etc/hosts dest=/etc/hosts backup=yes"
注释:
src 源文件路径
dest 目标文件路径
backup 覆盖到目标文件前,是否提前备份
content 添加文件内容
group 指定属组
owner 指定属主
mode 指定权限
案例:在ansible上远程配置rsync服务
(1)修改rsync配置文件,并传到rsync服务器
mkdir /etc/ansible/conf
cd /etc/ansible/conf
cp /etc/rsyncd.conf ./
vim rsyncd.conf
修改为:
uid = root
gid = root
port 873
address = 192.168.8.40
hosts allow = 192.168.8.0/24
max connections = 4
pid file = /var/run/rsyncd.pid
timeout = 900
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
[backup]
path = /backup
read only = no
auth users = rsync_backup
secrets file = /etc/rsync.password
保存退出
ansible rsync -m copy -a "src=rsyncd.conf dest=/etc/rsyncd.conf backup=yes"
(2)启动rsync服务
ansible rsync -m systemd -a "name=rsyncd state=restarted"
(3)创建目录,并赋权,更改属主属组
ansible rsync -m file -a "path=/backup owner=root group=root recurse=yes mode=777"
(4)配置rsync服务器的密码文件:
ansible rsync -m copy -a "content='rsync_backup:1' dest=/etc/rsync.password owner=root group=root mode=600"
ansible rsync -m shell -a "ls -l /etc/rsync.password"
ansible rsync -m shell -a "cat /etc/rsync.password"
(5)配置所有服务器的rsync连接密码文件
ansible benet -m copy -a "content='1' dest=/etc/server.pass owner=root group=root mode=600"
(6)测试:备份WEB的httpd.conf配置文件
ansible web -m shell -a "rsync -avz --password-file=/etc/server.pass /etc/httpd/conf/httpd.conf rsync_backup@192.168.8.40::backup"
5.service(或systemd)
ansible web -m service -a "name=httpd state=stopped"
等同于
ansible web -m shell -a "systemctl stop httpd"
注释:
name 指定服务名
state 指定服务运行状态
started 开启服务
stopped 关闭服务
reloaded 重载服务
restarted 重启服务
enabled 是否开机自启
6.group
在所有清单主机上创建组www,gid 666
ansible all -m group -a "name=www gid=666"
在所有清单主机删除组www
ansible all -m group -a "name=www gid=666 state=absent"
7.user
ansible all -m user -a "name=www"
ansible web -m shell -a "echo 123 |passwd --stdin www"
8.file
创建目录,并赋权,更改属主属组(recurse=yes可以由state=directory代替)
ansible rsync -m file -a "path=/cwb owner=root group=root recurse=yes mode=777"
创建文件
ansible rsync -m file -a "path=/cwb/test.txt owner=root group=root state=touch mode=777"
9.mount
ansible nfs -m file -a "path=/nfs owner=root group=root recurse=yes mode=777"
vim exports
添加:
/nfs 192.168.8.0/24(rw,sync,no_root_squash)
保存退出
ansible nfs -m copy -a "src=exports dest=/etc/exports"
ansible nfs -m systemd -a "name=nfs state=restarted"
ansible nfs -m systemd -a "name=rpcbind state=restarted"
挂载nfs目录到web下的/var/www/html
ansible web -m mount -a "src=192.168.8.30:/nfs path=/var/www/html fstype=nfs state=mounted"
注释:
state 挂载状态
mounted 挂载
unmounted 卸载
10.script
在ansible上编写任意测试脚本:
ansible web -m script -a "/root/test.sh"
ansible playbook:剧本
由一个或多个模块组成,完成统一的目的,实现自动化操作
剧本编写遵循yaml语法
yaml的三要素:***
缩进:两个字符,默认的tab键是四个字符,所以要使用tab键,需要修改.vimrc
vim /root/.vimrc
添加:
set tabstop=2
保存退出
冒号:冒号后面需要空格,除非以冒号结尾
短横杠:列表项,后面跟空格
playbook语法结构
ansible-playbook 选项 文件路径
选项:
-C 模拟预运行
--list-hosts:列出清单
--list-tasks:列出任务
--list-tags:列出标签
--syntax-check:语法检查
测试案例:通过playbook安装httpd,并修改端口号为8080
前提:安装httpd,复制httpd.conf到当前目录,修改端口为8080
vim httpd.yaml
添加:
- hosts: web
tasks:
- name: install httpd
yum: name=httpd state=latest
- name: httpd config
copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: restart httpd
- name: start httpd
service: name=httpd state=started
handlers:
- name: restart httpd
service: name=httpd state=restarted
保存退出
测试yaml: ansible-playbook -C httpd.yaml
执行yaml:ansible-playbook httpd.yaml
#####################################################################
playbook配置web--nfs--rsync架构环境
全局环境:修改各主机名:ansible、web、nfs、rsync
1.服务器配置
前提:
ifdown ens33;ifup ens33
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
vim /etc/hosts
192.168.8.10 ansible
192.168.8.20 web
192.168.8.30 nfs
192.168.8.40 rsync
保存退出
(1)安装ansible
yum -y install epel-release
yum -y install ansible
(2)ssh公钥
ssh-keygen -t rsa
ssh-copy-id root@web #web服务器
ssh-copy-id root@nfs #nfs服务器
ssh-copy-id root@rsync #rsync服务器
(3)复制/etc/hosts到被管理端
scp /etc/hosts root@web:/etc/
scp /etc/hosts root@nfs:/etc/
scp /etc/hosts root@rsync:/etc/
(4)创建ansible目录
mkdir -p /etc/ansible/ansible_playbook/{conf,file,scripts,tools}
(5)创建ansible清单
vim /etc/ansible/hosts
添加:
[web]
192.168.8.20
[nfs]
192.168.8.30
[rsync]
192.168.8.40
保存退出
(6)使用ansible copy 复制/etc/hosts到所有主机
ansible all -m copy -a "src=/etc/hosts dest=/etc"
2.基础环境部署
(1)网络环境(关闭firewall selinux)
(2)epel仓库
(3)安装rsync,nfs-utils
(4)创建组
(5)创建用户
(6)创建目录,并修改权限
(7)推送脚本
(8)推送rsync客户端密码文件,修改权限
(9)计划任务
vim /etc/ansible/ansible_playbook/base.yaml
添加:
- hosts: all
tasks:
- name: stop firewalld
shell: systemctl stop firewalld
- name: stop selinux
shell: setenforce 0
- name: clear repos.d
file: path=/etc/yum.repos.d/ state=absent
- name: create repos.d
file: path=/etc/yum.repos.d/ state=directory
- name: install base repo
get_url: url=http://mirrors.aliyun.com/repo/Centos-7.repo dest=/etc/yum.repos.d/CentOS-Base.repo
- name: install epel repo
get_url: url=http://mirrors.aliyun.com/repo/epel-7.repo dest=/etc/yum.repos.d/epel.repo
- name: install rsync nfs-utils
yum: name=rsync,nfs-utils state=installed
- name: create group www
group: name=www gid=666
- name: create user www
user: name=www uid=666 create_home=no shell=/sbin/nologin
- name: create rsync client password
copy: content='1' dest=/etc/rsync.pass mode=600
- name: create scripts directory
file: path=/server/scripts/ recurse=yes state=directory
- name: push scripts
copy: src=./scripts/rsync_backup.sh dest=/server/scripts
- name: crontab
cron: name="backup scripts" hour=01 minute=00 job="/usr/bin/bash /server/scripts/rsync_backup.sh &> /dev/null"
保存退出
3.rsync配置
(1)安装rsync
(2)配置
(3)启动
(4)脚本
(5)计划任务
vim /etc/ansible/ansible_playbook/rsync.yaml
添加:
- hosts: rsync
tasks:
- name: install rsync
yum: name=rsync state=installed
- name: config rsync
copy: src=/etc/ansible/ansible_playbook/conf/rsyncd.conf dest=/etc/rsyncd.conf
notify: restart rsync
- name: create rsync local user
copy: content='rsync_backup:1' dest=/etc/rsync.password mode=600
- name: create data
file: path=/data state=directory recurse=yes owner=www group=www mode=755
- name: create backup
file: path=/backup state=directory recurse=yes owner=www group=www mode=755
- name: start rsync
service: name=rsyncd state=started enabled=yes
- name: push check scripts
copy: src=./scripts/rsync_check.sh dest=/server/scripts
- name: crond check scripts
cron: name="check scripts" hour=05 minute=00 job="/usr/bin/bash /server/scripts/rsync_check.sh &> /dev/null"
handlers:
- name: restart rsync
service: name=rsyncd state=restarted
保存退出
4.nfs部署
(1)安装nfs-utils
(2)配置
(3)启动
vim /etc/ansible/ansible_playbook/nfs.yaml
添加:
- hosts: nfs
tasks:
- name: install nfs
yum: name=nfs-utils,rpcbind state=installed
- name: config nfs
copy: src=./conf/exports dest=/etc/exports
notify: restart nfs
- name: create data
file: path=/data state=directory recurse=yes owner=www group=www mode=755
- name: start nfs
service: name=nfs-server state=started enabled=yes
handlers:
- name: restart nfs
service: name=nfs-server state=restarted
保存退出
5.sersync部署
(1)在ansible服务器先下载sersync
(2)解压到/etc/ansible/ansible_playbook/并修改配置文件
(3)推送到nfs
(4)启动sersync
vim /etc/ansible/ansible_playbook/sersync.yaml
添加:
- hosts: nfs
tasks:
- name: scp sersync
copy: src=./tools/sersync/ dest=/usr/local/sersync owner=www group=www mode=755
- name: start sersync
shell: pgrep sersync;
[ $? -eq 0 ] || /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
保存退出
6.web部署
(1)本地安装httpd
(2)修改配置文件,复制到/etc/ansible/ansible_playbook/conf
(3)挂载
(4)启动
vim /etc/ansible/ansible_playbook/web.yaml
添加:
- hosts: web
tasks:
- name: install httpd
yum: name=httpd state=installed
- name: mount nfs
mount: src=nfs:/data path=/var/www/html fstype=nfs state=mounted
- name: config httpd
copy: src=./conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: restart httpd
- name: start httpd
service: name=httpd state=started enabled=yes
handlers:
- name: restart httpd
service: name=httpd state=restarted
保存退出
7.main.yaml
vim main.yaml
添加:
- import_playbook: base.yaml
- import_playbook: rsync.yaml
- import_playbook: nfs.yaml
- import_playbook: sersync.yaml
- import_playbook: web.yaml
保存退出
预检测:ansible-playbook -C main.yaml
执行: ansible-playbook main.yaml
------------------------------------
ansible roles扩展参考:
https://blog.csdn.net/woshizhangliang999/article/details/106005990/