Thirteenth week

 ansible常用模块介绍

1 Command 模块
功能:在远程主机执行命令,此为默认模块,可忽略 -m 选项
注意:此命令不支持 $VARNAME < > | ; & 等,可能用shell模块实现
注意:此模块不具有幂等性
[19:05:47 root@lucklyzpp2 ~]#ansible webservers -m command -a 'chdir=/etc creates=/data/f1.txt cat centos-release'
192.168.33.4 | CHANGED | rc=0 >>
CentOS Linux release 7.5.1804 (Core)
[19:08:59 root@lucklyzpp2 ~]#ansible webservers -m command -a 'echo $hostname'
192.168.33.4 | CHANGED | rc=0 >>
$hostname
[19:09:55 root@lucklyzpp2 ~]#ansible webservers -m command -a 'echo zheng.0830 |passwd --stdin zheng'
192.168.33.4 | CHANGED | rc=0 >>
zheng.0830 |passwd --stdin zheng

2 Shell 模块
功能:和command相似,用shell执行命令,支持各种符号,比如:*,$, >
注意:此模块不具有幂等性
[19:19:08 root@lucklyzpp2 ~]#ansible webservers -m shell -a 'echo hello> /data/hello.log'
192.168.33.4 | CHANGED | rc=0 >>

[19:21:12 root@lucklyzpp2 ~]#ansible webservers -m shell -a 'cat /data/hello.log'
192.168.33.4 | CHANGED | rc=0 >>
hello
3 Script 模块
功能:在远程主机上运行ansible服务器上的脚本(无需执行权限)
注意:此模块不具有幂等性

4 Copy 模块
功能:从ansible服务器主控端复制文件到远程主机
注意: src=file 如果是没指明路径,则为当前目录或当前目录下的files目录下的file文件
[21:02:20 root@lucklyzpp2 ~]#ansible webservers -m copy -a "src=/root/log.sh dest=/data/log1.sh owner=zheng mode=600 backup=yes"

5 Get_url 模块
功能: 用于将文件从http、https或ftp下载到被管理机节点上
url: 下载文件的URL,支持HTTP,HTTPS或FTP协议

[20:53:17 root@lucklyzpp2 ~]#ansible webservers -m get_url -a 'url=http://nginx.org/download/nginx-1.18.0.tar.gz dest=/usr/local/src/nginx.tar.gz'

6 Fetch 模块
功能:从远程主机提取文件至ansible的主控端,copy相反,目前不支持目录

21:10:00 root@lucklyzpp2 ~]#ansible webservers -m fetch -a "src=/data/httpd.sh dest=/root/"
[21:11:15 root@lucklyzpp2 ~]#ls 192.168.33.4/data/httpd.sh 
192.168.33.4/data/httpd.sh
7 File 模块
功能:设置文件属性,创建软链接等
#创建空文件
ansible all -m file  -a 'path=/data/test.txt state=touch'
ansible all -m file  -a 'path=/data/test.txt state=absent'
ansible all -m file -a "path=/root/test.sh owner=wang mode=755"
#创建目录
ansible all -m file -a "path=/data/mysql state=directory owner=mysql
#创建软链接
[21:22:54 root@lucklyzpp2 ~]#ansible all -m file -a 'src=/data/mysql name=/data/zzz state=link'
8 stat 模块
功能:检查文件或文件系统的状态
注意:对于Windows目标,请改用win_stat模块
path:文件/对象的完整路径(必须)
9 unarchive 模块
功能:解包解压缩
实现有两种用法:
1、将ansible主机上的压缩包传到远程主机后解压缩至特定目录,设置copy=yes,此为默认值,可省略
2、将远程主机上的某个压缩包解压缩到指定路径下,设置copy=no
10 Archive 模块
功能:打包压缩保存在被管理节点
11 Hostname 模块
功能:管理主机名
Cron 模块
功能:计划任务
支持时间:minute,hour,day,month,weekday
#创建任务
ansible 10.0.0.8 -m cron -a 'hour=2 minute=30 weekday=1-5 name="backup mysql"
job=/root/mysql_backup.sh'
ansible websrvs   -m cron -a "minute=*/5 job='/usr/sbin/ntpdate ntp.aliyun.com
&>/dev/null' name=Synctime"

13 Yum 和 Apt 模块
功能:
yum 管理软件包,只支持RHEL,CentOS,fedora,不支持Ubuntu其它版本
apt 模块管理 Debian 相关版本的软件包
[root@centos8 ~]#ansible 192.168.33.4 -m apt -a
'name=bb,sl,cowsay,cmatrix,oneko,hollywood,boxes,libaa-bin,x11-apps'
[root@centos8 ~]#ansible websrvs -m apt -a 'name=rsync,psmisc state=absent'
模块很多,需要慢慢学习

1、ansible-playbook实现MySQL的二进制部署

[10:35:52 root@lucklyzpp2 ~]#mkdir -p /data/ansible/files
[10:35:52 root@lucklyzpp2 ~]#tree /data/ansible/
/data/ansible/
├── files
│   ├── my.cnf
│   ├── mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
│   └── mysql-test-5.7.35-linux-glibc2.12-x86_64.tar.gz
└── install_mysql5.7.yml

1 directory, 4 files
[10:35:56 root@lucklyzpp2 ~]#cd /data/ansible/
[10:36:13 root@lucklyzpp2 ansible]#cat files/my
my.cnf
mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
mysql-test-5.7.35-linux-glibc2.12-x86_64.tar.gz
[10:36:13 root@lucklyzpp2 ansible]#cat files/my.cnf 
[mysqld]
server-id=1
log-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock                                                  
                                               
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
[10:36:19 root@lucklyzpp2 ansible]#cat install_mysql5.7.yml 
---
- hosts: dbservers
  remote_user: root
  gather_facts: no
  vars:
   mysql_version: 5.7.35
   mysql_file: mysql-{{mysql_version}}-linux-glibc2.12-x86_64.tar.gz
   mysql_root_password: zhengpeng@123
  tasks:
    - name: install packages
      yum:
       name:
          - libaio
          - numactl-libs
          - MySQL-python
       state: latest
    - name: create mysql group
      group: name=mysql gid=306
    - name: create mysql user
      user: name=mysql uid=306 group=mysql shell=/sbin/nologin system=yes create_home=no home=/data/mysql
    - name: copy tar to remote host and file mode
      unarchive: src=/data/ansible/files/{{mysql_file}} dest=/usr/local/ owner=root group=root
    - name: create linkfile /usr/local/mysql
      file: src=/usr/local/mysql-{{ mysql_version }}-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link
    - name: data dir
      shell: /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/data/mysql
      tags: data
    - name: config my.cnf
      copy: src=/data/ansible/files/my.cnf  dest=/etc/my.cnf
    - name: service script
      shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
    - name: PATH variable
      copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
    - name: enable service
      shell: chkconfig --add mysqld;/etc/init.d/mysqld start
      tags: service
    - name: get password
      shell: awk '/A temporary password/{print $NF}' /data/mysql/mysql.log
      register: password
    - name: change password
      shell: /usr/local/mysql/bin/mysqladmin  -uroot -p'{{password.stdout}}' password {{mysql_root_password}}
[10:36:27 root@lucklyzpp2 ansible]#
[10:37:44 root@lucklyzpp2 ansible]#cat /etc/ansible/hosts 
[webservers]
192.168.33.4

[dbservers]
192.168.33.5
[10:37:44 root@lucklyzpp2 ansible]#ansible-playbook install_mysql5.7.yml
查看是否安装
[10:39:14 root@lucklydb ~]#ss -ntl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128           *:22                        *:*                  
LISTEN     0      100    127.0.0.1:25                        *:*                  
LISTEN     0      80           :::3306                     :::*                  
LISTEN     0      128          :::22                       :::*                  
LISTEN     0      100         ::1:25                       :::*                  
[10:39:21 root@lucklydb ~]#mysql -uroot -pzhengpeng@123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
mysql> exit
Bye

2、Ansible playbook实现apache批量部署,并对不同主机提供以各自IP地址为内容的index.html

21:45:50 root@lucklyzpp2 httpd_install]#cat httpd.service 
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
[Service]
Type=forking
ExecStart=/apps/httpd/bin/apachectl start
ExecReload=/apps/httpd/bin/apachectl graceful
ExecStop=/apps/httpd/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[21:45:56 root@lucklyzpp2 httpd_install]#cat > hosts << EOF
> [httpd]
> 192.168.33.4
> 192.168.33.5
> EOF
[21:50:10 root@lucklyzpp2 httpd_install]#ls
apr-1.7.0.tar.bz2  apr-util-1.6.1.tar.bz2  hosts  httpd-2.4.51.tar.bz2  httpd.service
[21:50:11 root@lucklyzpp2 httpd_install]#vim httpd_install3.yml
[21:53:03 root@lucklyzpp2 httpd_install]#cat httpd_install3.yml 
  - hosts: httpd
    remote_user: root
    gather_facts: no
    vars:
      httpd_file: httpd-2.4.51.tar.bz2
      arp_file: apr-1.7.0.tar.bz2
      arp_util_file: apr-util-1.6.1.tar.bz2

    tasks:
    - name: Ansible delete file /etc/yum.repos.d/*.repo
      find:
        paths: /etc/yum.repos.d/
        patterns: "*.repo"
      register: repos_to_del
    - name: yum repo file clean
      file: 
        path: "{{ item.path }}"
        state: absent
      with_items: "{{ repos_to_del.files }}"
    - name: copy repo file to remote
      copy:
        src: centos8.repo
        dest: /etc/yum.repos.d/
    - name: yum repo init
      mount:
        path: /media
        src: /dev/sr0
        fstype: iso9660
        opts: ro,noauto
        state: mounted
    - name: install packages
      yum:
        name:
          - gcc 
          - lrzsz
          - wget
          - make
          - pcre-devel
          - openssl-devel
          - expat-devel 
        state: present
    - name: Create a directory if it does not exist
      file:
        path: /apps
        state: directory
        mode: '0755'
    - name: Extract {{ httpd_file }} into /apps/httpd24
      unarchive:
        src: ./{{ httpd_file }}
        dest: /apps/
    - name: Extract {{ arp_file }} into /apps/apr/
      unarchive:
        src: ./{{ arp_file }}
        dest: /apps/httpd-2.4.46/srclib
    - name: Extract {{ arp_util_file }} into /apps/apr/
      unarchive:
        src: ./{{ arp_util_file }}
        dest: /apps/httpd-2.4.46/srclib
    - name: move directory /apps/httpd-2.4.46/srclib/apr
      shell: mv /apps/httpd-2.4.46/srclib/apr-1.7.0 /apps/httpd-2.4.46/srclib/apr
    - name: move directory /apps/httpd-2.4.46/srclib/apr-util
      shell: mv /apps/httpd-2.4.46/srclib/apr-util-1.6.1 /apps/httpd-2.4.46/srclib/apr-util  
    - name: Ensure group "apache" exists
      group:
        name: apache
        state: present
        gid: 80
    - name: Add the user 'apache' with a specific uid and a primary group of 'apache'
      user:
        name: apache
        comment: apache
        uid: 80
        group: apache
    - name: configure httpd
      shell: ./configure --prefix=/apps/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork chdir=/apps/httpd-2.4.46/
    - name: make
      shell: make -j 2 chdir=/apps/httpd-2.4.46/
    - name: make install 
      shell: make install chdir=/apps/httpd-2.4.46/
    - name: make ln
      file:
        src: /apps/httpd24
        dest: /apps/httpd
        owner: apache
        group: apache
        state: link
    - name: copy http.service file to remote
      copy:
        src: httpd.service
        dest: /usr/lib/systemd/system/
      notify: start httpd service
    - name: config index.html
      shell: echo `hostname -I` > /apps/httpd/htdocs/index.html
    - name: Replace httpd config file
      replace:
        path: /apps/httpd/conf/httpd.conf
        regexp: '^#(ServerName).*$'
        replace: '\1 :80'
    handlers:
    - name: start httpd service
      service:
        name: httpd
        state: started
        enabled: yes

[21:53:07 root@lucklyzpp2 httpd_install]#vim httpd_install3.yml
[22:00:45 root@lucklyzpp2 httpd_install]#ansible-playbook -i hosts httpd_install3.yml
查看服务是否安装
[22:10:53 root@lucklyweb ~]#ls /apps/httpd-2.4.51/
ABOUT_APACHE      CHANGES           .gitignore        libhttpd.dsp      README.CHANGES
acinclude.m4      changes-entries/  httpd.dep         libhttpd.mak      README.cmake
Apache-apr2.dsw   CMakeLists.txt    httpd.dsp         LICENSE           README.platforms
Apache.dsw        config.layout     httpd.mak         Makefile.in       ROADMAP
apache_probes.d   configure         httpd.spec        Makefile.win      server/
ap.d              configure.in      include/          modules/          srclib/
build/            .deps             INSTALL           NOTICE            support/
BuildAll.dsp      docs/             InstallBin.dsp    NWGNUmakefile     test/
BuildBin.dsp      emacs-style       LAYOUT            os/               .travis.yml
buildconf         .gdbinit          libhttpd.dep      README            VERSIONING
[22:12:02 root@lucklyweb ~]#service httpd status
Redirecting to /bin/systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2021-12-16 22:11:34 CST; 41s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 12758 (httpd)


3、http的报文结构和状态码总结

请求方法:

  • GET:请求指定的页面信息并返回结果。
  • POST:向指定资源提交数据进行处理请求(例如提交表单或者上传文件)。数据被包含在请求体中。POST请求可能会导致新的资源的建立和/或已有资源的修改。
  • HEAD:类似于get请求,只不过返回的响应中没有具体的内容,用于获取报头
  • PUT:从客户端向服务器传送的数据取代指定的文档的内容(修改)。
  • DELETE:请求服务器删除指定的数据。

post和get的区别:

  • 都包含请求头请求行,post多了请求body。
  • get多用来查询,请求参数放在url中,不会对服务器上的内容产生作用。post用来提交,如把账号密码放入body中。
  • GET是直接添加到URL后面的,直接就可以在URL中看到内容,而POST是放在报文内部的,用户无法直接看到。
  • GET提交的数据长度是有限制的,因为URL长度有限制,具体的长度限制视浏览器而定。而POST没有。
  • 概述

  • 当服务器响应时,其状态行的信息为HTTP的版本号,状态码,及解释状态码的简单说明。
  • HTTP的常见状态码种类:

  • 1XX:表示请求已被接受,需接后续处理。这类响应是临时响应,只包含状态行和某些可选的响应头信息,并以空行结束。
  • 2XX:表示请求已成功被服务器接收、理解并接受。
  • 3XX:表示需要客户端采取进一步的操作才能完成请求。通常用来重定向,重定向目标需在本次响应中指明。
  • 4XX:表示客户端可能发生了错误,妨碍了服务器的处理。
  • 5XX:表示服务器在处理请求的过程中有错误或者异常状态发生,也有可能是服务器以当前的软硬件资源无法完成对请求的处理。
  • 1xx:表示临时响应

  • 100:(继续)请求者应当继续提出请求。服务器返回此代码表示已收到请求的第一部分,正在等待其余部分
  • 101:(切换协议)请求者已要求服务器切换协议,服务器已确认并准备切换
  • 2xx:表示成功处理了请求的状态代码

  • 200(成功)服务器已成功处理了请求,通常,这表示服务器提供了请求的页面
  • 204(重置内容)服务器成功处理了请求,但没有返回任何内容
  • 206(部分内容)服务器成功处理了部分GET请求
  • 3xx(重定向):表示要完成请求,需要进一步操作。通常,这些状态代码用来重定向

  • 301:(永久移动)请求的页面已永久移动到新位置。服务器返回此响应(对GET和HEAD请求的响应)时,会自动将请求者转到新位置
  • 302(临时移动)服务器目前从不同位置的网页响应请求,但请求者应继续使用原有位置来进行以后的请求
  • 303(查看其他位置)请求者应当对不同的位置使用单独的GET请求来检索响应时,服务器返回此代码
  • 304(未修改)自从上次请求后,请求的页面未修改过。服务器返回此响应时,不会返回网页内容
  • 4xx(请求错误):这些状态代码表示请求可能出错,妨碍了服务器的处理

  • 400:(错误请求)服务器不理解请求的语法
  • 401(未授权)请求要求身份验证。对于需要登录的网页,服务器可能返回此响应
  • 403(禁止)服务器拒绝请求
  • 404(未找到)服务器找不到请求的网页
  • 405(方法禁用)禁用请求中指定的方法
  • 406(不接受)无法使用请求的内容特性响应请求的网页
  • 408(请求超时)服务器等候请求时发生超时
  • 414(请求的URI过长)请求的URI(通常为网址)过长,服务器无法处理
  • 415(不支持的媒体类型)请求的格式不受请求页面的支持
  • 416(请求范围不符合要求)如果页面无法提供请求的范围,则服务器会返回此状态码
  • 5xx(服务器错误)这些状态代码表示服务器在尝试处理请求时发生内部错误。这些错误可能是服务器本身的错误,而不是请求出错

  • 500(服务器内部错误)服务器遇到错误,无法完成请求
  • 501(尚未实施)服务器不具备完成请求的功能,例如,服务器无法识别请求方法时可能会返回此代码
  • 502(错误网关)服务器作为网关和代理,从上游服务器收到无效响应
  • 503(服务器不可用)服务器目前无法使用(由于超载或者停机维护)。通常,这只是暂时状态
  • 504(网关超时)服务器作为网关或者代理,但是没有及时从上游服务器收到请求
  • 505(HTTP版本不受支持)服务器不支持请求中所用的HTTP协议版本

https传输流程:

  1. 首先客户端通过URL访问服务器建立SSL连接。
  2. 服务端收到客户端请求后,会将网站支持的证书信息(证书中包含公钥)传送一份给客户端。
  3. 客户端的服务器开始协商SSL连接的安全等级,也就是信息加密的等级。
  4. 客户端的浏览器根据双方同意的安全等级,建立会话密钥,然后利用网站的公钥将会话密钥加密,并传送给网站。
  5. 服务器利用自己的私钥解密出会话密钥。
  6. 服务器利用会话密钥加密与客户端之间的通信。

http协议的缺点:

  • 请求信息明文传输,容易被窃听截取。
  • 数据的完整性未校验,容易被篡改
  • 没有验证对方身份,存在冒充危险

HTTP协议不适合传输一些敏感信息,比如:各种账号、密码等信息,使用http协议传输隐私信息非常不安全。

HTTPS的缺点:

  • HTTPS协议多次握手,导致页面的加载时间延长近50%;
  • HTTPS连接缓存不如HTTP高效,会增加数据开销和功耗;
  • 申请SSL证书需要钱,功能越强大的证书费用越高。
  • SSL涉及到的安全算法会消耗 CPU 资源,对服务器资源消耗较大。

HTTPS和HTTP的区别:

  • https是http协议的安全版本,http协议的数据传输是明文的,是不安全的,https使用了SSL/TLS协议进行了加密处理。
  • http和https使用连接方式不同,默认端口也不一样,http是80,https是443。

        耐得住寂寞,方能内心平静、宠辱不惊,有所作为。耐得住寂寞,才能不为外物所惑,才能不浮躁,才能专心致志,心无旁骛,不怨天尤人,不妄自菲薄,才能不忘初心,坚持到底。有些事情,不是看到了希望才去坚持,而是坚持了才有希望

原文地址:https://www.cnblogs.com/zpkf/p/15695981.html