Ansible is a powerful automation tool used by IT professionals to manage and configure systems at scale. Whether you're setting up servers, deploying applications, or orchestrating complex workflows, Ansible provides a streamlined, efficient approach to automation. In this post, we'll walk through the process of troubleshooting and creating a simple Ansible playbook on a jump host, where a team member left off.
Step 1: Updating the Inventory File
The inventory file in Ansible is crucial as it defines the hosts and groups of hosts upon which commands, modules, and tasks in your playbook will operate. In our scenario, the inventory file is located at /home/thor/ansible/inventory
and needs adjustments to ensure the playbook runs on App Server 2 in the Stratos DC.
Here’s how you can update your inventory file:
Open the inventory file with a text editor:
vi /home/thor/ansible/inventory
Add or update the following line to specify App Server 2 (
stapp02
):stapp02 ansible_host=stapp02.stratos.xfusioncorp.com ansible_user=user ansible_password=password ansible_ssh_common_args='-o StrictHostKeyChecking=no'
This line defines:
stapp02
as the host.The connection details: host address, user credentials, and SSH options.
Step 2: Creating the Playbook
A playbook is a YAML file that describes a series of tasks to be executed on specified hosts. Our objective is to create a playbook at /home/thor/ansible/playbook.yml
that includes a task to create an empty file (/tmp/file.txt
) on App Server 2.
Follow these steps to create the playbook:
Open a new file in a text editor:
vi /home/thor/ansible/playbook.yml
Insert the following content:
--- - name: Create an empty file hosts: stapp02 remote_user: root tasks: - name: Touch a file file: path: /tmp/file.txt state: touch mode: u=rw,g=r,o=r
This playbook contains:
hosts: stapp02
: Specifies that the playbook should run on App Server 2.remote_user: root
: Executes the tasks with root privileges.A task named `Touch a file` that uses the Ansible
file
module to create an empty file at/tmp/file.txt
with specific permissions.
Step 3: Running the Playbook
Once the inventory file and playbook are set up, you can validate the playbook by running it with the following command:
ansible-playbook -i /home/thor/ansible/inventory /home/thor/ansible/playbook.yml
Ensure that you run this command from the directory where the inventory and playbook files are located or specify the correct paths.
Conclusion
In this tutorial, we've successfully updated an Ansible inventory file and created a playbook to perform a simple task on a specified server. Ansible's flexibility and power make it an invaluable tool for IT automation, and getting comfortable with basic tasks like these is a great step towards mastering it.
By following the outlined steps, you should be able to troubleshoot and create Ansible playbooks with confidence. Happy automating!