Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Your First Playbook | Getting Started with Ansible
Introduction to Ansible

bookYour First Playbook

Creating a hosts.ini File for Local Use

To run Ansible tasks on your own machine, you need to define an inventory file that tells Ansible where to connect. The most common format for this file is called hosts.ini.

Example hosts.ini File

[local]
localhost ansible_connection=local

Line-by-Line Explanation

  • [local];
    • Defines a group called local. Group names are enclosed in square brackets. You can use any name, but local is clear and descriptive;
  • localhost ansible_connection=local;
    • Adds a host named localhost to the local group;
    • The ansible_connection=local part tells Ansible to run commands directly on your machine, rather than connecting over SSH or other protocols.

This simple file setup allows you to practice Ansible playbooks on your own computer without needing to connect to remote servers.

Simple Ansible Playbook Example

Below is a basic Ansible playbook written in YAML. This playbook prints a message on your local server. It uses an inventory file called hosts.ini that defines which servers Ansible will manage.

---
- name: Print a message on the local server
  hosts: local
  tasks:
    - name: Display Hello World
      ansible.builtin.debug:
        msg: "Hello, Ansible!"

Step-by-Step Explanation

  • - name: Print a message on the local server
    • Provides a human-readable name for the playbook, describing its purpose;
  • hosts: local
    • Tells Ansible to run the playbook on the group called local defined in your hosts.ini inventory file;
  • tasks:
    • Begins the list of actions Ansible will perform;
  • - name: Display Hello World
    • Gives a clear label to this specific task;
  • ansible.builtin.debug:
    • Uses Ansible's built-in module to print messages to the console;
  • msg: "Hello, Ansible!"
    • Sets the message that will be displayed when the playbook runs.

Running the Playbook from the Terminal

To execute your playbook, use the following command in your terminal:

ansible-playbook -i hosts.ini playbook.yml
  • ansible-playbook runs the playbook engine;
  • -i hosts.ini tells Ansible to use your inventory file;
  • playbook.yml is the name of your playbook file.

You will see output showing the playbook's progress and the message "Hello, Ansible!" displayed on your screen.

question mark

What does ansible_connection=local mean in the hosts.ini file?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 4

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Awesome!

Completion rate improved to 8.33

bookYour First Playbook

Stryg for at vise menuen

Creating a hosts.ini File for Local Use

To run Ansible tasks on your own machine, you need to define an inventory file that tells Ansible where to connect. The most common format for this file is called hosts.ini.

Example hosts.ini File

[local]
localhost ansible_connection=local

Line-by-Line Explanation

  • [local];
    • Defines a group called local. Group names are enclosed in square brackets. You can use any name, but local is clear and descriptive;
  • localhost ansible_connection=local;
    • Adds a host named localhost to the local group;
    • The ansible_connection=local part tells Ansible to run commands directly on your machine, rather than connecting over SSH or other protocols.

This simple file setup allows you to practice Ansible playbooks on your own computer without needing to connect to remote servers.

Simple Ansible Playbook Example

Below is a basic Ansible playbook written in YAML. This playbook prints a message on your local server. It uses an inventory file called hosts.ini that defines which servers Ansible will manage.

---
- name: Print a message on the local server
  hosts: local
  tasks:
    - name: Display Hello World
      ansible.builtin.debug:
        msg: "Hello, Ansible!"

Step-by-Step Explanation

  • - name: Print a message on the local server
    • Provides a human-readable name for the playbook, describing its purpose;
  • hosts: local
    • Tells Ansible to run the playbook on the group called local defined in your hosts.ini inventory file;
  • tasks:
    • Begins the list of actions Ansible will perform;
  • - name: Display Hello World
    • Gives a clear label to this specific task;
  • ansible.builtin.debug:
    • Uses Ansible's built-in module to print messages to the console;
  • msg: "Hello, Ansible!"
    • Sets the message that will be displayed when the playbook runs.

Running the Playbook from the Terminal

To execute your playbook, use the following command in your terminal:

ansible-playbook -i hosts.ini playbook.yml
  • ansible-playbook runs the playbook engine;
  • -i hosts.ini tells Ansible to use your inventory file;
  • playbook.yml is the name of your playbook file.

You will see output showing the playbook's progress and the message "Hello, Ansible!" displayed on your screen.

question mark

What does ansible_connection=local mean in the hosts.ini file?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 4
some-alt