Ansible Playbooks

Ansible Playbooks

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.

Tasks:

Write An Inventory file (inventory.ini):

[remote_server]
server1 ansible_host=54.162.242.208 ansible_ssh_private_key_file=/home/ubuntu/keys/ansible-key.pem ansible_ssh_user=ubuntu

This inventory file defines servername as server1 , Public IP address for the remote server- Replace the IP address, ansible_ssh_private_key_file location and ansible_ssh_user with your server details.

1) Write an ansible playbook to create a file on a different server and Verify.

Here's a basic Ansible playbook that will create a file on a remote server:

Playbook (createfile.yml):

---
- name: Create a file on a remote server and verify its existence
  hosts: remote_server
  become: yes  # Run tasks with sudo if needed

  tasks:
    - name: Create a file called 'sample.txt'
      ansible.builtin.file:
        path: /tmp/sample.txt
        state: touch

    - name: Verify that the file 'sample.txt' exists
      ansible.builtin.stat:
        path: /tmp/sample.txt
      register: result

    - name: Print verification result
      ansible.builtin.debug:
        msg: "The file /tmp/sample.txt exists on the server."
      when: result.stat.exists

    - name: Fail if file does not exist
      ansible.builtin.fail:
        msg: "The file /tmp/sample.txt does not exist on the server."
      when: not result.stat.exists

This playbook has a single play that targets the remote_server group from the inventory. The play has one task that uses the file module to create (or touch) a file at /tmp/sample.txt.

Run the playbook: Execute the playbook with:

ansible-playbook -i inventory.ini createfile.yml

Some things to consider:

  • The become: yes directive is used to run the task as a superuser (sudo) on the remote server. If your user doesn't have sudo permissions or if you're creating the file in a location that doesn't require sudo, you can remove this directive.

  • Ensure your Ansible control machine can access the remote server using SSH. If you're using SSH keys for authentication (recommended), ensure your public key is present in the ~/.ssh/authorized_keys file of the ansible_ssh_user on the remote server.

  • Modify the path in the playbook to specify a different location or filename for the file you're creating.

2) Write an ansible playbook to create a new user.

Here's a simple Ansible playbook that creates a new user and then verifies its existence:

Playbook (createuser.yml):

---
- name: Create and verify a new user
  hosts: all
  become: yes
  vars:
    username: newuser

  tasks:
    - name: Create a new user
      user:
        name: "{{ username }}"
        state: present
        shell: /bin/bash

    - name: Verify the new user exists
      command: id "{{ username }}"
      register: user_check_result
      changed_when: false

    - name: Print verification result
      debug:
        msg: "The user {{ username }} exists with details: {{ user_check_result.stdout }}"

Explanation:

  1. We specify hosts: all to target all hosts in the inventory, but this can be adjusted to target a specific group or host.

  2. The become: yes directive is used to execute tasks with superuser privileges.

  3. We declare a variable username for the new user's name. This makes it easy to modify the user name in the future.

  4. The user module is employed to create a user with a /bin/bash shell.

  5. We then use the command module to execute the id command, verifying the user's existence. The result of this command is registered in the user_check_result variable.

  6. The debug module displays the result, confirming the user's creation.

Usage:

  1. Ensure you have an appropriate inventory file.

  2. Run the playbook with:

ansible-playbook -i inventory.ini createuser.yml

Make sure you replace your_inventory_file.ini with the path to your Ansible inventory.

3) Write an ansible playbook to install docker on a group of servers

Here's an Ansible playbook to install Docker on a group of Ubuntu servers and then verify the installation:

Playbook (install_docker.yml):

---
- name: Install Docker on servers
  hosts: remote_servers
  become: yes
  tasks:
    - name: Update the apt package index
      ansible.builtin.apt:
        update_cache: yes

    - name: Ensure apt-transport-https, ca-certificates, curl, software-properties-common are installed
      ansible.builtin.apt:
        name: ['apt-transport-https', 'ca-certificates', 'curl', 'software-properties-common']
        state: present

    - name: Add Docker's official GPG key
      ansible.builtin.apt_key:
        url: https://download.docker.com/linux/ubuntu/gpg
        state: present

    - name: Add Docker apt repository
      ansible.builtin.apt_repository:
        repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
        state: present

    - name: Install Docker CE
      ansible.builtin.apt:
        name: docker-ce
        state: present
        update_cache: yes

    - name: Verify Docker installation
      command: docker --version
      register: docker_version_result
      changed_when: false

    - name: Display Docker version
      debug:
        msg: "Installed Docker version: {{ docker_version_result.stdout }}"

Explanation:

  1. The playbook targets the docker_servers group from the inventory. You can adjust this to match the group name in your inventory that you wish to target.

  2. We use the become: yes directive to run tasks with superuser privileges, which is required for software installation.

  3. The playbook first ensures that necessary packages are installed before adding the Docker GPG key and repository.

  4. Then, Docker CE (Community Edition) is installed.

  5. Finally, the installation is verified by fetching the Docker version and displaying it.

Usage:

Make sure the servers you want to target are grouped under docker_servers in your Ansible inventory file. For example:

Run the playbook:

ansible-playbook -i inventory.ini install_docker.yml

Make sure to replace your_inventory_file.ini with the path to your Ansible inventory.

Also, note that this playbook is tailored for Ubuntu servers. Adjustments might be needed for other distributions. Always test the playbook in a safe environment before running it in production.

Thus, you have installed Docker on the Remote Server.

Conclusion

Ansible playbooks offer a clear, powerful, and efficient method to manage configurations, orchestrate deployments, and automate almost any IT process. Whether you're a beginner or an expert, there's always more to explore with Ansible. So, start scripting, and let the automation begin!

This blog is a basic introduction. There's a lot more depth and nuance to working with Ansible playbooks, and as you gain experience, you'll discover the many advanced features and techniques to optimize your automation tasks.

Thanks for reading! 😃🙏