1、使用ansible的playbook實(shí)現(xiàn)自動(dòng)化安裝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 root@localhost.localdomain
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到被管理的主機(jī)上
~]# 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 目錄結(jié)構(gòu)
~]# tree
.
├── hosts_list # 主機(jī)列表文件
├── 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服務(wù)器,要求提供兩個(gè)基于名稱的虛擬主機(jī):
(1)www.X.com袱院,頁面文件目錄為/web/vhosts/x;錯(cuò)誤日志為/var/log/httpd/x.err,訪問日志為/var/log/httpd/x.access
~]# cat /etc/httpd/conf.d/site-x.conf
<VirtualHost 192.168.58.149:80>
DirectoryIndex index.html
ServerName www.X.com
DocumentRoot "/web/vhosts/x"
ErrorLog "/var/log/httpd/x.err"
CustomLog /var/log/httpd/x.access combined
<Directory "/web/vhosts/x">
Options -Indexes +FollowSymlinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
(2)www.Y.com瞭稼,頁面文件目錄為/web/vhosts/y;錯(cuò)誤日志為 /var/log/httpd/www2.err忽洛,訪問日志為/var/log/httpd/y.access
~]# cat /etc/httpd/conf.d/site-y.conf
<VirtualHost 192.168.58.149:80>
DirectoryIndex index.html
ServerName www.Y.com
DocumentRoot "/web/vhosts/y"
ErrorLog "/var/log/httpd/y.err"
CustomLog /var/log/httpd/y.access combined
<Directory "/web/vhosts/y">
Options -Indexes +FollowSymlinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
(3)為兩個(gè)虛擬主機(jī)建立各自的主頁文件index.html,內(nèi)容分別為其對(duì)應(yīng)的主機(jī)名
~]# 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