Simple Ansible Playbook Assignment
Create a file using the below command in your directory
$ sudo nano Simple_Playbook_Example.yml
Paste the below code in the YAML file which we created as shown in the below screenshot
Now execute the playbook with the below command
$ ansible-playbook Simple_Playbook_Example.yml
$ cat /home/Pranjali/Documents/example_directory/example_file.txt
1. Create a role with the below command with the name as “ansible_role”
$ ansible-galaxy init ansible_role
This will create a directory named ansible_role with a predefined structure
Install the tree with the below command to view the predefined structure of created
ansible_role.
$ sudo apt-get install tree
Navigate to the directory “ansible_role”
$ cd ansible_role
Use the tree command to view the structure.
$ tree
Explanation:
defaults/main.yml
Purpose: Contains default variables for the role.
Usage: Provides default values that can be overridden by other variable definitions.
files/
Purpose: Holds static files that need to be deployed to the target machines.
Usage: Store files to be copied to remote hosts.
handlers/main.yml
Purpose: Contains handlers, which are tasks triggered by other tasks.
Usage: Typically used to restart services or notify other tasks when changes occur.
meta/main.yml
Purpose: Stores metadata about the role, such as dependencies.
Usage: Define the role’s dependencies and any special information about the role.
README.md
Purpose: Provides documentation for the role.
Usage: Describe the role’s functionality, usage, and important information.
tasks/main.yml
Purpose: Contains the main list of tasks to be executed by the role.
Usage: Define the steps required to configure or set up the application or service.
templates/
Purpose: Stores Jinja2 templates that can be rendered and deployed to the remote servers.
Usage: Keep template files to be populated with variables and deployed.
tests/inventory
Purpose: Inventory file for testing the role.
Usage: Define the hosts and groups for testing purposes.
tests/test.yml
Purpose: Playbook for testing the role.
Usage: Use this playbook to test the role in a controlled environment.
vars/main.yml
Purpose: Contains variables that are specific to the role.
Usage: Define variables used throughout the role that are not intended to be overridden.
Ansible Variables
Ansible variables are placeholders that store data and can be used throughout your
playbook. They allow you to make your playbooks dynamic and adaptable to different
environments.
Create a YAML file in your desired directory and copy paste the below code in it to know
more about how to use variables and make sure the directory /var/www/html is writable (If
not execute $ sudo chmod -R 777 /var/www/html).
$ sudo nano var_localhost.yml
website_name: Stores the name of the website.
document_root: Stores the path to the document root directory where website files are
stored.
index_file_content: Stores the content for the index.html file
2. Execute the playbook with the ansible-playbook command
$ ansible-playbook var_localhost.yml
Check the output of index.html file by navigating to the URL http://localhost/index.html as
we defined to print the message as “Hello, world! This is my website.”
Check the apache status with the below command as we defined to restart the apche in
tasks.