Skip to main content

Command Palette

Search for a command to run...

Day 58: Ansible Playbooks

Updated
2 min read

Task-01: Creating a File on a Different Server

Here is my host file look like

To create a file on a different server using Ansible, you can use the file module. write create_file.yml using vim

vim create_file.yml

Here's an example playbook:

---
- name: Create a file on a different server
  hosts: webserver
  tasks:
    - name: Create file
      file:
        path: /home/ubuntu/new_file.txt
        state: touch

now run this playbook using following command.

ansible-playbook create_file.yml

your output will be like this

Task-02: Creating a New User

To create a new user using Ansible, you can utilize the user module. Here's an example playbook:

To create a file on a different server using Ansible, you can use the file module. write create_user.yml using vim

vim create_user.yml

Here's an example playbook:

---
- name: Create a new user
  hosts: webserver
  become: true
  tasks:
    - name: Create user
      user:
        name: newuser
        state: present

now run this playbook using following command.

ansible-playbook create_user.yml

Task-03: Installing Docker on a Group of Servers

To install Docker on a group of servers using Ansible, you can utilize the apt or yum module based on your Linux distribution.

write install_docker.yml using vim

vim install_docker.yml

Here's an example playbook for Ubuntu servers:

---
- name: Install Docker on a group of servers
  hosts: webserver
  become: true
  tasks:
    - name: Update package repositories
      apt:
        update_cache: yes
      tags: docker
    - name: Install Docker packages
      apt:
        name: docker.io
        state: present
      tags: docker
    - name: Start Docker service
      service:
        name: docker
        state: started
        enabled: true
      tags: docker

now run this playbook using following command.

ansible-playbook install_docker.yml

That was all for today. see you another day with another challenge.

More from this blog

Ajay Patel

116 posts