Your 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, butlocalis clear and descriptive;
- Defines a group called
- localhost ansible_connection=local;
- Adds a host named
localhostto thelocalgroup; - The
ansible_connection=localpart tells Ansible to run commands directly on your machine, rather than connecting over SSH or other protocols.
- Adds a host named
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
localdefined in yourhosts.iniinventory file;
- Tells Ansible to run the playbook on the group called
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-playbookruns the playbook engine;-i hosts.initells Ansible to use your inventory file;playbook.ymlis the name of your playbook file.
You will see output showing the playbook's progress and the message "Hello, Ansible!" displayed on your screen.
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Can you explain what an inventory file is in Ansible?
What other connection types can I use besides `local`?
How do I create and save a `hosts.ini` file on my computer?
Awesome!
Completion rate improved to 8.33
Your First Playbook
Glissez pour afficher le menu
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, butlocalis clear and descriptive;
- Defines a group called
- localhost ansible_connection=local;
- Adds a host named
localhostto thelocalgroup; - The
ansible_connection=localpart tells Ansible to run commands directly on your machine, rather than connecting over SSH or other protocols.
- Adds a host named
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
localdefined in yourhosts.iniinventory file;
- Tells Ansible to run the playbook on the group called
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-playbookruns the playbook engine;-i hosts.initells Ansible to use your inventory file;playbook.ymlis the name of your playbook file.
You will see output showing the playbook's progress and the message "Hello, Ansible!" displayed on your screen.
Merci pour vos commentaires !