Ansible Labs Documentation

This documentation provides step-by-step guides and examples for working with Ansible, covering various topics such as setting up the environment, practicing inventory management, configuration, playbooks, tasks, handlers, roles, and more.

Table of Contents

  1. Setup Environment
  2. Practicing Inventory
  3. Practicing ansible.cfg
  4. Practicing Command Escalation (become)
  5. Practicing Playbook
  6. Install Nginx Task
  7. Practicing Tags
  8. Always Tag
  9. Practicing Variables
  10. Practicing Loops
  11. Practicing When
  12. Practicing Register
  13. Practicing Register & When
  14. Practicing Handler
  15. Practicing Template
  16. Roles Project

Setup Environment

  1. Install Ansible on your machine.

    $ sudo apt install ansible
    
    # Install using pip to ensure all required Python packages are installed
    $ pip install ansible
    
    # Setup Docker environment
  2. Create a key to be associated with the container to SSH into it, in the /.ssh/

    $ ssh-keygen -t rsa -b 2048
  3. Create a Dockerfile for the image

    Dockerfile

  4. Build the image

    $ docker build -t ansible-ubuntu-image .
  5. Create a container using the image

    $ docker run -itd --name "lab1-server01" ansible-ubuntu-image
  6. Get the container IP to connect to it

    $ docker inspect lab1-server01

    The IP is typically 172.17.0.2.

  7. Move the public key to the container to activate it for SSH

    $ ssh-copy-id -i /home/user/.ssh/ansible-key.pub ansible@172.17.0.2

    ssh-copy-id

  8. Now connect to the container using SSH

    $ ssh ansible@172.17.0.2

    ssh-result

Practicing Inventory

  1. Create an inventory file that contains all the IPs in your environment.

    inventory

  2. Ping all the servers using the following command.

    $ ansible all -i ./inventory --private-key /home/h-test/.ssh/ansible-key -u ansible -m ping

    ping-result-01

Practicing ansible.cfg

  1. Write your config file

    ansible-cfg

  2. Now ping the web servers only for example.

    $ ansible webservers -m ping

    ping-result-02

Practicing Command Escalation (become)

If we tried to execute a sudo command that requires a password to has it’s execution privilege. Ansible will response with an error.

$ ansible webservers -m command -a "sudo touch /etc/shadow"

It will cause a runtime error.

So we need to tell ansible that you are going to need the root password, and this gets done using become

To do so, we need to activate the become and password asking either in terminal.

$ ansible webservers -m command --ask-become-pass -b -a "sudo touch /etc/shadow"

or in ansible.cfg

ansible-cfg-become

Practicing Playbook

  1. Write your playbook file in .yml

    playbook

  2. Execute the following command

    $ ansible-playbook pinging-playbook.yml

    playbook-result-01

Install Nginx Task

📢 Task

- Update cache
- Install latest nginx
- Copy index.html from controller to server01
- Restart nginx service
- Can you see your index.html file when you hit server01 on port 80 ?
  1. Write a simple index.html file

    index-html

  2. Write your playbook file

    nginx-task-playbook

  3. Execute the following command

    $ ansible-playbook pinging-playbook.yml
  4. Hit nginx

    Expose nginx port on the host by restarting the container and modifying the container networking.

Practicing Tags

  1. Write your tags-playbook file

    tags

  2. Execute the following command

    $ ansible-playbook tags-playbook.yml --tags install

Always Tag

Using always tag ensures the associated task is executed regardless.

always-tags

Practicing Variables

  1. you can specify your variables in the playbook file directly, as following..

    vairables

  2. Or write them in a separate variables.yml file. and refer to them in the playbook file.

    variables(2) variables(3)
  3. Execute the following command

    $ ansible-playbook variables-playbook.yml

Practicing Loops

  1. Write your loops-playbook file

    loops

    You also can add multiple attributes to the loop item, as following..

    loops(2)

    Also you can store the item values as a variables, as following..

    loops(3)

  2. Execute the following command

    $ ansible-playbook loops-playbook.yml

Practicing When

  1. Write your when-playbook file

    when

    Execute using:

    $ ansible-playbook when-playbook.yml

    when-result

Practicing Register

  1. Write your register-playbook file

    !

    register

    Execute using:

    $ ansible-playbook register-playbook.yml

    register-result

Practicing Register & When

Register & When Playbook

Practicing Handler

Handler Playbook

Practicing Template

Template Playbook


Roles Project

Project Directory Files

  • vars/main.yml

    Vars Main

  • files/index01.html

    Index01 HTML

  • templates/index02.html

    Index02 HTML

  • tasks/main.yml

    Tasks Main

  • handlers/main.yml

    Handlers Main

  • ./playbook.yml

    Playbook

Run the following command

$ ansible-playbook playbook.yml

Result

Roles Project Result


This documentation is a comprehensive guide to using Ansible for various tasks and projects. Each section provides detailed instructions and examples for practical implementation.

For more information and detailed usage, refer to the respective sections in this document.