Ansible-2 : Creation and running inventory file

Ansible-2 : Creation and running inventory file

1.

Installation of nginx and docker into two other VMs by using an ansible playbook.

---
- name: Install Nginx
  hosts: web   # Replace with your target host or host group
  become: yes   # This allows the playbook to run with sudo privileges

  tasks:
    - name: Update apt cache
      apt:
        update_cache: yes

    - name: Install Nginx
      apt:
        name: nginx
        state: present

    - name: Start Nginx
      service:
        name: nginx
        state: started


- name: Install Docker using docker.io package
  hosts: web  # Replace with your target host or host group
  become: yes  # This allows the playbook to run with sudo privileges

  tasks:
    - name: Update APT package cache
      apt:
        update_cache: yes

    - name: Install Docker using docker.io package
      apt:
        name: docker.io
        state: present

    - name: Start Nginx
      service:
        name: nginx
        state: started

Now write the below command to run the ansible-playbook,

---
- name: Install Nginx
  hosts: web   # Replace with your target host or host group
  become: yes   # This allows the playbook to run with sudo privileges

  tasks:
    - name: Update apt cache
      apt:
        update_cache: yes

    - name: Install Nginx
      apt:
        name: nginx
        state: present

    - name: Start Nginx
      service:
        name: nginx
        state: started


- name: Install Docker using docker.io package
  hosts: web  # Replace with your target host or host group
  become: yes  # This allows the playbook to run with sudo privileges

  tasks:
    - name: Update APT package cache
      apt:
        update_cache: yes

    - name: Install Docker using docker.io package
      apt:
        name: docker.io
        state: present

    - name: Start Nginx
      service:
        name: nginx
        state: started
sudo ansible-playbook -i inventory.txt ansible-playbook.yaml


2.

Command to download using wget and extract getsamplefiles.com/download/tar/sample-1.tar, and paste in /$USER/folder for ansible-playbook.

---
- name: Download, extract, and move a file
  hosts: web  # Replace with your target host or host group
  become: yes  # This allows the playbook to run with sudo privileges

  tasks:
    - name: Download the tar file
      get_url:
        url: https://getsamplefiles.com/download/tar/sample-1.tar
        dest: /tmp/sample-1.tar
        mode: 0644  # Set the desired permissions

    - name: Extract the tar file
      unarchive:
        src: /tmp/sample-1.tar
        dest: /tmp  # Extract to a temporary directory
        remote_src: yes
        creates: /tmp/sample-1  # Specify the directory created by extraction

    - name: Move the extracted files to /$USER/folder
      command: mv /tmp/sample-1/* /home/$USER/

Above playbook i saved as extract_playbook.yaml.
Now write the below command to run the ansible-playbook,

sudo ansible-playbook -i inventory.txt extract_playbook.yaml