myvhost
[student@workstation dev-roles]$ cat use-vhost-role.yml 
---
- name: use vhost role playbook
  hosts: webservers

  pre_tasks:
    - debug:
        msg: 'Beginning web server configuration.'

  roles:
    - myvhost

  post_tasks:
    - debug:
        msg: 'Web server has been configured.'



[student@workstation dev-roles]$ mkdir -p roles/myvhost/{files,handlers}
[student@workstation dev-roles]$ mkdir roles/myvhost/{meta,tasks,templates}


[student@workstation dev-roles]$ vi roles/myvhost/tasks/main.yml
---
# tasks file for myvhost

- name: install httpd
  yum:
    name: httpd
    state: latest

- name: start and enable httpd service
  service:
    name: httpd
    state: started
    enabled: true

- name: deliver html content
  copy:
    src: html/
    dest: "/var/www/vhosts/{{ ansible_hostname }}"

- name: template vhost file
  template:
    src: vhost.conf.j2
    dest: /etc/httpd/conf.d/vhost.conf
    owner: root
    group: root
    mode: 0644
  notify:
    - restart httpd



[student@workstation dev-roles]$ vi roles/myvhost/handlers/main.yml
---
# handlers file for myvhost

- name: restart httpd
  service:
    name: httpd
    state: restarted


[student@workstation dev-roles]$ mkdir -p roles/myvhost/files/html
[student@workstation dev-roles]$ echo 'simle index' > roles/myvhost/files/html/index.html
simle index


[student@workstation dev-roles]$ mv vhost.conf.j2 roles/myvhost/templates/
# {{ ansible_managed }}

<VirtualHost *:80>
    ServerAdmin webmaster@{{ ansible_fqdn }}
    ServerName {{ ansible_fqdn }}
    ErrorLog logs/{{ ansible_hostname }}-error.log
    CustomLog logs/{{ ansible_hostname }}-common.log common
    DocumentRoot /var/www/vhosts/{{ ansible_hostname }}/

    <Directory /var/www/vhosts/{{ ansible_hostname }}/>
    Options +Indexes +FollowSymlinks +Includes
    Order allow,deny
    Allow from all
    </Directory>



[student@workstation dev-roles]$ ansible webservers -a 'yum list installed httpd'
 [WARNING]: Consider using yum module rather than running yum

servera.lab.example.com | SUCCESS | rc=0 >>
Loaded plugins: langpacks, search-disabled-repos
Installed Packages
httpd.x86_64                       2.4.6-45.el7                        @rhel_dvd

[student@workstation dev-roles]$ ansible webservers -a 'systemctl is-active httpd'
servera.lab.example.com | SUCCESS | rc=0 >>
active

[student@workstation dev-roles]$ 
[student@workstation dev-roles]$ ansible webservers -a 'systemctl is-enabled httpd'
servera.lab.example.com | SUCCESS | rc=0 >>
enabled

[student@workstation dev-roles]$ 
[student@workstation dev-roles]$ ansible webservers -a 'cat /etc/httpd/conf.d/vhost.conf'
servera.lab.example.com | SUCCESS | rc=0 >>
# Ansible managed

<VirtualHost *:80>
    ServerAdmin webmaster@servera.lab.example.com
    ServerName servera.lab.example.com
    ErrorLog logs/servera-error.log
    CustomLog logs/servera-common.log common
    DocumentRoot /var/www/vhosts/servera/

    <Directory /var/www/vhosts/servera/>
    Options +Indexes +FollowSymlinks +Includes
    Order allow,deny
    Allow from all
    </Directory>
</VirtualHost>

[student@workstation dev-roles]$ ansible webservers -a 'cat /var/www/vhosts/servera/index.html'
servera.lab.example.com | SUCCESS | rc=0 >>
simle index

[student@workstation dev-roles]$ ansible webservers -a 'curl -s http://localhost'
 [WARNING]: Consider using get_url or uri module rather than running curl

servera.lab.example.com | SUCCESS | rc=0 >>
simle index

[student@workstation dev-roles]$ curl -S http://servera.lab.example.com
simle index



[student@workstation dev-roles]$ mkdir -p roles/myfirewall/{defaults,handlers,tasks}


[student@workstation dev-roles]$ tree
.
├── ansible.cfg
├── inventory
├── roles
│   ├── myfirewall
│   │   ├── defaults
│   │   │   └── main.yml
│   │   ├── handlers
│   │   │   └── main.yml
│   │   └── tasks
│   │       └── main.yml
│   └── myvhost
│       ├── files
│       │   └── html
│       │       └── index.html
│       ├── handlers
│       │   └── main.yml
│       ├── meta
│       │   └── main.yml
│       ├── tasks
│       │   └── main.yml
│       └── templates
│           └── vhost.conf.j2
└── use-vhost-role.yml


[student@workstation dev-roles]$ cat roles/myfirewall/tasks/main.yml 
---
# tasks file for myfirewall

- name: install firewalld
  yum:
    name: firewalld
    state: latest

- name: start and enable firewalld service
  service:
    name: firewalld
    state: started
    enabled: true

- name: firewall services config
  firewalld:
    state: enabled
    immediate: true
    permanent: true
    service: "{{ firewall_service }}"

[student@workstation dev-roles]$ 
[student@workstation dev-roles]$ cat roles/myfirewall/handlers/main.yml 
---
# handlers file for myfirewall

- name: restart firewalld
  service:
    name: firewalld
    state: restared
[student@workstation dev-roles]$ 
[student@workstation dev-roles]$ cat roles/myfirewall/defaults/main.yml 
---
# default file for myfirewall

firewall_service: ssh
[student@workstation dev-roles]$ 
[student@workstation dev-roles]$ cat roles/myvhost/meta/main.yml 
---
dependencies:
  - { role: myfirewall, firewall_service: http }



[student@workstation dev-roles]$ ansible-playbook use-vhost-role.yml 

PLAY [use vhost role playbook] ************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [servera.lab.example.com]

TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [servera.lab.example.com] => {
    "msg": "Beginning web server configuration."
}

TASK [myfirewall : install firewalld] *****************************************************************************************************************************************************************************
changed: [servera.lab.example.com]

TASK [myfirewall : start and enable firewalld service] ************************************************************************************************************************************************************
changed: [servera.lab.example.com]

TASK [myfirewall : firewall services config] **********************************************************************************************************************************************************************
ok: [servera.lab.example.com]

TASK [myvhost : install httpd] ************************************************************************************************************************************************************************************
ok: [servera.lab.example.com]

TASK [myvhost : start and enable httpd service] *******************************************************************************************************************************************************************
ok: [servera.lab.example.com]

TASK [myvhost : deliver html content] *****************************************************************************************************************************************************************************
ok: [servera.lab.example.com]

TASK [myvhost : template vhost file] ******************************************************************************************************************************************************************************
ok: [servera.lab.example.com]

TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [servera.lab.example.com] => {
    "msg": "Web server has been configured."
}

PLAY RECAP ********************************************************************************************************************************************************************************************************
servera.lab.example.com    : ok=10   changed=2    unreachable=0    failed=0   

[student@workstation dev-roles]$ 
[student@workstation dev-roles]$ 
[student@workstation dev-roles]$ curl http://servera.lab.example.com
simle index


  • No labels
Write a comment…