Lets Understand Ansible Playbooks

DevOps

YOUR COSMETIC CARE STARTS HERE

Find the Best Cosmetic Hospitals

Trusted • Curated • Easy

Looking for the right place for a cosmetic procedure? Explore top cosmetic hospitals in one place and choose with confidence.

“Small steps lead to big changes — today is a perfect day to begin.”

Explore Cosmetic Hospitals Compare hospitals, services & options quickly.

✓ Shortlist providers • ✓ Review options • ✓ Take the next step with confidence


1. What is an Ansible playbook? Give an example.

2. List all the block sections of an Ansible play.

3. What are Ansible inventory parameters? Explain their behavior with an example.

4. What are the different ways to introduce variables in an Ansible playbook?

5. What are templates in Ansible?

6. What are handlers in Ansible?


Please share an answer in the comments sections.

21 thoughts on “Lets Understand Ansible Playbooks

  1. 1] A playbook is a YAML file that defines a set of tasks that are executed on remote hosts

    eg: – name = install apache
    ansible.builtin.apt:
    state: latest

    2] hosts, vars, tasks, handlers, roles

    3] inventory parameters define host information like IP address, groups and variables.

    eg: 3.111.28.12 ansible_port=22 ansible_user= insha

    4] vars, tasks_vars, var_files, hardcoded, register

    5] Handlers are tasks that are triggered only when notified, used for service restart

  2. 1. What is an Ansible playbook? Give an example.
    Collection of play, Yaml File – Ex: Installing apache2 in ubuntu from a ile
    2. List all the block sections of an Ansible play.
    Name, host, tasks
    3. What are Ansible inventory parameters? Explain their behavior with an example.
    user, privtekey, port and etc. It helps to define paramenters for specific host
    4. What are the different ways to introduce variables in an Ansible playbook?
    vars, varfile, vars_prompt, task_vars
    5. What are templates in Ansible?
    Templates helps to recofigure system application in runtime. Like changing ports of apache
    6. What are handlers in Ansible?
    Handlers used to run task on conditional cases of previously executed task

  3. 1. What is an Ansible playbook? Give an example.
    playbook is set of rules, hosts, task put together to perform some action
    example, changing a port of some running service requires restart which includes three steps, stop, modify and start and defining some variables

    2. List all the block sections of an Ansible play.
    host, tasks, include/import, handlers, roles, templates

    3. What are Ansible inventory parameters? Explain their behavior with an example.
    host vars and group vars, host vars are specific to host and group vars for group of hosts
    Eg :
    [web]
    13.201.50.27 ansible_user=root

    [web:vars]
    ansible_user=ubuntu
    ansible_ssh_private_key_file=node.pem
    ansible_become=yes

    4. What are the different ways to introduce variables in an Ansible playbook?
    static vars, variable files – include vars, import vars, command line arguments, dynamic inputs, registry variables

    5. What are templates in Ansible?
    templates to perform run time operation in jinja2 files

    6. What are handlers in Ansible?
    to handle specific task, like conditional statement, if port is changes restart the service else no action

  4. 1. What is an Ansible playbook? Give an example.
    A playbook is a YAML file that defines tasks to automate using Ansible.
    – name: Install Apache
     hosts: webservers
     become: yes
     tasks:
      – name: Install httpd
       yum:
        name: httpd
        state: present

    2. List all the block sections of an Ansible play.
    name
    hosts
    vars
    tasks
    handlers
    roles

    3. What are Ansible inventory parameters? Explain their behavior with an example.
    They define hosts and groups Ansible manages. Parameters can set SSH port, user, etc
    [web]
    192.168.1.10 ansible_user=ubuntu ansible_port=22

    4. What are the different ways to introduce variables in an Ansible playbook?
    Inline in playbook (vars)
    Inventory files
    External variable files (vars_files)
    Extra vars (–extra-vars)

    5. What are templates in Ansible?
    Templates are files (usually .j2) that use Jinja2 to insert variables into config files

    6. What are handlers in Ansible? 
    Handlers are special tasks run only when notified by other tasks, usually for actions like restarting services.
    – name: Restart Apache
     service:
      name: httpd
      state: restarted
     when: changed

  5. 1. What is an Ansible playbook? Give an example.

    An Ansible playbook is a YAML file that defines a set of tasks to be executed on remote systems. It is used to automate configuration management, application deployment, and other IT tasks. Playbooks are written in a human-readable format and allow you to define the desired state of your systems.

    ### Example of an Ansible Playbook
    Here is a simple playbook that installs a package and starts a service:


    – name: Install and start Apache
     hosts: webservers
     become: yes
     vars:
      packagename: apache2
     tasks:
      – name: Install Apache
       apt:
        name: “{{ packagename }}”
        state: present

      – name: Start Apache service
       service:
        name: “{{ packagename }}”
        state: started

    ==============================================================
    2. List all the block sections of an Ansible play.

    name: A description of the play.
    hosts: Specifies the target hosts or groups to run the play on.
    become: Indicates whether privilege escalation (e.g., sudo) is required.
    vars: Defines variables for the play.
    vars_files: Includes external variable files.
    tasks: Contains a list of tasks to execute on the target hosts.
    handlers: Defines tasks that are triggered by notify statements in other tasks.
    roles: Specifies roles to include in the play.
    environment: Sets environment variables for tasks.
    pre_tasks: Tasks to run before the main tasks section.
    post_tasks: Tasks to run after the main tasks section.
    tags: Allows tagging of tasks for selective execution.
    ==============================================================

    3. What are Ansible inventory parameters? Explain their behavior with an example.

    Ansible inventory parameters are used to define the hosts and groups of hosts on which Ansible will execute tasks. These parameters can include hostnames, IP addresses, variables, and connection details. Inventories can be written in INI or YAML format.

    Common Inventory Parameters
    ansible_host: Specifies the IP address or hostname of the target.
    ansible_user: Defines the SSH user to connect as.
    ansible_port: Specifies the SSH port (default is 22).
    ansible_password: Sets the password for SSH authentication.
    ansible_ssh_private_key_file: Path to the private key file for SSH.
    ansible_connection: Defines the connection type (e.g., ssh, local, winrm).
    Custom Variables: You can define custom variables for hosts or groups.

    all:
     hosts:
      web1:
       ansible_host: 192.168.1.10
       ansible_user: ubuntu
      web2:
       ansible_host: 192.168.1.11
       ansible_user: ubuntu
     children:
      databases:
       hosts:
        db1:
         ansible_host: 192.168.1.20
         ansible_user: root
         ansible_password: secret
    ==============================================================

    4. What are the different ways to introduce variables in an Ansible playbook?

    – name: Example Playbook
     hosts: all
     vars:
      app_name: my_app
      app_port: 8080
     tasks:
      – name: Print app details
       debug:
        msg: “App {{ app_name }} is running on port {{ app_port }}”

    – name: Example Playbook
     hosts: all
     vars_files:
      – vars.yml
     tasks:
      – name: Print variables
       debug:
        msg: “App {{ app_name }} is running on port {{ app_port }}”

    ==============================================================

    5. What are templates in Ansible?

    Dynamic Content: Templates can include variables, loops, and conditionals to generate content dynamically.
    Jinja2 Syntax: Templates use Jinja2 syntax for embedding logic.
    File Extension: Templates typically have a .j2 extension.

    ==============================================================
    6. What are handlers in Ansible?

    Triggered by notify: Handlers are executed only when a task with a notify directive reports a change.
    Run Once: Even if multiple tasks notify the same handler, it will only run once at the end of the play.
    Defined in handlers Section: Handlers are defined in a dedicated handlers section of a playbook

    ==============================================================

    1. Ansible playbook is a YAML file that defines a series of automated tasks for one or more target systems. It is the heart of Ansible automation, used to :

    a) Install Software
    b) Configure services
    c) Deploy applications
    d) Manage files and users

    A playbook has a list of plays. Each play targets hosts. Each play has tasks.
    Example: Install Aoache on Ubuntu


    -name: Install and start Apache web server
    hosts: webservers
    become: true # Run tasks as root (sudo)

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

    -name: Install Apache2 package
    apt:
    name: apache2
    state: present

    -name: Ensure Apache is running
    service:
    name: apache2
    state: started
    enabled: true

  6. 1. What is an Ansible playbook? Give an example.
    Ansible playbook is a yaml based script or program which run and execute define tasks init with set of inventory defined in script. it also follow based hosts: & tasks based format.

    -name: demo of Ansible playbook
    hosts: localhost
    tasks:
    -name: demo of task from question

    2. List all the block sections of an Ansible play.
    hosts, tasks, handlers, vars, block, when, rescue, always,

    3. What are Ansible inventory parameters? Explain their behavior with an example.
    [inventory:vars] – to define variable at inventory level,
    [inventory:children] – to define group of hosts in inventory

    4. What are the different ways to introduce variables in an Ansible playbook?
    basically we have 5 major ways to defined variables in ansible playbook but we have 38 ways to initialize variable to playbook.
    1.) at hard coded playbook level
    2.) tasks level
    3.) through vars files
    4.) through prompts
    5.) via register variable
    6.) dynamic ways

    5. What are templates in Ansible?
    templates are a way to reflect string interpolation at run time which is supported bu Jinja template in ansible.

    6. What are handlers in Ansible?
    Handlers is a way to execute any task which meet with desired conditions. such if I want to restart Apache service in case of dynamic file change which is register via notify section in handlers list.

  7. 1. What is an Ansible playbook? Give an example.
    A set of tasks in a single file is Ansible Playbook.
    Example:
    – name: Update web servers
     hosts: webservers
     remote_user: root

     tasks:
     – name: Ensure apache is at the latest version
      ansible.builtin.yum:
       name: httpd
       state: latest

     – name: Write the apache config file
      ansible.builtin.template:
       src: /opt/httpd.j2
       dest: /etc/httpd.conf 

    2. List all the block sections of an Ansible play.
    vars
    tasks
    handlers

    3. What are Ansible inventory parameters? Explain their behavior with an example.
    dictionary
    list
    key: value

    4. What are the different ways to introduce variables in an Ansible playbook?
    vars
    host_vars
    group_vars
    vars-file
    prompt
    register (dynamic variables)
    –extra-vars within playbook command

    5. What are templates in Ansible?
    Templates in Ansible (jinja2) help interpretation of variables.

    6. What are handlers in Ansible?
    Handlers in Ansible are also a type of task that run a specific task only when Changed condition is met.

  8. 1. What is an Ansible playbook? Give an example.
    Ansible playbook have set of Automation task to manage hosts.For examle:
    – name: Update web servers
     hosts: localhost

     tasks:
     – name: Install apache httpd (state=present is optional)
      ansible.builtin.apt:
       name: apache2
       state: present

    2. List all the block sections of an Ansible play.
    Block / rescue / always
    3. What are Ansible inventory parameters? Explain their behavior with an example.
    Inventory parameters are the vars defined in the inventory file which help to customise the ansible behaviour for a specific host of group of hosts.e.g:
    [web:vars]
    ansible_user=ubuntu
    ansible_ssh_private_key_file=node.pem
    ansible_become=yes

    4. What are the different ways to introduce variables in an Ansible playbook?

    1. playbook vars 2. inventory vars 3. task vars 4 tower vars

    5. What are templates in Ansible?
    Template helps in interpolating variables in the files outside the playbook
    6. What are handlers in Ansible?
    Handlers are the special task which are executed only when condition is true like change = true. we can call another task on this condition.

  9. 1. What is an Ansible playbook? Give an example.
    Playbook is used to perform multiple tasks, specify host and variables.
     name: Install and start Apache
      hosts: webservers
      become: yes
      tasks:
        – name: Install Apache
          apt:
            name: apache2
            state: present
    2. List all the block sections of an Ansible play.

    • name: A description of the play.
    • hosts: The target hosts or groups for the play.
    • become: Whether to escalate privileges (e.g., sudo).
    • vars: Variables defined for the play.
    • vars_files: External variable files to include.
    • vars_prompt: Prompts for variables at runtime.
    • tasks: The list of tasks to execute.
    • handlers: Special tasks triggered by notify statements.
    • pre_tasks: Tasks that run before the main tasks.
    • post_tasks: Tasks that run after the main tasks.
    • roles: Roles to include in the play.
    • environment: Environment variables for tasks.
    • tags: Tags to control which tasks run.

    3. What are Ansible inventory parameters? Explain their behavior with an example.
    it allows us to provide host and login information specific to each host in the inventory file.
    [web]
    13.201.50.27 ansible_user=ubuntu ansible_ssh_private_key_file=node.pem ansible_become=yes
    this helps web group to specify the user name and privake key info specifically.

    4. What are the different ways to introduce variables in an Ansible playbook?

    1. Extra vars (command line -e)
    2. Task vars (set by include, import, or block)
    3. Role and include vars (defined in playbook)
    4. Block vars (only for tasks in block)
    5. Play vars (defined in play)
    6. Host facts (gathered or set_fact)
    7. Playbook host_vars and group_vars
    8. Inventory host_vars and group_vars
    9. Inventory file or script group vars
    10. Inventory file or script host vars
    11. Role defaults

    5. What are templates in Ansible?
    templates are used to work on the file edits at the remote hosts. Jinja template is used in the Ansible as python is the base for it. For example, we shall update the config file in the runtime and replace at the remote host.
    6. What are handlers in Ansible?
    It is an action executed when a task comes true. It will be executed at the end of the play. If a task execution succeed, we may need to restart the system. This can be handled through handlers.

  10. 1a) ansible playbook is a configuration file written in YAML.
    2a)block, rescue, always etc
    3a)inventory paramters are variables defined within inventory file that are used to provide specific intructions to ansible.
    4a) invetory variables- host variables, group variables, external variables, command line variables etc.
    5a) templates are text files with .j2 extension.
    6a)handlers are a special type of task that only execute when expicitly triggered by other tasks within a playbook.

  11. 1)Ansible Playbook is a YAML file that contains a list of tasks to be executed on remote machines.
    EX:-

    – name: Update web servers
    hosts: localhost
    vars:
    myname: “khushi”
    port: 8

    2)name, vars,task,handlers,import_tasks

    3) ansible_port: ssh port
    ansible_user: ssh username to connect as
    ansible_ssh_key_private_file:vPath to private ssh key for authentication

    4)vars_file
    vars_prompt
    Playbook vars block
    host vars /group vars

    5)Templates r files usualyy .j2 file that contain dynamic content

    6) Handlers are same as tasks but triggered by notify

  12. 2.List of Block sections in ansible play are name, hosts, become, vars, vars_files, roles, tasks, handlers, tags, notify.. etc.

  13. 1. What is an Ansible playbook? Give an example.
    Play book is program file which contains multiple tasks.

    2. List all the block sections of an Ansible play.
    Task Block, Error Block, Handler Block

    3. What are Ansible inventory parameters? Explain their behavior with an example.
    inventory Behavioral parameters
    4. What are the different ways to introduce variables in an Ansible playbook?
    Task level, file level,

    5. What are templates in Ansible?
    Jinga Template

    6. What are handlers in Ansible?
    In Ansible, handlers are special tasks that runs only when it “notified” by other tasks within a playbook

    1. Playbook is a Collection of tasks that are executed in sequence in the host specified in it.
    2. name, hosts, tasks
    3. specified inventory file with -i and we can pass credentials for different host/ips.
    4. through vars, vars_files, vars_prompt, task_vars, register and 20+ others
    5. to reuse the vars in any files, we use templates
    6. Handlers are tasks that only run when notified.
    1. Ansible playbook? Give an example – An Ansible playbook is a YAML file that define a set of task to be executed on remote servers, example: Name, Hosts, Tasks
    2. block sections of an Ansible play – Play, variables, handlers
    3. Ansible inventory parameters – Ansible inventory parameters defines the hosts and group of host on which ansible will execute tasks, the parameter are specified in inventory files which can be in yaml format., example: Hosts-specific variables, group variables, children variables.
    4. Ways to introduce variables in an Ansible playbook – Variable can introduce by inline variables in playbook, using vars_files, Using inventory, Hosts variables.
    5. Templates in Ansible – Runtime operations to .j2 file.
    6. Handlers in Ansible – In Ansible, handlers are specific task that are triggered by the other tasks using the notify directives. It use to perform actions that should only occurred when a change is made.
  14. 1. What is an Ansible playbook? Give an example.
    Playbook is a yaml file contain the list of plays and tasks 

    2. List all the block sections of an Ansible play.
    hosts, vars, tasks, handlers

    3. What are Ansible inventory parameters? Explain their behavior with an example.
    ansible_port, ansible_user, ansible_ssh_private_key_file

    4. What are the different ways to introduce variables in an Ansible playbook?
    variable in playbook, variable file, task vars

    5. What are templates in Ansible?
    Templates is a module in ansible which is used access the variable in files  

    6. What are handlers in Ansible?
    Handlers are block in playbook which list all the handler tasks. this task is called by normal task when change is true. 

  15. 3.In Ansible, the inventory defines the hosts (machines) and groups of hosts that ansible will manage. The Ansible inventory parameters are ansible_host, ansible_user, ansible_port, ansible_password, ansible_connection, ansible_become, ansible_become_user, ansible_shell_type.

    ansible_host: The actual IP address or DNS name to connect.
    ansible_user: SSH username to connect as
    ansible_port: SSH port (default as 22)
    ansible_password: SSH password
    ansible_connection: Connection type (ssh, local, winrm, etc)
    ansible_become: sudo
    ansible_become_user: User to become (e.g.,root)
    ansible_shell_type: Shell to use (e.g. ssh, csh, powershell)

  16. 4.In Ansible, we can define and use variables in multiple flexible ways to make playbooks dynamic, resuable, and easy to maintain.

    1. Variables in the playbook itself.
    2. Variable Files
    3. Group Variables
    4. Host Variables
    5. Inventory Variables
    6. Extra Variables
    7. Prompted Variables
    8. Facts and Registered Variables
  17. 5.Templates in Ansible are files that contain dynamic content, rendered using the Jinja2 templating engine. These templates are typically used to create configuration files or scripts where values may vary depending on the environment. host, or other variables.

  18. 6.Handlers in Ansible are special tasks that are triggered only when notified by another task. They are typically used to perform actions that should only run if a change has occurred, such as restating a service after a configuration file is updated.

Leave a Reply to Prashanth Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.