Bo's Oracle Station

查看: 2344|回复: 0

ansible-5(项目案例:apache虚拟主机的配置)

[复制链接]

1005

主题

1469

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
12012
发表于 2020-12-12 15:52:06 | 显示全部楼层 |阅读模式
[student@workstation ansible]$ tree v4.d
v4.d
├── files
│   ├── index-servera.html
│   ├── index-wwwa.html
│   ├── vhosts.conf
│   └── wwwa.pass
└── vars
    └── secret.yml

2 directories, 5 files

[student@workstation ansible]$ cat v4.d/files/wwwa.pass ( htpasswd -c -m /文件名 用户名
testuser1:$apr1$oE0D6Utk$qlU7cjjt8a0ic8/YAHR9e.
testuser2:{SHA}pkxl4cxVt6gakc2TDshJtkXy2p4=


隐含文件要拷贝到:/var/www/html/wwwa:
.htaccess:
  1. AuthName "servera test apache with user and password"
  2. AuthUserFile "/etc/httpd/wwwa.pass"
  3. AuthType Basic
  4. require valid-user
复制代码


[student@workstation ansible]$ cat v4.d/files/index-server1.html
Welcome to server1.example.com.
[student@workstation ansible]$ cat v4.d/files/index-www1.html
Welcome to www1.example.com.


[student@workstation ansible]$ cat v4.d/files/vhosts.conf(拷贝自/usr/share/doc/httpd/httpd-vhost.conf)

<VirtualHost *:80>
    DocumentRoot "/var/www/html/servera"
    <Directory "/var/www/html/servera">
     <RequireAll>
      Require all granted
      Require not host serverc.lab.example.com
     </RequireAll>
    </Directory>   
    ServerName servera.lab.example.com
    ErrorLog "/var/log/httpd/servera.lab.example.com-error.log"
    CustomLog "/var/log/httpd/servera.lab.example.com-access.log" common
</VirtualHost>


<VirtualHost *:80>
    DocumentRoot "/var/www/html/wwwa"
    <Directory "/var/www/html/wwwa">
     AllowOverride  AuthConfig
    </Directory>   
    ServerName wwwa.lab.example.com
    ErrorLog "/var/log/httpd/wwwa.lab.example.com-error.log"
    CustomLog "/var/log/httpd/wwwa.lab.example.com-access.log" common
</VirtualHost>



[student@workstation ansible]$
------------------------------------------------------------
[student@workstation ansible]$ ansible-vault   view  v4.d/vars/secret.yml --vault-password-file=v3.d/vault.pass
web_pass: redhat123



v4-1_server1_E.yml:

  1. ---
  2. - name: Web Configuration
  3.   hosts: servera
  4.   vars:
  5.     v_firewall_pkg: firewalld
  6.     v_firewall_svc: firewalld
  7.     v_web_pkg: httpd
  8.     v_web_svc: httpd

  9.     v_httpconf_src: ./v4.d/files/vhosts.conf
  10.     v_httpconf_dest: /etc/httpd/conf.d/vhosts.conf

  11.     v_server_root: /var/www/html/servera
  12.     v_web_server_src: ./v4.d/files/index-servera.html
  13.     v_web_server_dest: /var/www/html/servera/index.html

  14.     v_www_root: /var/www/html/wwwa
  15.     v_web_www_src: ./v4.d/files/index-wwwa.html
  16.     v_web_www_dest: /var/www/html/wwwa/index.html
  17.     v_htaccss_src: ./v4.d/files/.htaccess
  18.     v_htaccess_dest: /var/www/html/wwwa/.htaccess
  19.     v_secrets_src: ./v4.d/files/wwwa.pass
  20.     v_secrets_dest: /etc/httpd/wwwa.pass
  21.   tasks:
  22.   - name: Install Web Packages
  23.     yum:
  24.       name: "{{ v_web_pkg }}"
  25.       state: latest

  26.   - name: Create Remote Directory for server
  27.     file:
  28.       path: "{{ v_server_root }}"
  29.       # recurse: yes
  30.       state: directory

  31.   - name: Create Remote Directory for www
  32.     file:
  33.       path: "{{ v_www_root }}"
  34.       #recurse: yes
  35.       state: directory

  36.   - name: Copy index.html for server
  37.     copy:
  38.       src: "{{ v_web_server_src }}"
  39.       dest: "{{ v_web_server_dest }}"

  40.   - name: Modified the index.html for server
  41.     lineinfile:
  42.       path: "{{ v_web_server_dest }}"
  43.       line: "{{ ansible_facts['fqdn'] }} and {{ ansible_facts['default_ipv4']['address'] }}"
  44.       state: present

  45.   - name: Copy index.html for www
  46.     copy:
  47.       src: "{{ v_web_www_src }}"
  48.       dest: "{{ v_web_www_dest }}"

  49.   - name: Modified the index.html for server
  50.     lineinfile:
  51.       path: "{{ v_web_www_dest }}"
  52.       line: "{{ ansible_facts['fqdn'] }} and {{ ansible_default_ipv4['address'] }}"
  53.       state: present

  54.   - name: Copy Web Configuration
  55.     copy:
  56.       src: "{{ v_httpconf_src }}"
  57.       dest: "{{ v_httpconf_dest }}"

  58.   - name: Copy Web Passwd
  59.     copy:
  60.       src: "{{ v_secrets_src }}"
  61.       dest: "{{ v_secrets_dest }}"

  62.   - name: Copy Web Htaccess
  63.     copy:
  64.       src: "{{ v_htaccess_src }}"
  65.       dest: "{{ v_htaccess_dest }}"

  66.   - name: Start Web
  67.     service:
  68.       name: "{{ v_web_svc }}"
  69.       state: restarted
  70.       enabled: yes

  71.   - name: Firewalld Service Enable and Started
  72.     service:
  73.       name: "{{ v_firewall_svc }}"
  74.       state: started
  75.       enabled: yes

  76.   - name: Open the Port for the Web Server
  77.     firewalld:
  78.       service: http
  79.       permanent: yes
  80.       state: enabled
  81.       immediate: yes

  82. - name: Test Web Server with Basic Auth
  83.   hosts: localhost
  84.   vars:
  85.     web_user: testuser1
  86.   vars_files:
  87.     - ./v4.d/vars/secret.yml
  88.   tags:
  89.     - tag1
  90.   tasks:
  91.   - name: Connect to Web Server without Basic Authentication
  92.     uri:
  93.       url: http://servera.lab.example.com
  94.       return_content: yes
  95.       status_code: 200
  96.     register: v_result_server

  97.   - name: Connect to Web Server with Basic Authentication
  98.     uri:
  99.       url: http://wwwa.lab.example.com
  100.       validate_certs: no
  101.       force_basic_auth: yes
  102.       user: "{{ web_user }}"
  103.       password: "{{ web_pass }}"
  104.       return_content: yes
  105.       status_code: 200
  106.     register: v_result_www

  107.   - name: Show the Result
  108.     debug:
  109.       var: "{{ item }}"
  110.     loop:
  111.       - v_result_server.content
  112.       - v_result_server.status
  113.       - v_result_www.content
  114.       - v_result_www.status
  115.   
  116.    
复制代码



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Bo's Oracle Station   

GMT+8, 2024-11-21 14:29 , Processed in 0.036110 second(s), 24 queries .

快速回复 返回顶部 返回列表