Ad Code

Responsive Advertisement

Ticker

6/recent/ticker-posts

Ansible

 Inventoy

# Inventory File (hosts)
# This file defines groups of hosts: webservers and dbservers.

[webservers]
web1.example.com
web2.example.com

[dbservers]
db1.example.com
db2.example.com

# Playbook File (site.yml)
# This playbook includes multiple tasks to deploy and configure servers,
# create user accounts, deploy application code, and configure firewalld.

---
- name: Deploy and Configure Web and Database Servers
  hosts: all
  vars:
    http_port: 80
    db_user: admin
    db_pass: secret

  tasks:
    # Install NGINX on web servers
    - name: Install NGINX on web servers
      yum:
        name: nginx
        state: present
      when: "'webservers' in group_names"
      tags: install_web

    # Install MySQL on database servers
    - name: Install MySQL on database servers
      yum:
        name: mysql-server
        state: present
      when: "'dbservers' in group_names"
      tags: install_db

    # Copy NGINX configuration template
    - name: Copy NGINX configuration template
      template:
        src: templates/nginx.conf.j2
        dest: /etc/nginx/nginx.conf
      notify: restart nginx
      when: "'webservers' in group_names"
      tags: config_web

    # Ensure NGINX is running
    - name: Ensure NGINX is running
      service:
        name: nginx
        state: started
      when: "'webservers' in group_names"
      tags: config_web

    # Copy MySQL configuration template
    - name: Copy MySQL configuration template
      template:
        src: templates/my.cnf.j2
        dest: /etc/my.cnf
      notify: restart mysql
      when: "'dbservers' in group_names"
      tags: config_db

    # Ensure MySQL is running
    - name: Ensure MySQL is running
      service:
        name: mysqld
        state: started
      when: "'dbservers' in group_names"
      tags: config_db

  handlers:
    # Handler to restart NGINX
    - name: restart nginx
      service:
        name: nginx
        state: restarted

    # Handler to restart MySQL
    - name: restart mysql
      service:
        name: mysqld
        state: restarted

- name: Create and Configure User Accounts
  hosts: all
  tasks:
    # Create user on all servers
    - name: Create user on all servers
      user:
        name: "{{ item }}"
        state: present
        groups: "wheel"
      with_items:
        - user1
        - user2
      tags: users

- name: Deploy Application Code
  hosts: webservers
  tasks:
    # Copy application code
    - name: Copy application code
      copy:
        src: /local/path/to/code/
        dest: /var/www/html/
      tags: deploy_code

- name: Configure Firewalld
  hosts: all
  tasks:
    # Ensure firewalld is running
    - name: Ensure firewalld is running
      service:
        name: firewalld
        state: started
      tags: firewall

    # Open HTTP port in firewalld
    - name: Open HTTP port in firewalld
      firewalld:
        service: http
        permanent: true
        state: enabled
        immediate: yes
      tags: firewall

    # Open MySQL port in firewalld
    - name: Open MySQL port in firewalld
      firewalld:
        port: 3306/tcp
        permanent: true
        state: enabled
        immediate: yes
      when: "'dbservers' in group_names"
      tags: firewall

---
- name: Deploy and Configure Web and Database Servers
  hosts: all
  vars:
    http_port: 80
    db_user: admin
    db_pass: secret

  tasks:
    - name: Install NGINX on web servers
      ansible.builtin.yum:
        name: nginx
        state: present
      when: "'webservers' in group_names"
      tags: install_web

    - name: Install MySQL on database servers
      ansible.builtin.yum:
        name: mysql-server
        state: present
      when: "'dbservers' in group_names"
      tags: install_db

    - name: Copy NGINX configuration template
      template:
        src: templates/nginx.conf.j2
        dest: /etc/nginx/nginx.conf
      notify: restart nginx
      when: "'webservers' in group_names"
      tags: config_web

    - name: Ensure NGINX is running
      ansible.builtin.service:
        name: nginx
        state: started
      when: "'webservers' in group_names"
      tags: config_web

    - name: Copy MySQL configuration template
      template:
        src: templates/my.cnf.j2
        dest: /etc/my.cnf
      notify: restart mysql
      when: "'dbservers' in group_names"
      tags: config_db

    - name: Ensure MySQL is running
      ansible.builtin.service:
        name: mysqld
        state: started
      when: "'dbservers' in group_names"
      tags: config_db

 

Post a Comment

0 Comments

Ad Code

Responsive Advertisement