一、Ansible 主机信息模块

1.setup 模块

#1.获取web01主机所有信息
[root@m01 ~]# ansible web01 -m setup

#2.获取主机IP
[root@m01 ~]# ansible web01 -m setup -a 'filter=ansible_default_ipv4'

#3.获取主机名
[root@m01 ~]# ansible web01 -m setup -a 'filter=ansible_fqdn'
web01 | SUCCESS => {
    "ansible_facts": {
        "ansible_fqdn": "www.baidu.com", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

#4.获取内存信息
[root@m01 ~]# ansible web01 -m setup -a 'filter=ansible_memory_mb'
web01 | SUCCESS => {
    "ansible_facts": {
        "ansible_memory_mb": {
            "nocache": {
                "free": 720, 
                "used": 252
            }, 
            "real": {
                "free": 276, 
                "total": 972, 
                "used": 696
            }, 
            "swap": {
                "cached": 0, 
                "free": 1023, 
                "total": 1023, 
                "used": 0
            }
        }, 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

#5.常用参数
ansible_all_ipv4_addresses:仅显示ipv4的信息。
ansible_devices:仅显示磁盘设备信息。
ansible_distribution:显示是什么系统,例:centos,suse等。
ansible_distribution_major_version:显示是系统主版本。
ansible_distribution_version:仅显示系统版本。
ansible_machine:显示系统类型,例:32位,还是64位。
ansible_eth0:仅显示eth0的信息。
ansible_hostname:仅显示主机名(不准确)
ansible_fqdn:仅显示主机名。
ansible_kernel:仅显示内核版本。
ansible_lvm:显示lvm相关信息。
ansible_memtotal_mb:显示系统总内存。
ansible_memfree_mb:显示可用系统内存。
ansible_memory_mb:详细显示内存情况。
ansible_swaptotal_mb:显示总的swap内存。
ansible_swapfree_mb:显示swap内存的可用内存。
ansible_mounts:显示系统磁盘挂载情况。
ansible_processor:显示cpu个数(具体显示每个cpu的型号)。
ansible_processor_vcpus:显示cpu个数(只显示总的个数)。

二、使用模块加ad-hoc搭建交作业页面

1.准备文件

1)准备httpd配置文件
[root@m01 ~]# yum install -y httpd
[root@m01 ~]# vim /etc/httpd/conf/httpd.conf
User www
Group www
2)准备php安装包
[root@m01 ~]# ll
-rw-r--r--  1 root root 19889622 Nov 22 15:52 php.tar.gz
3)准备PHP配置文件
[root@m01 ~]# tar xf php.tar.gz -C /tmp/
[root@m01 tmp]# yum localinstall -y *.rpm
[root@m01 tmp]# vim /etc/php-fpm.d/www.conf
user = www
group = www
[root@m01 tmp]# vim /etc/php.ini
upload_max_filesize = 200M
post_max_size = 200M
4)准备代码文件
[root@m01 ~]# ll kaoshi.zip 
-rw-r--r-- 1 root root 26995 Nov 22 16:47 kaoshi.zip

2.编写ansible命令

#1.安装httpd
ansible web_group -m yum -a 'name=httpd state=present' &&\
#2.创建www用户组
ansible web_group,nfs -m group -a 'name=www gid=666 state=present' &&\
#3.创建www用户
ansible web_group,nfs -m user -a 'name=www uid=666 group=www shell=/sbin/nologin create_home=false' &&\
#4.配置httpd
ansible web_group -m copy -a 'src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/' &&\
#5.解压php安装包到web服务器
ansible web_group -m unarchive -a 'src=/root/php.tar.gz dest=/tmp/' &&\
#6.安装php
ansible web_group -m shell -a 'yum localinstall -y /tmp/*.rpm' &&\
#7.配置php
ansible web_group -m copy -a 'src=/etc/php-fpm.d/www.conf dest=/etc/php-fpm.d/' &&\
ansible web_group -m copy -a 'src=/etc/php.ini dest=/etc/' &&\
#8.启动php
ansible web_group -m systemd -a 'name=php-fpm state=started enabled=yes' &&\
#9.启动httpd
ansible web_group -m systemd -a 'name=httpd state=started enabled=yes' &&\
#10.解压代码
ansible web_group -m unarchive -a 'src=/root/kaoshi.zip dest=/var/www/html/ owner=www group=www' &&\
#11.站点目录授权
ansible web_group -m file -a 'path=/var/www/ state=directory owner=www group=www recurse=yes' &&\
#12.安装NFS
ansible nfs -m yum -a 'name=nfs-utils state=present' &&\
#13.安装rpcbind
ansible web_group,nfs -m yum -a 'name=rpcbind state=present' &&\
#14.配置nfs
ansible nfs -m copy -a 'content="/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)" dest=/etc/exports' &&\
#15.创建挂载目录
ansible nfs -m file -a 'path=/data state=directory owner=www group=www' &&\
#16.启动nfs
ansible nfs -m systemd -a 'name=nfs state=started' &&\
#17.启动rpcbind
ansible nfs -m systemd -a 'name=rpcbind state=started' &&\
#18.创建web端挂载的目录
ansible web_group -m file -a 'path=/var/www/html/upload state=directory owner=www group=www' &&\
#19.挂载
ansible web_group -m mount -a 'src=172.16.1.31:/data path=/var/www/html/upload fstype=nfs opts=defaults state=mounted'

Ansible 的 Playbook

一、playbook 概述

1.什么是playbook

PlayBook即"剧本","兵书"之意,PlayBook是由以下部分组成的

play(host): 定义的是主机的角色。(主角还是配角)
Book(task): 定义的是具体执行的任务。(角色的台词和动作)
playbook: 由一个或多个play(角色)组成,一个play(角色)可以包含多个task(台词,动作)。

简单理解为: 使用很多不同的模块指定主机完成一系列动作

在Ansible中"剧本文件"是以yml结尾的文件。
在SaltStack中"剧本文件"是以sls结尾的文件。
但是语法,使用的都是yaml语法

2.playbook组成

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QRqtmYqZ-1608644799408)(C:\Users\Administrator.DESKTOP-7PQVV6E\AppData\Roaming\Typora\typora-user-images\1608517310613.png)]

[root@m01 ~]# vim touch.yml 
#定义要执行动作的主机或主机组
- hosts: web_group
  #定义操作的用户
  remote_user: root
  #定义变量
  vars:
    #变量:变量的值
    file_name: lhd
  #指定主机的动作
  tasks:
    #动作的注释
    - name: Touch New File
      #使用shell模块执行动作
      shell: touch /tmp/{{ file_name }}
      
#模拟执行
[root@m01 ~]# ansible-playbook -C touch.yml
#验证语法
[root@m01 ~]# ansible-playbook --syntax-check touch.yml
#注意:只能验证语法,验证不了逻辑

3.PlayBook与ad-hoc

特点

PlayBook

ad-hoc

完整性



持久性



执行效率



变量

支持

不支持

耦合度



1.PlayBook功能比ad-hoc更全,是对ad-hoc的一种编排.
2.PlayBook能很好的控制先后执行顺序,以及依赖关系.
3.PlayBook语法展现更加的直观.
4.playbook可以持久使用,ad-hoc无法持久使用.

4. YAML 语法

语法

描述

缩进

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

冒号

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

短横线

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

- 中国:
  上海:
  北京:
    - 朝阳
    - 昌平
    - 海淀

二、playbook实战

0.配置主机清单

[root@m01 ~]# cat /etc/ansible/hosts 
[web_group]
web01 ansible_ssh_pass='1'
web02 ansible_ssh_pass='1'

[nfs_server]
nfs ansible_ssh_pass='1'

[rsync_server]
backup ansible_ssh_pass='1'

[db_server]
db01 ansible_ssh_pass='1'

[www:children]
web_group
nfs_server
rsync_server
[root@m01 lnmp]# cat base.yml 
- hosts: all
  tasks:
    - name: Stop Selinux
      selinux:
        state: disabled

    - name: Stop Firewalld
      systemd:
        name: firewalld
        state: stopped

1.部署httpd

1)编写剧本
[root@m01 lnmp]# cat httpd.yml 
- hosts: web_group
  tasks:
    - name: Install Httpd Server
      yum:
        name: httpd
        state: present

    - name: Config Httpd Server
      copy:
        src: /etc/httpd/conf/httpd.conf
        dest: /etc/httpd/conf/

    - name: Start Httpd Server
      systemd:
        name: httpd
        state: started
2)执行剧本
[root@m01 lnmp]# ansible-playbook httpd.yml

2.部署交作业页面

[root@m01 lnmp]# cat jiaozuoye.yml 
- hosts: all
  tasks:
    - name: Create www Group
      group:
        name: www
        gid: 666
        state: present

    - name: Create www User
      user:
        name: www
        uid: 666
        group: www
        shell: /sbin/nologin
        create_home: false

    - name: 安装NFS
      yum:
        name: nfs-utils
        state: present

    - name: 安装rpcbind
      yum:
        name: rpcbind
        state: present

    - name: 启动rpcbind
      systemd:
        name: rpcbind
        state: started

- hosts: web_group
  tasks:
    - name: Install Httpd Server
      yum:
        name: httpd
        state: present

    - name: Config httpd Server
      copy:
        src: /etc/httpd/conf/httpd.conf
        dest: /etc/httpd/conf/

    - name: 解压php安装包到web服务器
      unarchive:
        src: /root/php.tar.gz
        dest: /tmp/

    - name: 安装php
      shell: yum localinstall -y /tmp/*.rpm

    - name: 配置php
      copy:
        src: /etc/php-fpm.d/www.conf
        dest: /etc/php-fpm.d/

    - name: 配置php
      copy:
        src: /etc/php.ini
        dest: /etc/

    - name: 启动php
      systemd:
        name: php-fpm
        state: started
        enabled: yes

    - name: 启动httpd
      systemd:
        name: httpd
        state: started
        enabled: yes

    - name: 解压代码
      unarchive:
        src: /root/kaoshi.zip
        dest: /var/www/html/
        owner: www 
        group: www

    - name: 站点目录授权
      file:
        path: /var/www/
        state: directory
        owner: www
        group: www
        recurse: yes

    - name: 安装NFS
      yum:
        name: nfs-utils
        state: present

- hosts: nfs
  tasks:
    - name: 配置nfs
      copy:
        content: "/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)"
        dest: /etc/exports

    - name: 创建挂载目录
      file:
        path: /data
        state: directory
        owner: www
        group: www

    - name: 启动nfs
      systemd:
        name: nfs
        state: started

- hosts: web_group
  tasks:
    - name: 创建web端挂载的目录
      file:
        path: /var/www/html/upload
        state: directory
        owner: www
        group: www

    - name: 挂载
      mount:
        src: 172.16.1.31:/data
        path: /var/www/html/upload
        fstype: nfs
        opts: defaults
        state: mounted

3.部署rsync客户端和服务端

1)配置主机清单
[root@m01 lnmp]# cat /etc/ansible/hosts 
[web_group]
web01 ansible_ssh_pass='1'
web02 ansible_ssh_pass='1'

[nfs_server]
nfs ansible_ssh_pass='1'

[rsync_server]
backup ansible_ssh_pass='1'

[db_server]
db01 ansible_ssh_pass='1'

[www:children]
web_group
nfs_server
rsync_server
2)准备rsync配置文件
[root@m01 lnmp]# vim /etc/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.passwd
log file = /var/log/rsyncd.log
#####################################
[backup]
comment = welcome to oldboyedu backup!
path = /backup
3)准备sersync
#1.准备包
[root@m01 ~]# ll sersync2.5.4_64bit_binary_stable_final.tar.gz 
-rw-r--r-- 1 root root 727290 Aug 23 12:22 sersync2.5.4_64bit_binary_stable_final.tar.gz

#2.准备配置文件
[root@m01 ~]# vim GNU-Linux-x86/confxml.xml
    <inotify>
        <delete start="true"/>
        <createFolder start="true"/>
        <createFile start="true"/>
        <closeWrite start="true"/>
        <moveFrom start="true"/>
        <moveTo start="true"/>
        <attrib start="true"/>
        <modify start="true"/>
    </inotify>
    <sersync>
        <localpath watch="/data">
            <remote ip="172.16.1.41" name="backup"/>
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
    ... ...
    </sersync>
4)编写剧本
[root@m01 lnmp]# cat rsync_client.yml 
- hosts: nfs_server
  tasks:
    - name: Install Rsync Server
      yum:
        name: rsync
        state: present

    - name: Install Inotify-Tools Server
      yum:
        name: inotify-tools
        state: present

    - name: Install Sersync Server
      unarchive:
        src: /root/sersync2.5.4_64bit_binary_stable_final.tar.gz
        dest: /usr/local/

    - name: Rename Sersync Dir
      shell: "mv /usr/local/GNU-Linux-x86 /usr/local/sersync"

    - name: Config Sersync Server
      copy:
        src: /root/GNU-Linux-x86/confxml.xml
        dest: /usr/local/sersync/

    - name: Chmod Sersync
      copy:
        src: /root/GNU-Linux-x86/sersync2
        dest: /usr/local/sersync/
        mode: 755

    - name: Config Rsync Client Password File
      copy:
        content: "123456"
        dest: /etc/rsync.password
        mode: 600

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

作业

0.使用playbook完成以下作业
1.使用nginx搭建交作业页面
2.两台web,NFS共享,backup实时备份
3.负载均衡