Saniul Ahsan

#javascript, #python, #microservices, #automations, #blockchains, #devops

Ansible Playbook Configurations For Various Tasks

1. Install Packages on Ubuntu/Debian:

---
- name: Install packages
  hosts: web_servers
  tasks:
    - name: Install packages
      apt:
        name: "{{ item }}"
        state: present
      loop:
        - nginx
        - mysql-server
        - php

2. Copy Files to Remote Hosts:

---
- name: Copy files
  hosts: app_servers
  tasks:
    - name: Copy configuration file
      copy:
        src: /path/to/local/file.conf
        dest: /etc/app/file.conf

3. Create User and Set Password:

---
- name: Create user
  hosts: db_servers
  tasks:
    - name: Create user
      user:
        name: myuser
        password: "{{ 'mypassword' | password_hash('sha512') }}"
        state: present

4. Restart Service:

---
- name: Restart service
  hosts: app_servers
  tasks:
    - name: Restart nginx
      service:
        name: nginx
        state: restarted

5. Manage System Packages on CentOS/RHEL:

---
- name: Manage packages
  hosts: all
  tasks:
    - name: Install packages
      yum:
        name: "{{ item }}"
        state: present
      loop:
        - httpd
        - php

6. Create Directory and Set Permissions:

---
- name: Create directory
  hosts: data_servers
  tasks:
    - name: Create directory
      file:
        path: /data/mydir
        state: directory
        mode: '0755'

7. Execute Commands on Remote Hosts:

---
- name: Execute command
  hosts: utility_servers
  tasks:
    - name: Run custom command
      command: echo "Hello, Ansible!"

8. Manage Firewall Rules:

---
- name: Manage firewall
  hosts: web_servers
  tasks:
    - name: Allow HTTP
      ufw:
        rule: allow
        port: 80

9. Create and Use Variables:

---
- name: Use variables
  hosts: app_servers
  vars:
    my_var: "Hello Ansible!"
  tasks:
    - name: Print variable
      debug:
        var: my_var

10. Use Conditionals:

---
- name: Use conditionals
  hosts: app_servers
  vars:
    os_type: ubuntu
  tasks:
    - name: Install package on Ubuntu
      apt:
        name: nginx
        state: present
      when: os_type == "ubuntu"