学习ansible常用模块这篇就够了(剧本)

目录

 Ansible概述

配置Ansible

安装ansible

配置清单

ssh秘钥对访问

Ansible模块

command

shell   

yum

copy

service(或systemd)

group

user

file

案例1

mount

案例2

script

playbook剧本

playbook语法结构

服务器配置

基础环境部署

rsync配置

nfs部署

web部署

执行剧本


个人主页:大虾好吃吗

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

        实验环境:打开四台Centos7,根据拓扑图修改IP、修改主机名。

拓扑图如下:

学习ansible常用模块这篇就够了(剧本)_第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

[root@ansible ~]# yum -y install ansible

 查看版本

配置清单

1. 修改配置hosts文件

[root@ansible ~]# vim /etc/hosts
192.168.1.4 ansible
192.168.1.5 web
192.168.1.6 nfs
192.168.1.7 rsync

2. 编辑清单

[root@ansible ~]# vim /etc/ansible/hosts
添加:
[web]
192.168.1.5
[nfs]
192.168.1.6
[rsync]
192.168.1.7
[benet:children] //组内成员
web
nfs
rsync

ssh秘钥对访问

        ansible服务器配置密钥对访问。

[root@ansible ~]# ssh-keygen -t rsa //执行后按三次回车键
[root@ansible ~]# ssh-copy-id [email protected]
[root@ansible ~]# ssh-copy-id [email protected]
[root@ansible ~]# ssh-copy-id [email protected]

Ansible模块

调用模块颜色显示:

黄色  更改成功

绿色  没有更改

红色  错误

紫色  警告

        列出所有模块

[root@ansible ~]# ansible-doc --list

        查看ansible模块帮助

[root@ansible ~]# ansible-doc yum 

command

        仅支持简单语法命令,但语句中不能包含管道符等复杂元素。

1. ansible查看web主机的主机名称

[root@ansible ~]# ansible web -m command -a "hostname"

192.168.1.5 | CHANGED | rc=0 >>

web

2. ansible创建web主机用户zhangsan

[root@ansible ~]# ansible web -m command -a "useradd zhangsan"

192.168.1.5 | CHANGED | rc=0 >>

        查看已创建用户zhangsan

shell   

        command的升级版,支持复杂语句,但不支持别名。正常情况下使用shell就可以了,command可以忽略,了解就行。

[root@ansible ~]# ansible web -m shell -a "echo 123 |passwd --stdin zhangsan"

192.168.1.5 | CHANGED | rc=0 >>

更改用户 zhangsan 的密码 。

passwd:所有的身份验证令牌已经成功更新。

yum

        web主机yum安装nginx服务。

[root@ansible ~]# ansible web -m yum -a "name=nginx state=installed"

注释:

name                           安装的软件包名

state                            服务状态

installed,present         安装软件包

removed,absent         卸载软件包

latest                          安装最新软件包

copy

        复制ansible本地hosts文件到benet组的所有主机。

[root@ansible ~]# ansible benet -m copy -a "src=/etc/hosts  dest=/etc/hosts backup=yes"

注释:

src                       源文件路径

dest                     目标文件路径

backup                覆盖到目标文件前,是否提前备份

content               添加文件内容

group                  指定属组

owner                 指定属主

mode                  指定权限

service(或systemd)

        关闭web主机的nginx服务,实现开机自启。service/systemd两种方法都可以,个人推荐systemd。

[root@ansible ~]# ansible web -m service -a "name=nginx state=stopped enabled=yes"

注释:

name                       指定服务名

state                        指定服务运行状态

started                     开启服务

stopped                    关闭服务

reloaded                   重载服务

restarted                   重启服务

enabled                    是否开机自启

group

1. 在所有清单主机上创建组www,gid 666

[root@ansible ~]# ansible all -m group -a "name=www gid=666"

2. 在所有清单主机删除组www

[root@ansible ~]# ansible all -m group -a "name=www gid=666 state=absent"

user

1. 所有主机创建用户zhangsan

[root@ansible ~]# ansible all -m user -a "name=zhangsan"

2. 给web主机的zhangsan用户设置密码

[root@ansible ~]# ansible web -m shell -a "echo 123 |passwd --stdin zhangsan"

192.168.1.5 | CHANGED | rc=0 >>

更改用户 zhangsan 的密码 。

passwd:所有的身份验证令牌已经成功更新。

file

1. 创建backup目录,并赋权,更改属主属组

[root@ansible ~]# ansible rsync -m file -a "path=/backup owner=root group=root recurse=yes mode=777"

        recurse=yes 时表示可以递归地修改目录中文件的属性。

2. 创建test.txt文件,file模块可以创建目录又能创建文件

[root@ansible ~]# ansible rsync -m file -a "path=/test.txt owner=root group=root state=touch  mode=777"

案例1

        在ansible上远程配置rsync服务

1. 修改rsync配置文件,并传到rsync服务器

[root@ansible ~]# mkdir /etc/ansible/conf
[root@ansible ~]# cd /etc/ansible/conf
[root@ansible conf]# cp /etc/rsyncd.conf ./
[root@ansible conf]# vim rsyncd.conf 
uid = root
gid = root
port 873
address = 192.168.1.7
hosts allow = 192.168.1.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
[root@ansible conf]# ansible rsync -m copy -a "src=rsyncd.conf dest=/etc/rsyncd.conf backup=yes"

2. 启动rsync服务

[root@ansible conf]# ansible rsync -m systemd -a "name=rsyncd state=restarted"

3. 创建目录,并赋权,更改属主属组

[root@ansible conf]# ansible rsync -m file -a "path=/backup owner=root group=root recurse=yes mode=777"

4. 配置rsync服务器的密码文件:

[root@ansible conf]# ansible rsync -m copy -a "content='rsync_backup:1' dest=/etc/rsync.password owner=root group=root mode=600"
验证文件
[root@ansible conf]# ansible rsync -m shell -a "ls -l /etc/rsync.password"
192.168.1.7 | CHANGED | rc=0 >>
-rw------- 1 root root 14 11月 16 18:38 /etc/rsync.password
[root@ansible conf]# ansible rsync -m shell -a "cat /etc/rsync.password"
192.168.1.7 | CHANGED | rc=0 >>
rsync_backup:1

5. 配置所有服务器的rsync连接密码文件

[root@ansible conf]# ansible benet -m copy -a "content='1' dest=/etc/server.pass owner=root group=root mode=600"

6. 测试:备份WEB的httpd.conf配置文件

[root@ansible conf]# ansible web -m shell -a "rsync -avz --password-file=/etc/server.pass /etc/nginx/nginx.conf rsync_backup@rsync::backup"

mount

        挂载模块,详情请看下面案例。

[root@ansible ~]# ansible web -m mount -a "src=nfs:/nfs path=/usr/share/nginx/html fstype=nfs state=mounted"

注释:

           state 挂载状态

           mounted    挂载

           unmounted 卸载

案例2

        在ansible上远程部署nfs并远程挂载。

[root@ansible ~]# ansible nfs -m file -a "path=/nfs owner=root group=root recurse=yes mode=777"
[root@ansible ~]# vim /etc/exports
/nfs 192.168.1.0/24(rw,sync,no_root_squash)
[root@ansible ~]# ansible nfs -m copy -a "src=/etc/exports dest=/etc/exports"
[root@ansible ~]# ansible nfs -m systemd -a "name=nfs state=restarted"
[root@ansible ~]# ansible nfs -m systemd -a "name=rpcbind state=restarted"

        挂载nfs目录到web下的/var/www/html

[root@ansible ~]# ansible web -m mount -a "src=nfs:/nfs path=/usr/share/nginx/html fstype=nfs state=mounted"

注释:

 state              挂载状态

mounted         挂载

unmounted     卸载

script

        在ansible上编写测试脚本,指定web主机执行。

[root@ansible ~]# ansible web -m script -a "/test.sh"

playbook剧本

        是由一个或多个模块组成,完成统一的目的,实现自动化操作,剧本编写遵循yaml语法。

yaml的三要素:

1. 缩进:两个字符,默认的tab键是八个字符,所以要使用tab键,需要修改.vimrc。

[root@ansible ~]# vim .vimrc

set tabstop=2 //添加该行内容指定2字符

2. 冒号:冒号后面需要空格,除非以冒号结尾。

3. 短横杠:列表项,后面跟空格。

playbook语法结构

ansible-playbook  选项  文件路径

选项参数如下:

-C  模拟预运行

--list-hosts:列出清单

--list-tasks:列出任务

--list-tags:列出标签

--syntax-check:语法检查

        测试案例:通过playbook安装nginx,复制网页内容。

        前提条件:使用ansible安装需要有该服务的配置文件,如没有需要下载服务并配置,配置后才可以发送过去。

[root@ansible ~]# yum -y install nginx
[root@ansible ~]# echo "192.168.1.5" > /usr/share/nginx/html/index.html
[root@ansible ~]# vim nginx.yaml
- hosts: web

  tasks:
    - name: install nginx
      yum:  name=nginx state=latest

    - name: config nginx
      copy: src=/usr/share/nginx/html/index.html dest=/usr/share/nginx/html/
      notify: restart nginx
    
    - name: start nginx
      systemd: name=nginx state=started
    
  handlers:
    - name: restart nginx
      systemd: name=nginx state=restarted
[root@ansible ~]# ansible-playbook -C nginx.yaml //测试
[root@ansible ~]# ansible-playbook  nginx.yaml //执行

        执行后访问web服务器(192.168.1.5),可以看到修改后的网页。以上就是一套安装nginx并配置的全部流程,如需修改配置文件只需要ansible修改后使用copy命令即可。

        playbook配置web--nfs--rsync架构环境

        全局环境:修改各主机名:ansible、web、nfs、rsync。

拓扑图如下:

学习ansible常用模块这篇就够了(剧本)_第2张图片

服务器配置

1. 添加hosts文件

[root@ansible ~]# vim /etc/hosts
192.168.1.4 ansible
192.168.1.5 web
192.168.1.6 nfs
192.168.1.7 rsync

2. 安装ansible

[root@ansible ~]# yum -y install epel-release

[root@ansible ~]# yum -y install ansible

3. ssh公钥

[root@ansible ~]# ssh-keygen //按三次回车,保持默认

[root@ansible ~]# ssh-copy-id root@web //web服务器

[root@ansible ~]# ssh-copy-id root@nfs //nfs服务器

[root@ansible ~]# ssh-copy-id root@rsync //rsync服务器

4. 复制/etc/hosts到被管理端

[root@ansible ~]# scp /etc/hosts root@web:/etc

[root@ansible ~]# scp /etc/hosts root@nfs:/etc  

[root@ansible ~]# scp /etc/hosts root@rsync:/etc

5. 创建ansible清单

[root@ansible ~]# vim /etc/ansible/hosts
[web]
web
[nfs]
nfs
[backup]
rsync

6. 创建ansible目录

[root@ansible ~]# mkdir -p /etc/ansible/ansible_playbook/{conf,scripts}

7. 准备配置文件

        conf下准备三个文件,分别为web配置文件,(web配置文件从nginx下复制过来,提前根据需求修改好内容),nfs配置文件,rsync配置文件。

[root@ansible ~]# cd /etc/ansible/ansible_playbook/conf/

[root@ansible conf]# vim exports
/data 192.168.1.0/24(rw,sync,all_squash)
[root@ansible conf]# vim rsyncd.conf
uid = nobody
gid = nobody
port 873
address = 192.168.1.7
hosts allow = 192.168.1.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

        scripts目录下添加两个脚本文件

[root@ansible conf]# cd /etc/ansible/ansible_playbook/scripts/
[root@ansible scripts]# vim rsync_backup.sh
#!/usr/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
#1.定义变量
Host=$(hostname)
Addr=$(ifconfig ens33|awk 'NR==2{print $2}')
Date=$(date +%F)
Dest=${Host}_${Addr}_${Date}
Path=/backup

#2.创建备份目录
[ -d $Path/$Dest ] || mkdir -p $Path/$Dest

#3.备份对应的文件
cd / && \
[ -f $Path/$Dest/system.tar.gz ] || tar czf $Path/$Dest/system.tar.gz etc/fstab etc/rsyncd.conf && \
[ -f $Path/$Dest/log.tar.gz ] || tar czf $Path/$Dest/log.tar.gz  var/log/messages var/log/secure && \

#4.携带md5验证信息
[ -f $Path/$Dest/flag ] || md5sum $Path/$Dest/*.tar.gz >$Path/$Dest/flag_${Date}

#5.推送本地数据至备份服务器
export RSYNC_PASSWORD=1
rsync -avz $Path/ rsync_backup@rsync1::backup

#6.本地保留最近7天的数据
find $Path/ -type d -mtime +7|xargs rm -rf
[root@ansible scripts]# vim rsync_check.sh
#!/usr/bin/bash

#1.定义全局的变量
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

#2.定义局部变量
Path=/backup
Date=$(date +%F)

#3.查看flag文件,将校验的结果保存至result_时间
find $Path/*_${Date} -type f -name "flag$Date"  >$Path/result_${Date}

#4.将校验的结果发送邮件给管理员
mail -s "Rsync Backup $Date" [email protected] <$Path/result_${Date}

#5.删除超过7天的校验结果文件, 删除超过180天的备份数据文件
find $Path/ -type f -name "result*" -mtime +7|xargs rm -f
find $Path/ -type d -mtime +180|xargs rm -rf

基础环境部署

(1)网络环境(关闭firewall  selinux)

(2)epel仓库

(3)安装rsync,nfs-utils

(4)创建组

(5)创建用户

(6)创建目录,并修改权限

(7)推送脚本

(8)推送rsync客户端密码文件,修改权限

(9)计划任务

[root@ansible ~]# vim /etc/ansible/ansible_playbook/base.yaml
- hosts: all
  tasks:

    - 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"

rsync配置

(1)安装rsync

(2)配置

(3)启动

(4)脚本

(5)计划任务

[root@ansible ~]# 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

nfs部署

(1)安装nfs-utils

(2)配置

(3)启动

[root@ansible ~]# vim /etc/ansible/ansible_playbook/nfs.yaml
- hosts: nfs
  tasks:

    - name: install nfs
      yum: name=nfs-utils 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

web部署

(1)本地安装httpd

(2)修改配置文件,复制到/etc/ansible/ansible_playbook/conf

(3)挂载

(4)启动

[root@ansible ~]# vim /etc/ansible/ansible_playbook/web.yaml
- hosts: web

  tasks:
    - name: mount nfs
      mount: src=nfs:/data path=/data fstype=nfs state=mounted

    - name: install nginx
      yum: name=nginx state=installed

    - name: config nginx
      copy: src=conf/nginx.conf dest=/etc/nginx/nginx.conf
      notify: restart nginx

    - name: start nginx
      service: name=nginx state=started enabled=yes

  handlers:
    - name: restart nginx
      service: name=nginx state=restarted

执行剧本

[root@ansible ~]# cd /etc/ansible/ansible_playbook/
[root@ansible ansible_playbook]# vim main.yaml
- import_playbook: base.yaml
- import_playbook: rsync.yaml
- import_playbook: nfs.yaml
- import_playbook: web.yaml


[root@ansible ansible_playbook]# ansible-playbook -C main.yaml          #测试
[root@ansible ansible_playbook]# ansible-playbook main.yaml             #执行

  

你可能感兴趣的:(自动化运维,linux,运维,ssh)