ansible 部署 httpd 及 http虚拟主机

1、使用ansible的playbook实现自动化安装httpd

#安装 ansible
~]# yum install ansible -y

#配置ssh免密
~]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:iXQokv/vUug487my1JeUIncQZ4Y5k8K4yDJ9L0Z1rpo [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|   o  .++        |
|  ..o B*.        |
|.oo..o+=.        |
|+.oooo +.o       |
|.. oo.+oS        |
|    o=++..       |
|   ..*o.o        |
|   .E o+         |
|    .*++o        |
+----[SHA256]-----+

# 将公钥copy到被管理的主机上
~]# ssh-copy-id localhost
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'localhost (::1)' can't be established.
ECDSA key fingerprint is SHA256:NwVk32KmJr1JyYnb8OXlmdIULF5VG1/QIHjMqvbn1aY.
ECDSA key fingerprint is MD5:47:cb:af:a7:4f:62:59:27:13:ea:f9:a6:e6:e8:44:f9.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/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
root@localhost's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'localhost'"
and check to make sure that only the key(s) you wanted were added.

# ansible playbook 目录结构
~]# tree
.
├── hosts_list                    # 主机列表文件
├── httpd.yml                     # 部署 httpd 的 playbook
└── roles                         # 自定义 role 目录
    └── httpd
        └── tasks
            └── main.yml          # httpd role的task 文件

3 directories, 3 files

~]# cat hosts_list 
[httpd]
localhost
~]# cat httpd.yml 
- hosts:
    - httpd

  roles:
    - httpd
    
~]# cat roles/httpd/tasks/main.yml 
- name: install httpd
  yum:
    name: httpd
    state: present

- name: enable httpd.service
  systemd:
    name: httpd.service
    enabled: true
    state: started

- name: stop firewalld.service
  systemd:
    name: firewalld.service
    enabled: false
    state: stopped

- name: disable selinux
  selinux:
    state: disabled
  register: selinux_status

- name: setenforce 0
  command: setenforce 0
  when: selinux_status.state != "disabled"
  
  
~]# ansible-playbook httpd.yml -i hosts_list 

PLAY [httpd] ***********************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************
ok: [localhost]

TASK [httpd : install httpd] *******************************************************************************************************************
changed: [localhost]

TASK [httpd : enable httpd.service] ************************************************************************************************************
changed: [localhost]

TASK [httpd : stop firewalld.service] **********************************************************************************************************
changed: [localhost]

TASK [httpd : disable selinux] *****************************************************************************************************************
 [WARNING]: SELinux state temporarily changed from 'enforcing' to 'permissive'. State change will take effect next reboot.

changed: [localhost]

TASK [httpd : setenforce 0] ********************************************************************************************************************
skipping: [localhost]

PLAY RECAP *************************************************************************************************************************************
localhost                  : ok=5    changed=4    unreachable=0    failed=0   

2、建立httpd服务器,要求提供两个基于名称的虚拟主机:

(1)www.X.com,页面文件目录为/web/vhosts/x;错误日志为/var/log/httpd/x.err,访问日志为/var/log/httpd/x.access

~]# cat /etc/httpd/conf.d/site-x.conf 

 DirectoryIndex index.html 
 ServerName www.X.com
 DocumentRoot "/web/vhosts/x"
 ErrorLog "/var/log/httpd/x.err"
 CustomLog  /var/log/httpd/x.access  combined
 
  Options -Indexes +FollowSymlinks
  AllowOverride All
  Require all granted
 

(2)www.Y.com,页面文件目录为/web/vhosts/y;错误日志为 /var/log/httpd/www2.err,访问日志为/var/log/httpd/y.access

~]# cat /etc/httpd/conf.d/site-y.conf  

 DirectoryIndex index.html 
 ServerName www.Y.com
 DocumentRoot "/web/vhosts/y"
 ErrorLog "/var/log/httpd/y.err"
 CustomLog  /var/log/httpd/y.access  combined
 
  Options -Indexes +FollowSymlinks
  AllowOverride All
  Require all granted
 

(3)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名

~]# mkdir -p /web/vhosts/{x,y}

~]# cat /web/vhosts/x/index.html 
www.X.com
~]# cat /web/vhosts/y/index.html  
www.Y.com

~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.58.149 www.X.com
192.168.58.149 www.Y.com

~]# systemctl reload httpd      

~]# curl www.X.com
www.X.com

~]# curl www.Y.com           
www.Y.com

你可能感兴趣的:(ansible 部署 httpd 及 http虚拟主机)