马哥Linux第十六周

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

1、安装ansible,并做好基于ssh-key验证
[root@centos7 ~]# yum install ansible -y
[root@centos7 ~]# ssh-keygen
[root@centos7 ~]# ssh-copy-id 192.168.37.17
[root@centos7 ~]# ssh-copy-id 192.168.37.27

2、创建角色所需目录及文件
[root@centos7 ~]# mkdir -p /data/playbook/roles/httpd/{tasks,files,templates,vars,handlers}
[root@centos7 ~]# cd /data/playbook/roles/httpd/tasks/
[root@centos7 tasks]# touch user.yml install.yml config.yml data.yml service.yml
[root@centos7 tasks]# ls > main.yml

3、准备好模板文件
[root@centos7 tasks]# yum install httpd -y
[root@centos7 tasks]# cp -p /etc/httpd/conf/httpd.conf ../templates/httpd7.conf.j2

4、角色配置文件
[root@centos7 tasks]# vim /etc/ansible/hosts
[apps]
192.168.37.17
192.168.37.27

[root@centos7 tasks]# vim user.yml
- name: create user
  user: name=apache shell=/sbin/nologin system=yes create_home=no
  
[root@centos7 tasks]# vim install.yml 
- name: install     
  yum: name=httpd
  
[root@centos7 tasks]# vim ../templates/httpd7.conf.j2
listen {{ http_port }}
User {{ username }}
Group {{ groupname }}

[root@centos7 tasks]# vim ../vars/main.yml
http_port: 80
username: apache
groupname: apache

[root@centos7 tasks]# vim config.yml 
- name: config
  template: src=httpd7.conf.j2 dest=/etc/httpd/conf/httpd.conf
  notify: restart service
  when: ansible_distribution_major_version=="7"
- name: config
  template: src=httpd6.conf.j2 dest=/etc/httpd/conf/httpd.conf
  notify: restart service
  when: ansible_distribution_major_version=="6"
  
[root@centos7 tasks]# vim data.yml 
- name: data file
  copy: src=roles/httpd/files/index.html dest=/var/www/html
  
[root@centos7 tasks]# vim service.yml 
- name: service
  service: name=httpd state=started enabled=yes
  
[root@centos7 tasks]# vim main.yml 
- include: user.yml 
- include: install.yml
- include: config.yml
- include: data.yml
- include: service.yml

[root@centos7 tasks]# vim ../handlers/main.yml 
- name: restart service
  service: name=httpd state=restarted

[root@centos7 tasks]# cd /data/playbook
[root@centos7 playbook]# vim httpd_role.yml
- hosts: apps
  remote_user: root
  roles:
    - httpd 
    
[root@centos7 tasks]# echo "welcome to magedu" >> /data/playbook/roles/httpd/files/index.html

5、验证安装
[root@centos7 tasks]# ansible-playbook -C /data/playbook/httpd_role.yml     #检查配置文件

PLAY [apps] *******************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************
ok: [192.168.37.17]
ok: [192.168.37.27]

TASK [httpd : create user] ****************************************************************************************
ok: [192.168.37.27]
ok: [192.168.37.17]

TASK [httpd : install] ********************************************************************************************
changed: [192.168.37.17]
changed: [192.168.37.27]

TASK [httpd : config] *********************************************************************************************
changed: [192.168.37.17]
changed: [192.168.37.27]

TASK [httpd : data file] ******************************************************************************************
changed: [192.168.37.27]
changed: [192.168.37.17]

TASK [httpd : service] ********************************************************************************************
changed: [192.168.37.27]
changed: [192.168.37.17]

RUNNING HANDLER [httpd : restart service] *************************************************************************
changed: [192.168.37.27]
changed: [192.168.37.17]

PLAY RECAP ********************************************************************************************************
192.168.37.17              : ok=7    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.37.27              : ok=7    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[root@centos7 tasks]# ansible-playbook /data/playbook/httpd_role.yml        #安装

PLAY [apps] *******************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************
ok: [192.168.37.17]
ok: [192.168.37.27]

TASK [httpd : create user] ****************************************************************************************
ok: [192.168.37.17]
ok: [192.168.37.27]

TASK [httpd : install] ********************************************************************************************
changed: [192.168.37.17]
changed: [192.168.37.27]

TASK [httpd : config] *********************************************************************************************
ok: [192.168.37.17]
ok: [192.168.37.27]

TASK [httpd : data file] ******************************************************************************************
ok: [192.168.37.27]
ok: [192.168.37.17]

TASK [httpd : service] ********************************************************************************************
changed: [192.168.37.27]
changed: [192.168.37.17]

PLAY RECAP ********************************************************************************************************
192.168.37.17              : ok=6    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.37.27              : ok=6    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Q2、建立httpd服务器,要求提供两个基于名称的虚拟主机:
(1)www.X.com,页面文件目录为/web/vhosts/x;错误日志为 /var/log/httpd/x.err,访问日志为/var/log/httpd/x.access
(2)www.Y.com,页面文件目录为/web/vhosts/y;错误日志为 /var/log/httpd/www2.err,访问日志为/var/log/httpd/y.access
(3)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名

[root@Centos7 ~]# cd /etc/httpd/conf.d/
[root@Centos7 conf.d]# vim web.conf

    documentroot "/web/vhosts/x"
    servername www.x.com
    customlog "logs/x.access" combined
    errorlog "logs/x.err"
               
        Require all granted
        Options Indexes FollowSymLinks
    



    documentroot "/web/vhosts/y"
    servername www.y.com
    customlog "logs/y.access" combined
    errorlog "logs/www2.err"
    
        Require all granted
        Options Indexes FollowSymLinks
    


[root@Centos7 conf.d]# mkdir -p /web/vhosts/{x,y}
[root@Centos7 conf.d]# echo www.x.com > /web/vhosts/x/index.html
[root@Centos7 conf.d]# echo www.y.com > /web/vhosts/y/index.html
[root@Centos7 conf.d]# chown -R apache:apache /web/vhosts/
[root@Centos7 conf.d]# apachectl graceful

#验证结果
[root@Centos7 conf.d]# ls -lh /var/log/httpd/x.* /var/log/httpd/y.access /var/log/httpd/www2.err 
-rw-r--r-- 1 root root  312 Apr  4 16:34 /var/log/httpd/www2.err
-rw-r--r-- 1 root root  904 Apr  4 16:36 /var/log/httpd/x.access
-rw-r--r-- 1 root root 1.3K Apr  4 16:35 /var/log/httpd/x.err
-rw-r--r-- 1 root root  271 Apr  4 16:36 /var/log/httpd/y.access

[root@Centos7 ~]# curl http://www.x.com
www.x.com
[root@Centos7 ~]# curl http://www.y.com
www.y.com

你可能感兴趣的:(马哥Linux第十六周)