翼度科技»论坛 云主机 LINUX 查看内容

ansible批量部署apache

8

主题

8

帖子

24

积分

新手上路

Rank: 1

积分
24
ansible批量部署apache


目录

已经好久没有使用centos的镜像进行部署ansible了,上周一个偶然的机会让我在centos8.5上面进行部署ansible。我发现在以前的基础上ansible的源发生了点改变,于是就想着自己把它记录下来。红红火火恍恍惚惚!!!!!!
环境介绍
系统ip主机名服务centos8.5192.168.222.154wy-ansibleansiblecentos8.5192.168.222.155wy-node1apachecentos8.5192.168.222.156wy-node2apache使用镜像如下
CentOS 8.5.2111版本下载链接(清华源)
安装ansible

wy-ansible端操作
配置ansible需要的源
  1. [root@wy-ansible ~]# cd /etc/yum.repos.d/
  2. [root@wy-ansible yum.repos.d]# ls
  3. CentOS-Linux-AppStream.repo          CentOS-Linux-FastTrack.repo
  4. CentOS-Linux-BaseOS.repo             CentOS-Linux-HighAvailability.repo
  5. CentOS-Linux-ContinuousRelease.repo  CentOS-Linux-Media.repo
  6. CentOS-Linux-Debuginfo.repo          CentOS-Linux-Plus.repo
  7. CentOS-Linux-Devel.repo              CentOS-Linux-PowerTools.repo
  8. CentOS-Linux-Extras.repo             CentOS-Linux-Sources.repo
  9. [root@wy-ansible yum.repos.d]# rm -rf *
  10. [root@wy-ansible yum.repos.d]# ls
  11. [root@wy-ansible yum.repos.d]#
  12. [root@wy-ansible yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
  13.   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
  14.                                  Dload  Upload   Total   Spent    Left  Speed
  15.   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--  100  2495  100  2495    0     0  15030      0 --:--:-- --:--:-- --:--:-- 15030
  16. [root@wy-ansible yum.repos.d]#
  17. [root@wy-ansible yum.repos.d]# dnf -y install epel-release
  18. [root@wy-ansible yum.repos.d]# dnf -y install  python36
  19. [root@wy-ansible yum.repos.d]# dnf -y install  python2
  20. [root@wy-ansible yum.repos.d]# wget  https://dl.rockylinux.org/pub/rocky/8/extras/x86_64/os/Packages/c/centos-release-ansible-29-1-2.el8.noarch.rpm
  21. [root@wy-ansible yum.repos.d]# dnf -y localinstall  centos-release-ansible-29-1-2.el8.noarch.rpm
  22. [root@wy-ansible yum.repos.d]# ls
  23. CentOS-Base.repo                              epel-playground.repo
  24. centos-release-ansible-29-1-2.el8.noarch.rpm  epel.repo
  25. CentOS-SIG-ansible-29.repo                    epel-testing-modular.repo
  26. epel-modular.repo                             epel-testing.repo
  27. [root@wy-ansible yum.repos.d]# sed -i -e "s|mirrorlist=|#mirrorlist=|g" /etc/yum.repos.d/CentOS-*
  28. [root@wy-ansible yum.repos.d]# sed -i -e "s|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g" /etc/yum.repos.d/CentOS-*
  29. [root@wy-ansible yum.repos.d]# ls
  30. CentOS-Base.repo                              epel-playground.repo
  31. centos-release-ansible-29-1-2.el8.noarch.rpm  epel.repo
  32. CentOS-SIG-ansible-29.repo                    epel-testing-modular.repo
  33. epel-modular.repo                             epel-testing.repo
  34. [root@wy-ansible yum.repos.d]#
  35. 查看CentOS-SIG-ansible-29.repo源
  36. [root@wy-ansible yum.repos.d]# cat CentOS-SIG-ansible-29.repo
  37. # CentOS-SIG-ansible-29.repo
  38. #
  39. # Please see https://wiki.centos.org/SpecialInterestGroup/ConfigManagementSIG/Ansible
  40. # for more information
  41. [centos-ansible-29]
  42. name=CentOS Configmanagement SIG - ansible-29
  43. #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=configmanagement-ansible-29
  44. baseurl=http://vault.centos.org/$contentdir/$releasever/configmanagement/$basearch/ansible-29/
  45. gpgcheck=1
  46. enabled=1
  47. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-ConfigManagement
  48. [centos-ansible-29-testing]
  49. name=CentOS Configmanagement SIG - ansible-29 Testing
  50. baseurl=http://buildlogs.centos.org/centos/8/configmanagement/$basearch/ansible-29/
  51. gpgcheck=0
  52. enabled=0
  53. [centos-ansible-29-debuginfo]
  54. name=CentOS Configmanagement SIG - ansible-29 Debug
  55. baseurl=http://debuginfo.centos.org/$contentdir/8/configmanagement/$basearch/
  56. gpgcheck=1
  57. enabled=0
  58. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-ConfigManagement
  59. [centos-ansible-29-source]
  60. name=CentOS Configmanagement SIG - ansible-29 Source
  61. baseurl=http://vault.centos.org/$contentdir/8/configmanagement/Source/ansible-29/
  62. gpgcheck=1
  63. enabled=0
  64. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-ConfigManagement
  65. [root@wy-ansible yum.repos.d]#
复制代码
安装ansible
  1. [root@wy-ansible yum.repos.d]# cd
  2. [root@wy-ansible ~]# dnf   -y   install   ansible   --nobest
  3. 查看ansible的版本
  4. [root@wy-ansible ~]# ansible --version
  5. ansible 2.9.27
  6.   config file = /etc/ansible/ansible.cfg
  7.   configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  8.   ansible python module location = /usr/lib/python3.6/site-packages/ansible
  9.   executable location = /usr/bin/ansible
  10.   python version = 3.6.8 (default, Sep 10 2021, 09:13:53) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
  11. [root@wy-ansible ~]#
复制代码
基于ansible进行基础准备

wy-node1端部分操作
  1. [root@localhost ~]# hostnamectl set-hostname wy-node1
  2. [root@localhost ~]# bash
  3. [root@wy-node1 ~]# ip a
  4. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  5.     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  6.     inet 127.0.0.1/8 scope host lo
  7.        valid_lft forever preferred_lft forever
  8.     inet6 ::1/128 scope host
  9.        valid_lft forever preferred_lft forever
  10. 2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
  11.     link/ether 00:0c:29:b0:cd:5e brd ff:ff:ff:ff:ff:ff
  12.     inet 192.168.222.155/24 brd 192.168.222.255 scope global dynamic noprefixroute ens160
  13.        valid_lft 1655sec preferred_lft 1655sec
  14.     inet6 fe80::20c:29ff:feb0:cd5e/64 scope link noprefixroute
  15.        valid_lft forever preferred_lft forever
  16. 3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
  17.     link/ether 52:54:00:02:53:44 brd ff:ff:ff:ff:ff:ff
  18.     inet 192.168.122.1/24 brd 192.168.122.255 scope global noprefixroute virbr0
  19.        valid_lft forever preferred_lft forever
  20. 4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
  21.     link/ether 52:54:00:02:53:44 brd ff:ff:ff:ff:ff:ff
  22. [root@wy-node1 ~]#
复制代码
因为后面需要挂载本地磁盘所以需要做以下操作


wy-node2部分操作
  1. [root@localhost ~]# hostnamectl set-hostname wy-node2
  2. [root@localhost ~]# bash
  3. [root@wy-node2 ~]# ip a
  4. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  5.     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  6.     inet 127.0.0.1/8 scope host lo
  7.        valid_lft forever preferred_lft forever
  8.     inet6 ::1/128 scope host
  9.        valid_lft forever preferred_lft forever
  10. 2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
  11.     link/ether 00:0c:29:63:60:5f brd ff:ff:ff:ff:ff:ff
  12.     inet 192.168.222.156/24 brd 192.168.222.255 scope global dynamic noprefixroute ens160
  13.        valid_lft 1607sec preferred_lft 1607sec
  14.     inet6 fe80::20c:29ff:fe63:605f/64 scope link noprefixroute
  15.        valid_lft forever preferred_lft forever
  16. 3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
  17.     link/ether 52:54:00:d1:d9:6b brd ff:ff:ff:ff:ff:ff
  18.     inet 192.168.122.1/24 brd 192.168.122.255 scope global noprefixroute virbr0
  19.        valid_lft forever preferred_lft forever
  20. 4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
  21.     link/ether 52:54:00:d1:d9:6b brd ff:ff:ff:ff:ff:ff
  22. [root@wy-node2 ~]#
复制代码
因为后面需要挂载本地磁盘所以需要做以下操作


wy-ansible端操作
做主控端和受控端的映射
  1. [root@wy-ansible ~]# vim /etc/hosts
  2. [root@wy-ansible ~]# cat /etc/hosts
  3. 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
  4. ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
  5. 192.168.222.155 wy-node1
  6. 192.168.222.156 wy-node2
  7. [root@wy-ansible ~]#
  8. root@wy-ansible ~]# mkdir playdemo
  9. [root@wy-ansible ~]# cd playdemo/
  10. [root@wy-ansible playdemo]# ls
  11. [root@wy-ansible playdemo]# cp /etc/ansible/ansible.cfg .
  12. [root@wy-ansible playdemo]# ls
  13. ansible.cfg
  14. [root@wy-ansible playdemo]# vim ansible.cfg
  15. #inventory      = /etc/ansible/hosts
  16. inventory      = inventory
  17. [root@wy-ansible playdemo]# vim inventory
  18. 查看受控端主机
  19. [root@wy-ansible playdemo]# cat inventory
  20. [apache]
  21. 192.168.222.155
  22. 192.168.222.156
  23. [root@wy-ansible playdemo]# ls
  24. ansible.cfg  inventory
  25. [root@wy-ansible playdemo]#
  26. 实现免密登录受控主机
  27. [root@wy-ansible playdemo]# ssh-keygen -t rsa
  28. Generating public/private rsa key pair.
  29. Enter file in which to save the key (/root/.ssh/id_rsa):
  30. Created directory '/root/.ssh'.
  31. Enter passphrase (empty for no passphrase):
  32. Enter same passphrase again:
  33. Your identification has been saved in /root/.ssh/id_rsa.
  34. Your public key has been saved in /root/.ssh/id_rsa.pub.
  35. The key fingerprint is:
  36. SHA256:AkRmdCRTQ3SH7uRey7cqiZZ9ol6tvSBgJmSFgMe9Lc0 root@wy-ansible
  37. The key's randomart image is:
  38. +---[RSA 3072]----+
  39. |oo =X=B ...      |
  40. |. +=.+ o..       |
  41. | .o .= .         |
  42. | o  o.E o        |
  43. |  . +..+S        |
  44. |   + . .o..      |
  45. |      .=o+..     |
  46. |      +o*++ .    |
  47. |     oo..+++..   |
  48. +----[SHA256]-----+
  49. [root@wy-ansible playdemo]#
  50. [root@wy-ansible playdemo]# ssh-copy-id 192.168.222.155
  51. /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
  52. The authenticity of host '192.168.222.155 (192.168.222.155)' can't be established.
  53. ECDSA key fingerprint is SHA256:JQ7UCwc6pwXDVYU92WwkCQLgB6qqiTbNLPDSZF8+us8.
  54. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
  55. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  56. /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
  57. root@192.168.222.155's password:
  58. Number of key(s) added: 1
  59. Now try logging into the machine, with:   "ssh '192.168.222.155'"
  60. and check to make sure that only the key(s) you wanted were added.
  61. [root@wy-ansible playdemo]# ssh-copy-id 192.168.222.156
  62. /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
  63. The authenticity of host '192.168.222.156 (192.168.222.156)' can't be established.
  64. ECDSA key fingerprint is SHA256:BsGn0HnCG5xb7gspwLlfgHIbDS6iX9XRwbJvlSChjYc.
  65. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
  66. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  67. /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
  68. root@192.168.222.156's password:
  69. Number of key(s) added: 1
  70. Now try logging into the machine, with:   "ssh '192.168.222.156'"
  71. and check to make sure that only the key(s) you wanted were added.
  72. [root@wy-ansible playdemo]#
  73. 检查机器节点是否连通
  74. [root@wy-ansible playdemo]# ansible all -m ping
  75. 192.168.222.155 | SUCCESS => {
  76.     "ansible_facts": {
  77.         "discovered_interpreter_python": "/usr/libexec/platform-python"
  78.     },
  79.     "changed": false,
  80.     "ping": "pong"
  81. }
  82. 192.168.222.156 | SUCCESS => {
  83.     "ansible_facts": {
  84.         "discovered_interpreter_python": "/usr/libexec/platform-python"
  85.     },
  86.     "changed": false,
  87.     "ping": "pong"
  88. }
  89. [root@wy-ansible playdemo]# ansible apache -m ping
  90. 192.168.222.156 | SUCCESS => {
  91.     "ansible_facts": {
  92.         "discovered_interpreter_python": "/usr/libexec/platform-python"
  93.     },
  94.     "changed": false,
  95.     "ping": "pong"
  96. }
  97. 192.168.222.155 | SUCCESS => {
  98.     "ansible_facts": {
  99.         "discovered_interpreter_python": "/usr/libexec/platform-python"
  100.     },
  101.     "changed": false,
  102.     "ping": "pong"
  103. }
  104. [root@wy-ansible playdemo]#
复制代码
配置受控端本地软件仓库

wy-ansible端操作
  1. [root@wy-ansible playdemo]# ansible apache -m mount -a 'src=/dev/sr0 path=/media state=mounted fstype=iso9660'
  2. 192.168.222.156 | CHANGED => {
  3.     "ansible_facts": {
  4.         "discovered_interpreter_python": "/usr/libexec/platform-python"
  5.     },
  6.     "changed": true,
  7.     "dump": "0",
  8.     "fstab": "/etc/fstab",
  9.     "fstype": "iso9660",
  10.     "name": "/media",
  11.     "opts": "defaults",
  12.     "passno": "0",
  13.     "src": "/dev/sr0"
  14. }
  15. 192.168.222.155 | CHANGED => {
  16.     "ansible_facts": {
  17.         "discovered_interpreter_python": "/usr/libexec/platform-python"
  18.     },
  19.     "changed": true,
  20.     "dump": "0",
  21.     "fstab": "/etc/fstab",
  22.     "fstype": "iso9660",
  23.     "name": "/media",
  24.     "opts": "defaults",
  25.     "passno": "0",
  26.     "src": "/dev/sr0"
  27. }
  28. [root@wy-ansible playdemo]#
  29. [root@wy-ansible playdemo]# ansible apache -m shell -a 'rm -rf /etc/yum.repos.d/C*'
  30. [WARNING]: Consider using the file module with state=absent rather than running
  31. 'rm'.  If you need to use command because file is insufficient you can add
  32. 'warn: false' to this command task or set 'command_warnings=False' in
  33. ansible.cfg to get rid of this message.
  34. 192.168.222.155 | CHANGED | rc=0 >>
  35. 192.168.222.156 | CHANGED | rc=0 >>
  36. [root@wy-ansible playdemo]# ansible apache -m yum_repository -a 'file=wy name=AppStream description=AppStream baseurl=file:///media/AppStream enabled=yes gpgcheck=no'
  37. 192.168.222.155 | CHANGED => {
  38.     "ansible_facts": {
  39.         "discovered_interpreter_python": "/usr/libexec/platform-python"
  40.     },
  41.     "changed": true,
  42.     "repo": "AppStream",
  43.     "state": "present"
  44. }
  45. 192.168.222.156 | CHANGED => {
  46.     "ansible_facts": {
  47.         "discovered_interpreter_python": "/usr/libexec/platform-python"
  48.     },
  49.     "changed": true,
  50.     "repo": "AppStream",
  51.     "state": "present"
  52. }
  53. [root@wy-ansible playdemo]# ansible apache -m yum_repository -a 'file=wy name=BaseOS description=BaseOS baseurl=file:///media/BaseOS enabled=yes gpgcheck=no'
  54. 192.168.222.155 | CHANGED => {
  55.     "ansible_facts": {
  56.         "discovered_interpreter_python": "/usr/libexec/platform-python"
  57.     },
  58.     "changed": true,
  59.     "repo": "BaseOS",
  60.     "state": "present"
  61. }
  62. 192.168.222.156 | CHANGED => {
  63.     "ansible_facts": {
  64.         "discovered_interpreter_python": "/usr/libexec/platform-python"
  65.     },
  66.     "changed": true,
  67.     "repo": "BaseOS",
  68.     "state": "present"
  69. }
  70. [root@wy-ansible playdemo]#
复制代码
wy-node1端检查
  1. [root@wy-node1 ~]# df -Th
  2. 文件系统            类型      容量  已用  可用 已用% 挂载点
  3. devtmpfs            devtmpfs  867M     0  867M    0% /dev
  4. tmpfs               tmpfs     896M     0  896M    0% /dev/shm
  5. tmpfs               tmpfs     896M   11M  885M    2% /run
  6. tmpfs               tmpfs     896M     0  896M    0% /sys/fs/cgroup
  7. /dev/mapper/cl-root xfs        17G  4.6G   13G   27% /
  8. /dev/sda1           xfs      1014M  259M  756M   26% /boot
  9. tmpfs               tmpfs     179M   40K  179M    1% /run/user/1000
  10. /dev/sr0            iso9660    11G   11G     0  100% /media
  11. [root@wy-node1 ~]# cd /etc/yum.repos.d/
  12. [root@wy-node1 yum.repos.d]# ls
  13. wy.repo
  14. [root@wy-node1 yum.repos.d]# cat wy.repo
  15. [AppStream]
  16. baseurl = file:///media/AppStream
  17. enabled = 1
  18. gpgcheck = 0
  19. name = AppStream
  20. [BaseOS]
  21. baseurl = file:///media/BaseOS
  22. enabled = 1
  23. gpgcheck = 0
  24. name = BaseOS
  25. [root@wy-node1 yum.repos.d]#
复制代码
wy-node2端检查
  1. [root@wy-node2 ~]# df -Th
  2. 文件系统            类型      容量  已用  可用 已用% 挂载点
  3. devtmpfs            devtmpfs  867M     0  867M    0% /dev
  4. tmpfs               tmpfs     896M     0  896M    0% /dev/shm
  5. tmpfs               tmpfs     896M   11M  885M    2% /run
  6. tmpfs               tmpfs     896M     0  896M    0% /sys/fs/cgroup
  7. /dev/mapper/cl-root xfs        17G  4.6G   13G   27% /
  8. /dev/sda1           xfs      1014M  259M  756M   26% /boot
  9. tmpfs               tmpfs     179M   44K  179M    1% /run/user/1000
  10. /dev/sr0            iso9660    11G   11G     0  100% /media
  11. [root@wy-node2 ~]# cd /etc/yum.repos.d/
  12. [root@wy-node2 yum.repos.d]# ls
  13. wy.repo
  14. [root@wy-node2 yum.repos.d]# cat wy.repo
  15. [AppStream]
  16. baseurl = file:///media/AppStream
  17. enabled = 1
  18. gpgcheck = 0
  19. name = AppStream
  20. [BaseOS]
  21. baseurl = file:///media/BaseOS
  22. enabled = 1
  23. gpgcheck = 0
  24. name = BaseOS
  25. [root@wy-node2 yum.repos.d]#
复制代码
安装受控端Apache(httpd)的最新版本

wy-ansible端操作
  1. [root@wy-ansible playdemo]# ansible apache -m yum -a 'name=httpd state=latest'
  2. 192.168.222.156 | CHANGED => {
  3.     "ansible_facts": {
  4.         "discovered_interpreter_python": "/usr/libexec/platform-python"
  5.     },
  6.     "changed": true,
  7.     "msg": "",
  8.     "rc": 0,
  9.     "results": [
  10.         "Installed: apr-util-openssl-1.6.1-6.el8.x86_64",
  11.         "Installed: centos-logos-httpd-85.8-2.el8.noarch",
  12.         "Installed: mod_http2-1.15.7-3.module_el8.4.0+778+c970deab.x86_64",
  13.         "Installed: httpd-2.4.37-41.module_el8.5.0+977+5653bbea.x86_64",
  14.         "Installed: apr-1.6.3-12.el8.x86_64",
  15.         "Installed: httpd-filesystem-2.4.37-41.module_el8.5.0+977+5653bbea.noarch",
  16.         "Installed: httpd-tools-2.4.37-41.module_el8.5.0+977+5653bbea.x86_64",
  17.         "Installed: apr-util-1.6.1-6.el8.x86_64",
  18.         "Installed: apr-util-bdb-1.6.1-6.el8.x86_64"
  19.     ]
  20. }
  21. 192.168.222.155 | CHANGED => {
  22.     "ansible_facts": {
  23.         "discovered_interpreter_python": "/usr/libexec/platform-python"
  24.     },
  25.     "changed": true,
  26.     "msg": "",
  27.     "rc": 0,
  28.     "results": [
  29.         "Installed: apr-util-openssl-1.6.1-6.el8.x86_64",
  30.         "Installed: centos-logos-httpd-85.8-2.el8.noarch",
  31.         "Installed: mod_http2-1.15.7-3.module_el8.4.0+778+c970deab.x86_64",
  32.         "Installed: httpd-2.4.37-41.module_el8.5.0+977+5653bbea.x86_64",
  33.         "Installed: apr-1.6.3-12.el8.x86_64",
  34.         "Installed: httpd-filesystem-2.4.37-41.module_el8.5.0+977+5653bbea.noarch",
  35.         "Installed: httpd-tools-2.4.37-41.module_el8.5.0+977+5653bbea.x86_64",
  36.         "Installed: apr-util-1.6.1-6.el8.x86_64",
  37.         "Installed: apr-util-bdb-1.6.1-6.el8.x86_64"
  38.     ]
  39. }
复制代码
启动受控端web服务(httpd)

wy-ansible端操作
  1. [root@wy-ansible playdemo]# ansible apache -m service -a 'name=httpd state=started enabled=yes'
  2. 192.168.222.155 | CHANGED => {
  3.     "ansible_facts": {
  4.         "discovered_interpreter_python": "/usr/libexec/platform-python"
  5.     },
  6.     "changed": true,
  7.     "enabled": true,
  8.     "name": "httpd",
  9.     "state": "started",
  10.     "status": {
  11.         "ActiveEnterTimestampMonotonic": "0",
  12.         "ActiveExitTimestampMonotonic": "0",
  13.         "ActiveState": "inactive",
  14.         "After": "network.target httpd-init.service systemd-journald.socket sysinit.target remote-fs.target system.slice basic.target systemd-tmpfiles-setup.service -.mount tmp.mount nss-lookup.target",
  15.         "AllowIsolate": "no",
  16.         "AllowedCPUs": "",
  17.         "AllowedMemoryNodes": "",
  18.         "AmbientCapabilities": "",
  19.         "AssertResult": "no",
  20.         "AssertTimestampMonotonic": "0",
  21.         "Before": "shutdown.target",
  22.         "BlockIOAccounting": "no",
  23.         "BlockIOWeight": "[not set]",
  24.         "CPUAccounting": "no",
  25.         "CPUAffinity": "",
  26. ......
  27. 192.168.222.156 | CHANGED => {
  28.     "ansible_facts": {
  29.         "discovered_interpreter_python": "/usr/libexec/platform-python"
  30.     },
  31.     "changed": true,
  32.     "enabled": true,
  33.     "name": "httpd",
  34.     "state": "started",
  35.     "status": {
  36.         "ActiveEnterTimestampMonotonic": "0",
  37.         "ActiveExitTimestampMonotonic": "0",
  38.         "ActiveState": "inactive",
  39.         "After": "system.slice basic.target nss-lookup.target -.mount sysinit.target remote-fs.target systemd-journald.socket httpd-init.service network.target tmp.mount systemd-tmpfiles-setup.service",
  40.         "AllowIsolate": "no",
  41.         "AllowedCPUs": "",
  42.         "AllowedMemoryNodes": "",
  43.         "AmbientCapabilities": "",
  44.         "AssertResult": "no",
  45.         "AssertTimestampMonotonic": "0",
  46. .......
  47. "Transient": "no",
  48.         "Type": "notify",
  49.         "UID": "[not set]",
  50.         "UMask": "0022",
  51.         "UnitFilePreset": "disabled",
  52.         "UnitFileState": "disabled",
  53.         "UtmpMode": "init",
  54.         "Wants": "httpd-init.service",
  55.         "WatchdogTimestampMonotonic": "0",
  56.         "WatchdogUSec": "0"
  57.     }
  58. }
  59. [root@wy-ansible playdemo]#
复制代码
配置受控端防火墙放行httpd服务流量

wy-ansible端操作
  1. [root@wy-ansible playdemo]# ansible apache -m firewalld -a 'zone=public service=http permanent=yes state=enabled immediate=yes'
  2. 192.168.222.155 | CHANGED => {
  3.     "ansible_facts": {
  4.         "discovered_interpreter_python": "/usr/libexec/platform-python"
  5.     },
  6.     "changed": true,
  7.     "msg": "Permanent and Non-Permanent(immediate) operation, Changed service http to enabled"
  8. }
  9. 192.168.222.156 | CHANGED => {
  10.     "ansible_facts": {
  11.         "discovered_interpreter_python": "/usr/libexec/platform-python"
  12.     },
  13.     "changed": true,
  14.     "msg": "Permanent and Non-Permanent(immediate) operation, Changed service http to enabled"
  15. }
  16. [root@wy-ansible playdemo]#
复制代码
受控端写入测试页面,要求带上个人信息(姓名或学号等),其它内容不限

wy-ansible端操作
  1. [root@wy-ansible playdemo]# ansible apache -m copy -a 'dest=/var/www/html/index.html content="wy-12345678的网站"'
  2. 192.168.222.155 | CHANGED => {
  3.     "ansible_facts": {
  4.         "discovered_interpreter_python": "/usr/libexec/platform-python"
  5.     },
  6.     "changed": true,
  7.     "checksum": "c2d3a833b1925aa171b555b98e3619f62ca531cc",
  8.     "dest": "/var/www/html/index.html",
  9.     "gid": 0,
  10.     "group": "root",
  11.     "md5sum": "bc55adb5abb3add29a34f0f7cc0563e0",
  12.     "mode": "0644",
  13.     "owner": "root",
  14.     "secontext": "system_u:object_r:httpd_sys_content_t:s0",
  15.     "size": 20,
  16.     "src": "/root/.ansible/tmp/ansible-tmp-1729763913.270614-351975-35211884046076/source",
  17.     "state": "file",
  18.     "uid": 0
  19. }
  20. 192.168.222.156 | CHANGED => {
  21.     "ansible_facts": {
  22.         "discovered_interpreter_python": "/usr/libexec/platform-python"
  23.     },
  24.     "changed": true,
  25.     "checksum": "c2d3a833b1925aa171b555b98e3619f62ca531cc",
  26.     "dest": "/var/www/html/index.html",
  27.     "gid": 0,
  28.     "group": "root",
  29.     "md5sum": "bc55adb5abb3add29a34f0f7cc0563e0",
  30.     "mode": "0644",
  31.     "owner": "root",
  32.     "secontext": "system_u:object_r:httpd_sys_content_t:s0",
  33.     "size": 20,
  34.     "src": "/root/.ansible/tmp/ansible-tmp-1729763913.2374783-351977-264864621390314/source",
  35.     "state": "file",
  36.     "uid": 0
  37. }
  38. [root@wy-ansible playdemo]#
复制代码
在主控端访问任意节点的IP测试

(可以用浏览器或curl   IP命令测试)
wy-ansible端操作
  1. [root@wy-ansible playdemo]# curl 192.168.222.155
  2. wy-12345678的网站[root@wy-ansible playdemo]# curl 192.168.222.156
  3. wy-12345678的网站[root@wy-ansible playdemo]# curl 192.168.222.155
  4. wy-12345678的网站[root@wy-ansible playdemo]# curl 192.168.222.156
  5. wy-12345678的网站[root@wy-ansible playdemo]#
复制代码
主控端浏览器查看



来源:https://www.cnblogs.com/tushanbu/p/18500351
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具