Ansible playbooks run multiple tasks, assign roles, and define configurations, deployment steps, and variables. If you’re using multiple servers, Ansible playbooks organize the steps between the assembled machines or servers and get them organized and running in the way the users need them to. Consider playbooks as the equivalent of instruction manuals.
Task-01
1.Write an ansible playbook to create a file on a different server
First of all edit or create the inventory file for ansible according to the public IP's of the server.
Create an Ansible playbook to create a file on a different server
In this playbook, we've set up a simple task to make a file using the file module. The path parameter tells the playbook where to create the file, and the state parameter, set to touch, ensures the file is made if it's not there yet, and if it's already there, it won't change anything.
Run the playbook with the ansible-playbook command:
ansible-playbook your-file-playbook.yml -i <path-to-your-inventory> --private-key=<path-to-your-private-key>
After running the playbook, double-check that the file has indeed been created in the specified location.
To confirm the file's presence on different servers, use this command:
ansible all -a "ls /home/ubuntu" -i <path-to-your-inventory> --private-key=<path-to-your-private-key>
Remember to replace <path-to-your-inventory>
and <path-to-your-private-key>
with the actual paths to your inventory file and private key.
2.Write an ansible playbook to create a new user.
In this playbook, we define a single task that uses the user module to create the new user.
Run playbook using the ansible-playbook command,
To check if the user has been created, look for the user's name in the /etc/passwd file."
3. Write an ansible playbook to install docker on a group of servers
In this playbook, we've got three tasks lined up to get Docker up and running on a group of servers. The hosts parameter tells us which servers are in the game, and with become set to yes, these tasks get some extra muscle to install Docker smoothly.
The first task adds the Docker GPG key to the apt keyring. It's a necessary step to make sure the system can trust the Docker packages. The second task slides Docker into the list of sources where the system looks for software. This lets the system fetch Docker packages from the Docker repository. The final task does the real magic—installing Docker using the apt module. We specify docker-ce as the package name, bringing in the Community Edition of Docker.
To put this playbook into action, just fire up the ansible-playbook command.
After running the playbook, make sure Docker is good to go on multiple servers:
ansible all -a "docker --version" -i <path-to-your-inventory> --private-key=<path-to-your-private-key>
Remember to swap out <path-to-your-inventory>
and <path-to-your-private-key>
with the actual paths to your inventory file and private key.
Task-02
Write a blog about writing ansible playbooks with the best practices.
Ansible, a robust open-source automation tool, empowers you to automate IT infrastructure management. Ansible playbooks, the blueprints of your system's desired state, become more formidable when crafted with best practices. Let's explore some guidelines that enhance the robustness, reliability, and scalability of your Ansible playbooks.
Use a Clear Structure:
Ensure your playbook is well-organized and easy to read. Leverage comments and section headers to elucidate each part's purpose. Avoid excessive nesting, as it hinders readability and maintenance.
Use Idempotent Tasks:
Design tasks with idempotency in mind. This ensures tasks can be executed repeatedly without altering the system state. Modules like apt, yum, and copy support idempotency, preventing unexpected changes.
Example:
- name: Install Apache web server
apt:
name: apache2
state: latest
Use Variables:
Enhance flexibility and reusability with variables. Store values that span multiple tasks and playbooks. Variables can be defined within the playbook or in external files like group_vars or host_vars.
Example:
- name: Configure Nginx web server
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
vars:
nginx_port: 80
Use Roles:
Organize tasks, variables, and files into reusable roles. Roles improve modularity, ease of maintenance, and facilitate sharing functionality across different teams.
Example:
- name: Install web server
hosts: all
roles:
- webserver
Use Tags:
Facilitate selective task execution with tags. Useful for testing, debugging, or running specific subsets of tasks within a playbook.
Example:
- name: Install web server
hosts: all
become: yes
tags:
- webserver
tasks:
- name: Install Apache web server
apt:
name: apache2
state: latest
tags:
- apache
- name: Install Nginx web server
apt:
name: nginx
state: latest
tags:
- nginx
Use Conditionals:
Execute tasks conditionally based on specific criteria. Handy for running tasks on specific hosts or when a variable meets a certain condition.
Example:
- name: Example playbook with a conditional
hosts: all
vars:
myvar: "foo"
tasks:
- name: Task 1
debug:
msg: "This task will always run"
- name: Task 2
debug:
msg: "This task will run if myvar equals 'foo'"
when: myvar == "foo"
By integrating these best practices into your Ansible playbook writing process, you create playbooks that are not only efficient but also adaptable to diverse environments and scenarios. Happy automating!