第九章day40 ansible 模块

1、

    #vim /etc/ansible/yaml/back.yml
    - hosts: siyi
      tasks:
         - name: "yum rsync"
           yum: name=rsync state=installed
         - name: "配置rsync模块"
           copy: backup=yes src=/etc/rsyncd.conf dest=/etc/rsyncd.conf
         - name: "创建rsync用户"
           user: name=rsync
         - name: "创建backup目录"
           file: name=/backup state=directory owner=rsync group=rsync
         - name: "创建/server/tools/目录"
           file: name=/server/tools/ state=directory owner=root group=root
         - name: "开启rsync服务"
           service: name=rsyncd state=started enabled=yes
         - name: "创建密码文件"
           file:  name=/etc/rsync.password state=touch mode=600
         - name: "添加密码"
           blockinfile: path=/etc/rsync.password block='rsync_backup:oldboy'
    - hosts: sanyi
      tasks:
         - name: "yum rsync"
           yum: name=rsync  state=installed
         - name: "创建data目录"
           file: name=/data state=directory
         - name: "创建密码文件"
           file:  name=/etc/rsync.password state=touch mode=600
         - name: "添加密码"
           blockinfile: path=/etc/rsync.password block='oldboy'
         - name: "添加bashrc"
           blockinfile: path=/etc/bashrc block='export RSYNC_PASSWORD=oldboy'
         - name: "执行bashrc"
           shell: source /etc/bashrc
         - name: "推送测试"
           command: rsync -az /tmp [email protected]::backup/
         - name: "解压监控包"
           command: tar -xf /server/tools/application.tar.gz -C /server/tools/
         - name: "创建日志目录"
           file: name=/application/logs/ state=directory 
         - name: "创建日志文件"
           file:  name=/application/logs/rsync_fail_log.sh state=touch
         - name: "执行监控"
           command: /server/tools/application/bin/sersync2 -d -o /server/tools/application/conf/confxml.xml
         - name: "yum nfs"
           yum: name=nfs-utils state=installed
         - name: "yum rpcbind"
           yum: name=rpcbind state=installed
         - name: "开启rpcbind服务"
           service: name=rpcbind state=started enabled=yes
         - name: "开启nfs服务"
           service: name=nfs state=started enabled=yes
         - name: "添加共享目录"
           blockinfile: path=/etc/exports block='/data 172.16.1.0/24(rw,sync)'
         - name: "重启nfs服务"
           service: name=nfs state=restarted
         - name: "修改data属主"
           file:  name=/data owner=nfsnobody group=nfsnobody
    - hosts: oldboy
      tasks:
         - name: "yum nfs-utils rpcbind"
           yum: name=nfs-utils state=installed
         - name: "yum nfs-utils rpcbind"
           yum: name=rpcbind state=installed
         - name: "开启rpcbind服务"
           service: name=rpcbind state=started enabled=yes
         - name: "挂载"
           mount: src='172.16.1.31:/data' state=mounted fstype=nfs name=/mnt 
         - name: "添加执行权限"
           file:  name=/etc/rc.local  mode=755
         - name: "添加密码"
           blockinfile: path=/etc/rc.local block='/usr/bin/mount -t nfs 172.16.1.31:/data /mnt'

4、m01的/server/scripts/目录下

        # vim /server/scripts/mokuai.sh
        #!/bin/bash
        cd /root/.ssh/ &&\
        rm -f * &&\
        /bin/ssh-keygen -t rsa -f /root/.ssh/id_rsa  -P "" &>/dev/null
        yum install -y sshpass >/dev/null 2>&1
        for n in  7 8  31 41
        do 
        sshpass -p123456 ssh-copy-id -i ~/.ssh/id_rsa.pub 172.16.1.$n -o StrictHostKeyChecking=no &>/dev/null
          if [ $? -eq 0 ]
          then
            echo "172.16.1.$n Distribute successfully "
              else
            echo "172.16.1.$n Distribute no successfully "
          fi
         done &&\
     scp -P 22 -rp /server/tools/application.tar.gz [email protected]:/server/tools/ &>/dev/null &&\
    cp /etc/ansible/hosts{,.ori} &&\
    cat >/etc/ansible/hosts</etc/rsyncd.conf<

内网下Yum仓库搭建配置

1、实验环境

虚拟机环境:

  • VMware 12 版本虚拟机

网络环境:

  • 内网 IP 段:172.16.1.0
  • 外网 iP 段(模拟):10.0.0.0

实验基础:(能够上网,有原本yum源)

  • m01:外网 IP 10.0.0.61;内网 IP :172.16.1.61
  • web01:外网 IP 10.0.0.7;内网 IP :172.16.1.7

Linux版本:

  • CentOS Linux release 7.6.1810
  • 3.10.0-957.10.1.el7.x86_64

提示:本无需外网,因个人原因而添加,真实环境只需内网即可,有本地yum仓库的可认为是服务端,其他主机作为同样也能像本案例中的web01一样作为客户端

2、实验步骤

2.1、m01:制作本地yum(服务端)

**1)将CentOS 7 的镜像装入CD/DVD中
2)将/mnt目录作为挂载点,挂载(也可挂载其他目录中)然后查看里面的文件

--------------------------------------------------------------
==>命令如下<==
--------------------------------------------------------------
mount /dev/cdrom /mnt/

df -h|grep 'mnt'

cd /mnt/

ll


--------------------------------------------------------------
==>演示如下<==
--------------------------------------------------------------
[root@m01 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载

[root@m01 ~]# df -h|grep 'mnt'
/dev/sr0        4.3G  4.3G     0  100% /mnt

[root@m01 ~]# cd /mnt/
[root@m01 /mnt]# 
[root@m01 /mnt]# ll
总用量 686
-rw-rw-r-- 1 root root     14 11月 26 00:01 CentOS_BuildTag
drwxr-xr-x 3 root root   2048 11月 26 00:20 EFI
###省略###

3)创建安装包存放的目录

--------------------------------------------------------------
==>命令如下<==
--------------------------------------------------------------
mkdir -p /var/ftp/pub/Packages


--------------------------------------------------------------
==>演示如下<==
--------------------------------------------------------------
[root@m01 /mnt]# mkdir -p /var/ftp/pub/Packages

4)复制ISO中的所有文件(时间比较久,需要等待一段时间)完成后查看确认

--------------------------------------------------------------
==>命令如下<==
--------------------------------------------------------------
cp -a /mnt/* /var/ftp/pub

cd /var/ftp/pub/ 

ls -rtlh


--------------------------------------------------------------
==>演示如下<==
--------------------------------------------------------------
[root@m01 /mnt]# cp -a /mnt/* /var/ftp/pub
[root@m01 /mnt]# cd /var/ftp/pub/ 
[root@m01 /var/ftp/pub]# ls -rtlh
总用量 320K
-rw-rw-r-- 1 root root 1.7K 12月 10 2015 RPM-GPG-KEY-CentOS-Testing-7
-rw-rw-r-- 1 root root 1.7K 12月 10 2015 RPM-GPG-KEY-CentOS-7
-rw-rw-r-- 1 root root  18K 12月 10 2015 GPL
###省略###

5)安装vsftp服务,启动和设置开机启动

--------------------------------------------------------------
==>命令如下<==
--------------------------------------------------------------
yum install -y vsftpd

systemctl start vsftpd

systemctl enable vsftpd


--------------------------------------------------------------
==>演示如下<==
--------------------------------------------------------------
[root@m01 /var/ftp/pub]# yum install -y vsftpd
###省略###
已安装:
  vsftpd.x86_64 0:3.0.2-25.el7                                                                                                                     

完毕!
[root@m01 /var/ftp/pub]# systemctl start vsftpd
[root@m01 /var/ftp/pub]# systemctl enable vsftpd
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.

6)安装createrepo软件包

--------------------------------------------------------------
==>命令如下<==
--------------------------------------------------------------
cd /var/ftp/pub/Packages

yum install -y createrepo


--------------------------------------------------------------
==>演示如下<==
--------------------------------------------------------------
[root@m01 /var/ftp/pub]# cd /var/ftp/pub/Packages
[root@m01 /var/ftp/pub/Packages]# yum install -y createrepo
###省略###
已安装:
  createrepo.noarch 0:0.9.9-28.el7                                                                                                                 

作为依赖被安装:
  deltarpm.x86_64 0:3.6-3.el7                                          python-deltarpm.x86_64 0:3.6-3.el7                                         

完毕!

7)上传并拷贝安装的软件所需要的相关文件到Packges包下(例如oracle安装所需依赖包、docker安装所需依赖包等,这里不需要,可直接跳过)

8)创建Yum仓库(执行过程可能需要很长时间的等待)

--------------------------------------------------------------
==>命令如下<==
--------------------------------------------------------------
cd /var/ftp/pub 

createrepo .
#(注意后面的点)


--------------------------------------------------------------
==>演示如下<==
--------------------------------------------------------------
[root@m01 /var/ftp/pub/Packages]# cd /var/ftp/pub 
[root@m01 /var/ftp/pub]# createrepo .
Spawning worker 0 with 4021 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete

9)清除原有配置缓存,然后缓存yum源(需要一段时间)

--------------------------------------------------------------
==>命令如下<==
--------------------------------------------------------------
yum clean all

yum makecache


--------------------------------------------------------------
==>演示如下<==
--------------------------------------------------------------
[root@m01 /var/ftp/pub]# yum clean all
已加载插件:fastestmirror
正在清理软件源: base epel extras updates
Cleaning up list of fastest mirrors
[root@m01 /var/ftp/pub]# yum makecache
###省略###
(15/18): updates/7/x86_64/primary_db                                         | 3.4 MB  00:00:15     
(16/18): updates/7/x86_64/other_db                                             | 488 kB  00:00:01     
(17/18): updates/7/x86_64/filelists_db                                           | 3.0 MB  00:00:23     
元数据缓存已建立

2.2、web01:修改配置文件(客户端)

1)更改Yum配置文件,将不需要的yum配置文件放入备份文件夹中

--------------------------------------------------------------
==>命令如下<==
--------------------------------------------------------------
cd /etc/yum.repos.d/

mkdir bak

ls

 mv *repo* bak/

ls


--------------------------------------------------------------
==>演示如下<==
--------------------------------------------------------------
[root@web01 ~]# cd /etc/yum.repos.d/
[root@web01 /etc/yum.repos.d]# mkdir bak
[root@web01 /etc/yum.repos.d]# ls
bak                    CentOS7-Base-163.repo.1  CentOS-CR.repo         CentOS-fasttrack.repo  CentOS-Sources.repo  epel.repo
CentOS7-Base-163.repo  CentOS-Base.repo.backup  CentOS-Debuginfo.repo  CentOS-Media.repo      CentOS-Vault.repo
[root@web01 /etc/yum.repos.d]# mv *repo* bak/
[root@web01 /etc/yum.repos.d]# ls
bak

2)只保留CentOS-Media.repo配置文件

--------------------------------------------------------------
==>命令如下<==
--------------------------------------------------------------
mv bak/*M* .
#(注意后面的点)

ls


--------------------------------------------------------------
==>演示如下<==
--------------------------------------------------------------
[root@web01 /etc/yum.repos.d]# mv bak/*M* .
[root@web01 /etc/yum.repos.d]# ls
bak  CentOS-Media.repo

3)修改CentOS-Media.repo配置文件

--------------------------------------------------------------
==>命令如下<==
--------------------------------------------------------------
vim CentOS-Media.repo
#(需要会使用vim编辑器,若不会,使用下面一种方法)

cat >CentOS-Media.repo <演示如下<==
--------------------------------------------------------------
[root@web01 /etc/yum.repos.d]# cat >CentOS-Media.repo < [c7-media]
> name=CentOS-$releasever - Media
> baseurl=ftp://172.16.1.61/pub
> gpgcheck=0
> enabled=1
> gpgkey=ftp://172.16.1.61/pub/RPM-GPG-KEY-CentOS-7
> EOF
[root@web01 /etc/yum.repos.d]# cat CentOS-Media.repo 
[c7-media]
name=CentOS- - Media
baseurl=ftp://172.16.1.61/pub
gpgcheck=0
enabled=1
gpgkey=ftp://172.16.1.61/pub/RPM-GPG-KEY-CentOS-7

4)测试Yum仓库

--------------------------------------------------------------
==>命令如下<==
--------------------------------------------------------------
yum search docker


--------------------------------------------------------------
==>演示如下<==
--------------------------------------------------------------
[root@web01 /etc/yum.repos.d]# yum search docker
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
================================================================== 匹配:docker ===================================================================
virt-what.x86_64 : Detect if we are running in a virtual machine

至此,内网下Yum仓库搭建配置结束了,你,学会了吗?

你可能感兴趣的:(第九章day40 ansible 模块)