Ansible-下部

2019-11-21 分类:DevOps 阅读(185) 评论(0)

ansible-playbook

playbook是由一个或多个模块组成的,使用多个不同的模块,完成一件事情。

ansible软件特点

可以实现批量管理
可以实现批量部署
ad-hoc(批量执行命令)---针对临时性的操作
    ansible clsn -m command -a "hostname"   <- 批量执行命令举例
编写剧本-脚本(playbook)---针对重复性的操作

ansible核心功能

pyYAML-----用于ansible编写剧本所使用的语言格式(saltstack---python) 
rsync-ini语法  sersync-xml语法  ansible-pyYAML语法
paramiko---远程连接与数据传输  
Jinja2-----用于编写ansible的模板信息

剧本编写规则说明

YAML三板斧

缩进

YAML使用一个固定的缩进风格表示层级结构,每个缩进由两个空格组成, 不能使用tabs

冒号

以冒号结尾的除外,其他所有冒号后面所有必须有空格。

短横线

表示列表项,使用一个短横杠加一个空格。
多个项使用同样的缩进级别作为同一列表。

剧本书写格式

- hosts: 172.16.1.7       处理指定服务器         (空格)hosts:(空格)172.16.1.7
task:                剧本所要干的事情;              (空格)(空格)task:
- name:            (两个空格)-(空格)name:
command: echo hello clsn linux   (四个空格)command:(空格)
剧本格式示例
[root@m01 ansible-playbook]# vim rsync.yaml
- hosts: 172.16.1.41
tasks:
- name: Install Rsync
yum: name=rsync state=installed

剧本检查方法

ansible-playbook --syntax-check 01.yml
--- 进行剧本配置信息语法检查
ansible-playbook -C 01.yml
--- 模拟剧本执行(彩排)

语法检查

ansible-playbook --syntax-check
[root@m01 ansible-playbook]# ansible-playbook --syntax-check 01.yml
playbook: 01.yml

模拟剧本执行

ansible-playbook -C
[root@m01 ansible-playbook]# ansible-playbook -C 01.yml
PLAY [all] ****************************************************************

TASK [Gathering Facts] ****************************************************
ok: [172.16.1.41]
ok: [172.16.1.8]
此处省略………..
PLAY RECAP ****************************************************************
172.16.1.31                : ok=2    changed=0    unreachable=0    failed=0
172.16.1.41                : ok=2    changed=0    unreachable=0    failed=0
172.16.1.8                 : ok=2    changed=0    unreachable=0    failed=0

剧本示例

剧本编写内容扩展:剧本任务编写多个任务

- hosts: all
tasks:
- name: restart-network
cron: name='restart network' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov &gt;/dev/null 2&gt;&amp;1'
- name: sync time
cron: name='sync time' minute=*/5 job="/usr/sbin/ntpdate pool.ntp.com &gt;/dev/null 2&gt;&amp;1"
剧本编写内容扩展:剧本任务编写多个主机
- hosts: 172.16.1.7
tasks:
- name: restart-network
cron: name='restart network' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov &gt;/dev/null 2&gt;&amp;1'
- name: sync time
cron: name='sync time' minute=*/5 job="/usr/sbin/ntpdate pool.ntp.com &gt;/dev/null 2&gt;&amp;1"
- hosts: 172.16.1.31
tasks:
- name: show ip addr to file
shell: echo $(hostname -i) &gt;&gt; /tmp/ip.txt

剧本编写方式

多主机单任务编写方式
多主机多任务编写方式
不同主机多任务编写方式

Ansible项目案例

环境规划

全网备份
实时备份

角色 外网IP(NAT) 内网IP(LAN) 部署软件
m01 eth0:10.0.0.61 eth1:172.16.1.61 ansible
backup  eth0:10.0.0.41 eth1:172.16.1.41 rsync
nfs eth0:10.0.0.31 eth1:172.16.1.31 nfs、Sersync
web01 eth0:10.0.0.7 eth1:172.16.1.7  httpd

目录规划

[root@m01 ~]# mkdir /etc/ansible/ansible_playbook/{file,conf,scripts} -p
[root@m01 ~]# tree /etc/ansible/ansible_playbook/
/etc/ansible/ansible_playbook/
├── conf
└── file
└── scripts

需提前准备好的文件

rsync配置文件

准备对应的配置文件存放至/etc/ansible/ansible_playbook/conf/

[root@m01 conf]# cat /etc/ansible/ansible_playbook/conf/rsyncd.conf
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.password
log file = /var/log/rsyncd.log
#####################################
[backup]
path = /backup

[data]
path = /data

nfs配置文件

准备nfs配置文件exports

[root@m01 ansible_playbook]# cat /etc/ansible/ansible_playbook/conf/nfs_exports
/data/ 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)

Sersync软件包

下载Sersync软件包

[root@m01 ansible_playbook]# ll /etc/ansible/ansible_playbook/file/
-rw-r--r-- 1 root root 727290 Aug  1 12:04 sersync.tar.gz

sersync配置文件

准备sersync实时同步的配置文件

[root@m01 ansible_playbook]# cat /etc/ansible/ansible_playbook/conf/confxml.xml.nfs
<!--?xml version="1.0" encoding="ISO-8859-1"?-->

<host hostip="localhost" port="8008"></host>
<debug start="false">
<filesystem xfs="true">
<filter start="false">
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify>
<delete start="true">
<createfolder start="true">
<createfile start="true">
<closewrite start="true">
<movefrom start="true">
<moveto start="true">
<attrib start="false">
<modify start="false">
</modify></attrib></moveto></movefrom></closewrite></createfile></createfolder></delete></inotify>
<sersync>
<localpath watch="/data">
<remote ip="172.16.1.41" name="data">
</remote></localpath>
<rsync>
<commonparams params="-az">
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.pass">
<userdefinedport start="false" port="874"><!-- port=874 -->
<timeout start="true" time="100"><!-- timeout=100 -->
<ssh start="false">
</ssh></timeout></userdefinedport></auth></commonparams></rsync>
<faillog path="/tmp/rsync_fail_log.sh" timetoexecute="60"><!--default every 60mins execute once-->
<crontab start="false" schedule="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
    <plugin start="false" name="command">
    </plugin></faillog></sersync>
    <plugin name="command">
    <param prefix="/bin/sh" suffix="" ignoreerror="true">  <!--prefix /opt/tongbu/mmm.sh suffix-->
<filter start="false">
<include expression="(.*)\.php">
<include expression="(.*)\.sh">
</include></include></filter>
    </plugin>
    <plugin name="socket">
    <localpath watch="/opt/tongbu">
<deshost ip="192.168.138.20" port="8009">
</deshost></localpath>
    </plugin>
    <plugin name="refreshCDN">
    <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx">
<sendurl base="http://pic.xoyo.com/cms">
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images">
</regexurl></sendurl></cdninfo></localpath>
    </plugin>
</filesystem></debug>

基础环境部署

基础环境:所有机器统一的配置
1.需要关闭firewalld以及selinux、epel仓库、ssh端口、优化基础配置
2.需要安装rsync和nfs-utils
3.准备www用户
4.需要准备/etc/rsync.pass密码文件
5.需要准备全网备份脚本
基础的playbook剧本

[root@m01 ansible_playbook]# cat base.yaml
- hosts: all
tasks:

- name: Install Epel Repos
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 group=666 create_home=no shell=/sbin/nologin

- name: Create Rsync_Client_Pass
copy: content='1' dest=/etc/rsync.pass mode=600

- name: Create Scripts Directory
file: path=/server/scripts recurse=yes state=directory

- name: Push File Scripts
copy: src=./scripts/rsync_backup_md5.sh dest=/server/scripts/

- name: Crontable Scripts
cron: name="backup scripts" hour=01 minute=00 job="/bin/bash /server/scripts/rsync_backup_md5.sh &amp;&gt;/dev/null"

应用环境:Rsync

1.安装rsync
2.配置rsync(配置变更,一定要进行重载操作)
3.创建虚拟用户,权限调整
4.创建目录/data/  /backup
5.启动rsync
6.配置邮箱->邮箱的发件人->校验的脚本

[root@m01 ansible_playbook]# cat rsync.yaml
- hosts: backup
tasks:
- name: Installed Rsync Server
yum: name=rsync,mailx state=installed

- name: configure Rsync Server
copy: src=./conf/rsyncd.conf dest=/etc/rsyncd.conf
notify: Restart Rsync Server

- name: Create Virt 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 RsyncServer
service: name=rsyncd state=started enabled=yes

- name: Push Check Scripts
copy: src=./scripts/rsync_check_backup.sh dest=/server/scripts/

- name: Crond Check Scripts
cron: name="check scripts" hour=05 minute=00 job="/bin/bash /server/scripts/rsync_check_backup.sh &amp;&gt;/dev/null"

应用环境:NFS

1.安装nfs-utils
2.配置nfs     (当修改了配置,触发重载操作)
3.创建目录,授权
4.启动

[root@m01 ansible_playbook]# cat nfs.yaml
- hosts: nfs
tasks:

- name: Installed Nfs Server
yum: name=nfs-utils state=installed

- name: Configure Nfs Server
copy: src=./conf/exports dest=/etc/exports
notify: Restart Nfs Server

- name: Create Share Data
file: path=/data state=directory recurse=yes owner=www group=www mode=755

- name: Start Nfs Server
service: name=nfs-server state=started enabled=yes

handlers:
- name: Restart Nfs Server
service: name=nfs-server state=restarted

应用环境:Sersync

1.下载sersync
2.解压,改名,配置
3.启动

[root@m01 ansible_playbook]# cat sersync.yaml
- hosts: nfs
tasks:
- name: Scp Sersync
copy: src=./file/sersync2.5.4_64bit_binary_stable_final.tar.gz dest=/usr/local/sersync.tar.gz

- name: Zip
shell: cd /usr/local &amp;&amp; tar xf sersync.tar.gz &amp;&amp; mv GNU-Linux-x86 sersync
args:
creates: /usr/local/sersync

- name: configure Sersync
copy: src=./conf/confxml.xml dest=/usr/local/sersync/

- name: Start Sersync
shell: /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml

应用环境:WEB

挂载nfs共享的目录

[root@m01 ansible_playbook]# cat web.yaml
- hosts: web
tasks:

- name: Mount NFS Server Share Data
mount: src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=mounted

包含include

[root@m01 ansible_playbook]# cat mail.yaml
- import_playbook: base.yaml
- import_playbook: rsync.yaml
- import_playbook: nfs.yaml
- import_playbook: sersync.yaml
- import_playbook: web.yaml
标签:

您可能也喜欢:

Ansible-中部

管理前操作 1.机器还原快照(firewalld、selinux、配置好仓库) 选择虚拟机-》快照-》恢复 2.推送你的公钥 [root@m01 ~]# sshpass -p1 ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.41 3.配置Ansibl...

more

Ansible-上部

Ansible概述 Ansible是一个配置管理系统configuration management system python 语言是运维人员必须会的语言 ansible 是一个基于python 开发的自动化运维工具 其功能实现基于ssh远程连接...

more

ERROR: Unrecognized command line argument: ‘use’

Unrecognized command line argument: 'use' gvm--GoLang语言多版本管理工具 基础环境 centos6.5 报错内容 gvm在命令行以外的任何地方调用 gvm use 都会报错 gvm use go1.13.4 ERROR: Unrecog...

more

欢迎新朋友你的到来!
还没有人抢沙发呢~
昵称
邮箱
网站

切换注册

登录

忘记密码 ?

切换登录

注册