利用Ansible的Playbook实现简单的案例和http的简单介绍

Posted 一夜入秋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用Ansible的Playbook实现简单的案例和http的简单介绍相关的知识,希望对你有一定的参考价值。

1、ansible-playbook实现mysql5.7.37的二进制部署

1.1、简单的架构和主机准备


上面的图是我用来做学习的简单小实验用的

主机的准备:7台虚拟机
1、Ansible主控制端:
主机名:ansible
系统:CentOS 8.5
IP: 10.0.0.8
ansible版本:2.9.27
2、Ansible被控制端-webserver组
主机1:
主机名:web1
系统:CentOS 7.9
IP: 10.0.0.7
主机2:
主机名:web2
系统:CentOS 7.9
IP: 10.0.0.17
3、Ansible被控制端-dbserver组
主机1:
主机名:db1
系统:ubuntu18.04
IP: 10.0.0.100
主机2:
主机名:db2
系统:ubuntu18.04
IP: 10.0.0.102
4、Ansible被控制端-webserver组
主机1:
主机名:app1
系统:CentOS 8.5
IP: 10.0.0.18
主机2:
主机名:app2
系统:CentOS 7.9
IP: 10.0.0.27

1.2、Ansible主机的准备

1.2.1、ansible主机环境的准备以及软件包的安装

[root@Centos8 ~]#hostnamectl set-hostname ansible
[root@Centos8 ~]#exit
[root@ansible ~]#hostname -I
10.0.0.8
#默认ansible的版本
[root@ansible ~]#yum info ansible
Last metadata expiration check: 0:28:15 ago on Thu 31 Mar 2022 09:37:42 PM CST.
Available Packages
Name         : ansible
Version      : 2.9.27
Release      : 1.el8
Architecture : noarch
Size         : 17 M
Source       : ansible-2.9.27-1.el8.src.rpm
Repository   : EPEL
Summary      : SSH-based configuration management, deployment, and task execution system
URL          : http://ansible.com
License      : GPLv3+
Description  : Ansible is a radically simple model-driven configuration management,
             : multi-node deployment, and remote task execution system. Ansible works
             : over SSH and does not require any software or daemons to be installed
             : on remote nodes. Extension modules can be written in any language and
             : are transferred to managed machines automatically.

#安装ansible软件
[root@ansible ~]#yum -y install ansible
#安装完检查安装的版本
[root@ansible ~]#ansible --version
ansible 2.9.27
  config file = /etc/ansible/ansible.cfg
  configured module search path = [/root/.ansible/plugins/modules, /usr/share/ansible/plugins/modules]
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Sep 10 2021, 09:13:53) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]

1.2.2、主控端和被控端基于key验证

#修改ssh服务配置文件
[root@ansible ~]#vim /etc/ssh/ssh_config
StrictHostKeyChecking no
#编写基于key验证的脚本
[root@ansible ~]#vim ssh_key_for_linux.sh
#!/bin/bash

PASS=wm521314
END=254

IP=`ip a s eth0 | awk -F[ /]+ NR==3print $3`
NET=$IP%.*.

. /etc/os-release

rm -f /root/.ssh/id_rsa
[ -e ./SCANIP.log ] && rm -f SCANIP.log

for((i=3;i<="$END";i++));do
    ping -c 1 -w 1  $NET$i &> /dev/null  && echo "$NET$i" >> SCANIP.log &
done
wait

ssh-keygen -P "" -f /root/.ssh/id_rsa
if [ $ID = "centos" -o $ID = "rocky" ];then
    rpm -q sshpass || yum -y install sshpass
else
    dpkg -i sshpass &> /dev/null || apt -y install sshpass
fi

sshpass -p $PASS ssh-copy-id -o StrictHostKeyChecking=no $IP 

AliveIP=(`cat SCANIP.log`)
for n in $AliveIP[*];do
    sshpass -p $PASS scp -o StrictHostKeyChecking=no -r /root/.ssh root@$n:
done

for n in $AliveIP[*];do
    scp /root/.ssh/known_hosts $n:.ssh/
done
#编写完后运行运行脚本,实现key验证
[root@ansible ~]#bash ssh_key_for_linux.sh

1.2.3、主控端ansible配置

#配置ansible中的主机清单
[root@ansible ~]#vim /etc/ansible/hosts
[test]
10.0.0.8 ansible_connection=local

[webserver]
10.0.0.7
10.0.0.17

[dbserver]
10.0.0.100
10.0.0.102

[appserver]                                                                                           
10.0.0.18
10.0.0.27
#查看一下ansible中的主机列表
[root@ansible ~]#ansible all --list
  hosts (7):
    10.0.0.8
    10.0.0.7
    10.0.0.17
    10.0.0.100
    10.0.0.102
    10.0.0.18
    10.0.0.27
[root@ansible ~]#ansible dbserver --list
  hosts (2):
    10.0.0.100
    10.0.0.102
#检查机器是否都可以通信使用ping模块
[root@ansible ~]#ansible all -m ping
10.0.0.8 | SUCCESS => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    ,
    "changed": false,
    "ping": "pong"

10.0.0.7 | SUCCESS => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python"
    ,
    "changed": false,
    "ping": "pong"

10.0.0.17 | SUCCESS => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python"
    ,
    "changed": false,
    "ping": "pong"

10.0.0.100 | SUCCESS => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python3"
    ,
    "changed": false,
    "ping": "pong"

10.0.0.102 | SUCCESS => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python3"
    ,
    "changed": false,
    "ping": "pong"

10.0.0.18 | SUCCESS => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    ,
    "changed": false,
    "ping": "pong"

10.0.0.27 | SUCCESS => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python"
    ,
    "changed": false,
    "ping": "pong"

1.3、准备MySQL5.7.37二进制包

#这个MySQL5.7.37二进制包我是在网上清华大学开源软件镜像站上下载的
[root@ansible ~]#mkdir /data/mysql
[root@ansible ~]#cd /data/mysql/
[root@ansible mysql]#wget https://mirror.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz

1.4、准备ansible-playbook二进制部署MySQL5.7.37

#编写MySQL配置文件
[root@ansible mysql]#vim my.cnf
[root@ansible mysql]#cat my.cnf 
[mysqld]
server-id=1
log-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock                                                   
skip_name_resolve=on                                                
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid

[client]
port=3306
socket=/data/mysql/mysql.sock
#准备好ansible-playbook脚本
#这个我就不去网上下载MySQL5.7.37的二进制文件包了,我首先下载好在本地了
[root@ansible mysql]#vim install_mysql5.7.37.yaml
---
- hosts: dbserver
  remote_user: root
  gather_facts: no
  vars:
    mysql_version: 5.7.37
    mysql_file: mysql-mysql_version-linux-glibc2.12-x86_64.tar.gz
    mysql_root_password: Mysql@2022

  tasks:
    - name: install packages
      apt:
        name:
          - libaio1
        state: latest
    - name: create mysql group
      group:
        name: mysql
        gid: 336
    - name: create mysql user
      user:
        name: mysql
        uid: 336
        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/mysql/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-insecure --user=mysql --datadir=/data/mysql
      tags: data
    - name: config my.cnf
      copy:
        src: /data/mysql/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: change password
      shell: /usr/local/mysql/bin/mysqladmin -uroot password mysql_root_password
[root@ansible mysql]#tree
.
├── install_mysql5.7.37.yaml
├── my.cnf
└── mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz

0 directories, 3 files
#使用刚刚写好的playbook文件部署一下
[root@ansible mysql]#ansible-playbook install_mysql5.7.37.yaml 

PLAY [dbserver] ************************************************************************************************************************************************************************************************

TASK [install packages] ****************************************************************************************************************************************************************************************
changed: [10.0.0.100]
changed: [10.0.0.102]

TASK [create mysql group] **************************************************************************************************************************************************************************************
changed: [10.0.0.100]
changed: [10.0.0.102]

TASK [create mysql user] ***************************************************************************************************************************************************************************************
changed: [10.0.0.100]
changed: [10.0.0.102]

TASK [copy tar to remote host and file mode] *******************************************************************************************************************************************************************
changed: [10.0.0.100]
changed: [10.0.0.102]

TASK [create linkfile /usr/local/mysql] ************************************************************************************************************************************************************************
changed: [10.0.0.102]
changed: [10.0.0.100]

TASK [data dir] ************************************************************************************************************************************************************************************************
changed: [10.0.0.100]
changed: [10.0.0.102]

TASK [config my.cnf] *******************************************************************************************************************************************************************************************
changed: [10.0.0.102]
changed: [10.0.0.100]

TASK [service script] ******************************************************************************************************************************************************************************************
changed: [10.0.0.100]
changed: [10.0.0.102]

TASK [PATH variable] *******************************************************************************************************************************************************************************************
changed: [10.0.0.102]
changed: [10.0.0.100]

TASK [enable service] ******************************************************************************************************************************************************************************************
changed: [10.0.0.102]
changed: [10.0.0.100]

TASK [change password] *****************************************************************************************************************************************************************************************
changed: [10.0.0.102]
changed: [10.0.0.100]

PLAY RECAP *****************************************************************************************************************************************************************************************************
10.0.0.100                 : ok=11   changed=11   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
10.0.0.102                 : ok=11   changed=11   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

1.2.5、验证是否安装成功

#安装完后需要退出登录一下,或者重重启一下
root@db1:~# mysql -V
mysql  Ver 14.14 Distrib 5.7.37, for linux-glibc2.12 (x86_64) using  EditLine wrapper
root@db1:~# mysql -uroot -pMysql@2022
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 3
Server version: 5.7.37-log MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type help; or \\h for help. Type \\c to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

root@db2:~# mysql -V
mysql  Ver 14.14 Distrib 5.7.37, for linux-glibc2.12 (x86_64) using  EditLine wrapper
root@db2:~# mysql -uroot -pMysql@2022
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 3
Server version: 5.7.37-log MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type help; or \\h for help. Type \\c to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

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

2.1、简单的架构和主机准备


上面的图是我用来做学习的简单小实验用的

主机的准备:7台虚拟机
1、Ansible主控制端:
主机名:ansible
系统:CentOS 8.5
IP: 10.0.0.8
ansible版本:2.9.27
2、Ansible被控制端-webserver组
主机1:
主机名:web1
系统:CentOS 7.9
IP: 10.0.0.7
主机2:
主机名:web2
系统:CentOS 7.9
IP: 10.0.0.17
3、Ansible被控制端-dbserver组
主机1:
主机名:db1
系统:ubuntu18.04
IP: 10.0.0.100
主机2:
主机名:db2
系统:ubuntu18.04
IP: 10.0.0.102
4、Ansible被控制端-webserver组
主机1:
主机名:app1
系统:CentOS 8.5
IP: 10.0.0.18
主机2:
主机名:app2
系统:CentOS 7.9
IP: 10.0.0.27

2.2、Ansible主机的准备

2.2.1、ansible主机环境的准备以及软件包的安装

[root@Centos8 ~]#hostnamectl set-hostname ansible
[root@Centos8 ~]#exit
[root@ansible ~]#hostname -I
10.0.0.8
#默认ansible的版本
[root@ansible ~]#yum info ansible
Last metadata expiration check: 0:28:15 ago on Thu 31 Mar 2022 09:37:42 PM CST.
Available Packages
Name         : ansible
Version      : 2.9.27
Release      : 1.el8
Architecture : noarch
Size         : 17 M
Source       : ansible-2.9.27-1.el8.src.rpm
Repository   : EPEL
Summary      : SSH-based configuration management, deployment, and task execution system
URL          : http://ansible.com
License      : GPLv3+
Description  : Ansible is a radically simple model-driven configuration management,
             : multi-node deployment, and remote task execution system. Ansible works
             : over SSH and does not require any software or daemons to be installed
             : on remote nodes. Extension modules can be written in any language and
             : are transferred to managed machines automatically.

#安装ansible软件
[root@ansible ~]#yum -y install ansible
#安装完检查安装的版本
[root@ansible ~]#ansible --version
ansible 2.9.27
  config file = /etc/ansible/ansible.cfg
  configured module search path = [/root/.ansible/plugins/modules, /usr/share/ansible/plugins/modules]
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Sep 10 2021, 09:13:53) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]

2.2.2、主控端和被控端基于key验证

#修改ssh服务配置文件
[root@ansible ~]#vim /etc/ssh/ssh_config
StrictHostKeyChecking no
#编写基于key验证的脚本
[root@ansible ~]#vim ssh_key_for_linux.sh
#!/bin/bash

PASS=wm521314
END=254

IP=`ip a s eth0 | awk -F[ /]+ NR==3print $3`
NET=$IP%.*.

. /etc/os-release

rm -f /root/.ssh/id_rsa
[ -e ./SCANIP.log ] && rm -f SCANIP.log

for((i=3;i<="$END";i++));do
    ping -c 1 -w 1  $NET$i &> /dev/null  && echo "$NET$i" >> SCANIP.log &
done
wait

ssh-keygen -P "" -f /root/.ssh/id_rsa
if [ $ID = "centos" -o $ID = "rocky" ];then
    rpm -q sshpass || yum -y install sshpass
else
    dpkg -i sshpass &> /dev/null || apt -y install sshpass
fi

sshpass -p $PASS ssh-copy-id -o StrictHostKeyChecking=no $IP 

AliveIP=(`cat SCANIP.log`)
for n in $AliveIP[*];do
    sshpass -p $PASS scp -o StrictHostKeyChecking=no -r /root/.ssh root@$n:
done

for n in $AliveIP[*];do
    scp /root/.ssh/known_hosts $n:.ssh/
done
#编写完后运行运行脚本,实现key验证
[root@ansible ~]#bash ssh_key_for_linux.sh

2.2.3、主控端ansible配置

#配置ansible中的主机清单
[root@ansible ~]#vim /etc/ansible/hosts
[test]
10.0.0.8 ansible_connection=local

[webserver]
10.0.0.7
10.0.0.17

[dbserver]
10.0.0.100
10.0.0.102

[appserver]                                                                                           
10.0.0.18
10.0.0.27
#查看一下ansible中的主机列表
[root@ansible ~]#ansible all --list
  hosts (7):
    10.0.0.8
    10.0.0.7
    10.0.0.17
    10.0.0.100
    10.0.0.102
    10.0.0.18
    10.0.0.27
[root@ansible ~]#ansible webserver --list
  hosts (2):
    10.0.0.7
    10.0.0.17
#检查机器是否都可以通信使用ping模块
[root@ansible ~]#ansible all -m ping
10.0.0.8 | SUCCESS => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    ,
    "changed": false,
    "ping": "pong"

10.0.0.7 | SUCCESS => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python"
    ,
    "changed": false,
    "ping": "pong"

10.0.0.17 | SUCCESS => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python"
    ,
    "changed": false,
    "ping": "pong"

10.0.0.100 | SUCCESS => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python3"
    ,
    "changed": false,
    "ping": "pong"

10.0.0.102 | SUCCESS => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python3"
    ,
    "changed": false,
    "ping": "pong"

10.0.0.18 | SUCCESS => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    ,
    "changed": false,
    "ping": "pong"

10.0.0.27 | SUCCESS => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python"
    ,
    "changed": false,
    "ping": "pong"

2.3、准备httpd的源码包以及相关的依赖包和文件

[root@ansible ~]#mkdir -pv /apps/httpd
mkdir: created directory /apps
mkdir: created directory /apps/httpd
[root@ansible ~]#cd /apps/httpd/
[root@ansible httpd]#wget https://mirror.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.51.tar.bz2
[root@ansible httpd]#wget https://mirror.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.bz2
[root@ansible httpd]#wget https://mirror.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.bz2
[root@ansible httpd]#vim 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
#EnvironmentFile=/etc/sysconfig/httpd
#ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecStart=/apps/httpd/bin/apachectl start
#ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecReload=/apps/httpd/bin/apachectl graceful
#ExecStop=/bin/kill -WINCH $MAINPID
ExecStop=/apps/httpd/bin/apachectl stop                                                                                                                                                                         
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target
#以下是准备的文件和软件包
[root@ansible httpd]#tree
.
├── apr-1.7.0.tar.bz2
├── apr-util-1.6.1.tar.bz2
├── httpd-2.4.51.tar.bz2
└── httpd.service

0 directories, 4 files

2.4、准备ansible-playbook文件实现批量安装部署httpd

[root@ansible httpd]#vim install_httpd.yaml
---
- hosts: webserver
  remote_user: root
  vars:
    data_dir: /usr/local/src
    base_dir: /apps/httpd
    httpd_version: httpd-2.4.51
    apr_version: apr-1.7.0
    apr_util_version: apr-util-1.6.1

  tasks:
    - name: install packages
      yum:
        name: gcc,make,pcre-devel,openssl-devel,expat-devel,bzip2
        state: installed
    - name: download httpd file
      unarchive:
        src: " base_dir / httpd_version .tar.bz2"
        dest: " data_dir "
        owner: root
        remote_src: no
    - name: download apr file
      unarchive:
        src: " base_dir / apr_version .tar.bz2"
        dest: " data_dir "
        owner: root
        remote_src: no
    - name: download apr_util file
      unarchive:
        src: " base_dir / apr_util_version .tar.bz2"
        dest: " data_dir "
        owner: root
        remote_src: no
    - name: prepare apr dir
      shell: mv  apr_version   data_dir / httpd_version /srclib/apr
      args:
        chdir: " data_dir "
    - name: prepare apr-util dir
      shell: mv  apr_util_version   data_dir / httpd_version /srclib/apr-util
      args:
        chdir: " data_dir "
    - name: build httpd
      shell: ./configure --prefix= base_dir  --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 && make -j  ansible_processor_vcpus  && make install
      args:
        chdir: " data_dir / httpd_version "
    - name: create group
      group:
        name: www
        gid: 80
        system: yes
    - name: create user
      user:
        name: www
        uid: 80
        group: www
        shell: /sbin/nologin
        system: yes
        create_home: no
        home: " base_dir /conf/httpd"
    - name: set httpd user
      lineinfile:
        path: " base_dir /conf/httpd.conf"
        regexp: ^User
        line: User www
    - name: set httpd group
      lineinfile:
        path: " base_dir /conf/httpd.conf"
        regexp: ^Group
        line: Group www
    - name: set variable PATH
      shell: echo PATH= base_dir /bin:$PATH >> /etc/profile.d/httpd.sh
    - name: copy service file to remote
      copy:
        src: " base_dir /httpd.service"
        dest: /usr/lib/systemd/system/httpd.service
    - name: create index.html
      shell: echo `hostname -I` > /apps/httpd/htdocs/index.html
    - name: start service
      service:
        name: httpd
        state: started
        enabled: yes
#检查一下写的ansible-playbook是否有语法错误
[root@ansible httpd]#ansible-playbook --syntax-check install_httpd.yaml 

playbook: install_httpd.yaml
#运行ansible-playbook文件来部署httpd服务
[root@ansible httpd]#ansible-playbook install_httpd.yaml 

PLAY [webserver] ***********************************************************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************************************************
ok: [10.0.0.17]
ok: [10.0.0.7]

TASK [install packages] ****************************************************************************************************************************************************************************************
changed: [10.0.0.7]
changed: [10.0.0.17]

TASK [download httpd file] *************************************************************************************************************************************************************************************
changed: [10.0.0.17]
changed: [10.0.0.7]

TASK [download apr file] ***************************************************************************************************************************************************************************************
changed: [10.0.0.17]
changed: [10.0.0.7]

TASK [download apr_util file] **********************************************************************************************************************************************************************************
changed: [10.0.0.7]
changed: [10.0.0.17]

TASK [prepare apr dir] *****************************************************************************************************************************************************************************************
changed: [10.0.0.17]
changed: [10.0.0.7]

TASK [prepare apr-util dir] ************************************************************************************************************************************************************************************
changed: [10.0.0.7]
changed: [10.0.0.17]

TASK [build httpd] *********************************************************************************************************************************************************************************************
changed: [10.0.0.17]
changed: [10.0.0.7]

TASK [create group] ********************************************************************************************************************************************************************************************
changed: [10.0.0.17]
changed: [10.0.0.7]

TASK [create user] *********************************************************************************************************************************************************************************************
changed: [10.0.0.17]
changed: [10.0.0.7]

TASK [set httpd user] ******************************************************************************************************************************************************************************************
changed: [10.0.0.17]
changed: [10.0.0.7]

TASK [set httpd group] *****************************************************************************************************************************************************************************************
changed: [10.0.0.7]
changed: [10.0.0.17]

TASK [set variable PATH] ***************************************************************************************************************************************************************************************
changed: [10.0.0.17]
changed: [10.0.0.7]

TASK [copy service file to remote] *****************************************************************************************************************************************************************************
changed: [10.0.0.17]
changed: [10.0.0.7]

TASK [create index.html] ***************************************************************************************************************************************************************************************
changed: [10.0.0.7]
changed: [10.0.0.17]

TASK [start service] *******************************************************************************************************************************************************************************************
changed: [10.0.0.7]
changed: [10.0.0.17]

PLAY RECAP *****************************************************************************************************************************************************************************************************
10.0.0.17                  : ok=16   changed=15   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
10.0.0.7                   : ok=16   changed=15   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

2.5、验证是否安装

#检查安装的服务是否起来了
[root@web1 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2022-04-01 18:02:50 CST; 11min ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 95993 ExecStart=/apps/httpd/bin/apachectl start (code=exited, status=0/SUCCESS)
 Main PID: 95996 (httpd)
   CGroup: /system.slice/httpd.service
           ├─95996 /apps/httpd/bin/httpd -k start
           ├─96005 /apps/httpd/bin/httpd -k start
           ├─96006 /apps/httpd/bin/httpd -k start
           ├─96007 /apps/httpd/bin/httpd -k start
           ├─96008 /apps/httpd/bin/httpd -k start
           └─96009 /apps/httpd/bin/httpd -k start

Apr 01 18:02:45 web1 systemd[1]: Starting The Apache HTTP Server...
Apr 01 18:02:50 web1 apachectl[95993]: AH00558: httpd: Could not reliably determine the servers fully qualified domain name, using fe80::20c:29ff:fe80:5484%eth0. Set the ServerName directi...s this message
Apr 01 18:02:50 web1 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

[root@web2 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2022-04-01 18:02:50 CST; 12min ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 95538 ExecStart=/apps/httpd/bin/apachectl start (code=exited, status=0/SUCCESS)
 Main PID: 95541 (httpd)
   CGroup: /system.slice/httpd.service
           ├─95541 /apps/httpd/bin/httpd -k start
           ├─95550 /apps/httpd/bin/httpd -k start
           ├─95551 /apps/httpd/bin/httpd -k start
           ├─95552 /apps/httpd/bin/httpd -k start
           ├─95553 /apps/httpd/bin/httpd -k start
           └─95554 /apps/httpd/bin/httpd -k start

Apr 01 18:02:45 web2 systemd[1]: Starting The Apache HTTP Server...
Apr 01 18:02:50 web2 apachectl[95538]: AH00558: httpd: Could not reliably determine the servers fully qualified domain name, using fe80::20c:29ff:fe1f:4056%eth0. Set the ServerName directi...s this message
Apr 01 18:02:50 web2 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

#查看定义的index.html页面
[root@ansible httpd]#curl 10.0.0.17
10.0.0.17
[root@ansible httpd]#curl 10.0.0.7
10.0.0.7

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

以上是关于利用Ansible的Playbook实现简单的案例和http的简单介绍的主要内容,如果未能解决你的问题,请参考以下文章

Ansible6:Playbook简单使用

Ansible6:Playbook简单使用

ansible用法之ansible-playbook简单使用

自动化运维工具 Ansible ——playbook 剧本详解及简易案例

自动化运维工具Ansible-playbook详解和案例实战

ansible笔记3--playbook windows基础