Bo's Oracle Station

查看: 734|回复: 0

ANSIBLE7-配置网页服务器(类似考题)

[复制链接]

1005

主题

1469

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
12012
发表于 2020-8-8 21:06:10 | 显示全部楼层 |阅读模式
  1. ---
  2. - name: Web Configuration
  3.   hosts: server1.example.com
  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/server1
  12.     v_web_server_src: ./v4.d/files/index-server1.html
  13.     v_web_server_dest: /var/www/html/server1/index.html

  14.     v_www_root: /var/www/html/www1
  15.     v_web_www_src: ./v4.d/files/index-www1.html
  16.     v_web_www_dest: /var/www/html/www1/index.html
  17.     v_htaccess_src: ./v4.d/files/.htaccess
  18.     v_htaccess_dest: /var/www/html/www1/.htaccess
  19.     v_secrets_src: ./v4.d/files/www1.pass
  20.     v_secrets_dest: /etc/httpd/www1.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://server1.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://www1.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.    

复制代码



v4.d.zip (2.58 KB, 下载次数: 25)
回复

使用道具 举报

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

本版积分规则

QQ|手机版|Bo's Oracle Station   

GMT+8, 2024-5-9 12:27 , Processed in 0.039221 second(s), 27 queries .

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