botang 发表于 2020-8-8 21:06:10

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

---
- name: Web Configuration
hosts: server1.example.com
vars:
    v_firewall_pkg: firewalld
    v_firewall_svc: firewalld
    v_web_pkg: httpd
    v_web_svc: httpd

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

    v_server_root: /var/www/html/server1
    v_web_server_src: ./v4.d/files/index-server1.html
    v_web_server_dest: /var/www/html/server1/index.html

    v_www_root: /var/www/html/www1
    v_web_www_src: ./v4.d/files/index-www1.html
    v_web_www_dest: /var/www/html/www1/index.html
    v_htaccess_src: ./v4.d/files/.htaccess
    v_htaccess_dest: /var/www/html/www1/.htaccess
    v_secrets_src: ./v4.d/files/www1.pass
    v_secrets_dest: /etc/httpd/www1.pass
tasks:
- name: Install Web Packages
    yum:
      name: "{{ v_web_pkg }}"
      state: latest

- name: Create Remote Directory for server
    file:
      path: "{{ v_server_root }}"
      # recurse: yes
      state: directory

- name: Create Remote Directory for www
    file:
      path: "{{ v_www_root }}"
      #recurse: yes
      state: directory

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

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

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

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

- name: Copy Web Configuration
    copy:
      src: "{{ v_httpconf_src }}"
      dest: "{{ v_httpconf_dest }}"

- name: Copy Web Passwd
    copy:
      src: "{{ v_secrets_src }}"
      dest: "{{ v_secrets_dest }}"

- name: Copy Web Htaccess
    copy:
      src: "{{ v_htaccess_src }}"
      dest: "{{ v_htaccess_dest }}"

- name: Start Web
    service:
      name: "{{ v_web_svc }}"
      state: restarted
      enabled: yes

- name: Firewalld Service Enable and Started
    service:
      name: "{{ v_firewall_svc }}"
      state: started
      enabled: yes

- name: Open the Port for the Web Server
    firewalld:
      service: http
      permanent: yes
      state: enabled
      immediate: yes

- name: Test Web Server with Basic Auth
hosts: localhost
vars:
    web_user: testuser1
vars_files:
    - ./v4.d/vars/secret.yml
tags:
    - tag1
tasks:
- name: Connect to Web Server without Basic Authentication
    uri:
      url: http://server1.example.com
      return_content: yes
      status_code: 200
    register: v_result_server

- name: Connect to Web Server with Basic Authentication
    uri:
      url: http://www1.example.com
      validate_certs: no
      force_basic_auth: yes
      user: "{{ web_user }}"
      password: "{{ web_pass }}"
      return_content: yes
      status_code: 200
    register: v_result_www

- name: Show the Result
    debug:
      var: "{{ item }}"
    loop:
      - v_result_server.content
      - v_result_server.status
      - v_result_www.content
      - v_result_www.status

   






了解loop循环的语法:
---
- name: Runing Mail Server
hosts: srvgroup
vars:
    v_mail_servers:
      - postfix
      - dovecot
tasks:
- name: Install Postfix
    yum:
      name: "{{ item }}"
      state: latest
    loop:
       "{{ v_mail_servers }}"

- name: Running Servers
    service:
      name: "{{ item }}"
      state: restarted
      enabled: yes
    loop:
      - postfix
      - dovecot

这个剧本改进以后的样子:

---
- name: Runing Mail Server
hosts: srvgroup
vars:
    v_mail_servers:
      - postfix
      - dovecot
    v_mail_firewall:
      - pop3
      - smtp
tasks:
- name: Install Postfix
    yum:
      name: "{{ item }}"
      state: latest
    loop:
      - "{{ v_mail_servers }}"

- name: Config File
    lineinfile:
      path: /etc/postfix/main.cf
      regexp: '^inet_interfaces ='
      line: inet_interfaces = all

- name: Running Servers
    service:
      name: "{{ item }}"
      state: restarted
      enabled: yes
    loop:
      - postfix
      - dovecot

- name: Open the Port for the Web Server
    firewalld:
      service: "{{ item }}"
      permanent: yes
      state: enabled
      immediate: yes
    loop: "{{ v_mail_firewall }}"

- name: Runing Mail Server
hosts: localhost
vars:
    v_mail_servers:
      - postfix
      - dovecot
tasks:
- name: Install Postfix
    yum:
      name: "{{ item }}"
      state: latest
    loop:
      - "{{ v_mail_servers }}"

- name: Config File
    lineinfile:
      path: /etc/postfix/main.cf
      regexp: '^inet_interfaces ='
      line: inet_interfaces = all


- name: Running Servers
    service:
      name: "{{ item }}"
      state: restarted
      enabled: yes
    loop: "{{ v_mail_servers }}"




页: [1]
查看完整版本: ANSIBLE7-配置网页服务器(类似考题)