Saturday, April 18, 2020

Sample Playbook with Roles

exroles/roles/webserver/{vars,tasks,handlers,templates}



Without Roles:
---


ex2.yml
---



---
- hosts: webservers
  sudo: yes
  vars:
    http_port: 80
    doc_dir: /ansible/
    doc_root: /var/www/html/ansible/
    max_clients: 5

  vars_prompt:
    - name: username
      prompt: what is your name?

  tasks:
  - name: Ensure that apache installed
    yum: name=httpd state=present
    when: ansible_os_family == "RedHat"

  - name: Start apache services
    service: name=httpd enaled=yes state=started
  - name: Deploy config file
    template: src=templates/httpd.j2 dest=/etc/httpd/conf/httpd.conf
    notify:
      - Restart Apache
  - name: Copy Site Files
    template: src=templates/index.j2 dest={{ doc_root }}/index.html

  handlers:
    - name: Restart Apache

With Roles: ----

We can move all the logic above to other files.

webserver.yml



---
- hosts: webservers
  sudo: yes
  gather_facts: no
  roles:
  - webserver

roles/webserver/vars/main.yml

---
http_port: 80
doc_dir: /ansible/
doc_root: /var/www/html/ansible/
max_clients: 5
username: My name is testingname

roles/webserver/tasks/main.yml

---
- name: Ensure Apache is installed
  yum: name=httpd state=present

- name: start apache
  service: name=httpd state=started enabled=yes

- name: Deploy cnfig
  template: src=httpd.j2 dest=/etc/httpd/conf/httpd.conf
  notify:
  - Restart Apache

- name: Copy Site Files
  template: src=index.j2 dest={{ doc_root }}/index.html

roles/webserver/handlers/main.yml

---
- name: Restart Apache
  service: name=httpd state=restarted

cd <parent folder to roles/main folder>
ansible-playbook webserver.yml

No comments:

Post a Comment