
ansible-playbook
is a command-line tool in Ansible, an open-source automation tool used for configuration management, application deployment, task automation, and managing IT infrastructure. Ansible uses a declarative language to describe the desired state of the system, and ansible-playbook
is used to execute a collection of tasks defined in a playbook.
Inventory: Ansible uses an inventory file to define the hosts on which it should run tasks. This file typically contains information such as hostnames or IP addresses, connection details, and groupings of hosts.
Example inventory.ini
file:
[webserver]
4.193.143.97
[dbserver]
4.194.182.229
---
- name: Example Playbook
hosts: webserver
become: yes
tasks:
- name: Ensure Nginx is installed
apt:
name: nginx
state: present
- name: Ensure Nginx is running
service:
name: nginx
state: started
Running the Playbook: To execute a playbook, you use the ansible-playbook
command followed by the name of the playbook file:
ansible-playbook -i inventory.ini playbook.yml
Same execution we can perform using terraform local-exec
provisioner "local-exec" {
command = "ansible-playbook './ansible-nginx.yaml' -u adminuser"
}

Result :

The import_playbook
directive in Ansible is used to include another playbook, similar to include_playbook
. However, there is a key difference: import_playbook
is a static import, and it includes the specified playbook at the same level as the tasks in the calling playbook
- The
import_playbook
directive is used to importplaybook1.yml
andplaybook2.yml
. - The tasks in the main playbook will be executed before and after the imported playbooks.
import_playbook
is more suited for cases where you want to control the order of execution of the playbooks.
---
- name: Main Playbook
hosts: all
gather_facts: false
- import_playbook: ansible-conditional.yaml
- import_playbook: ansible-nginx.yaml
Folder structure :

Result :
