Ansible Tutorial Without AWS

·

3 min read

Question:

You must be wondering, how can I do the Ansible hands-on project without going to AWS? And control multiple servers from one master server.

Solution:

So, I come up with the solution, we can create multiple Vagrant boxes with different IP addresses in a virtual machine of the Ubuntu operating system.

So, we can ssh into another Vagrant Box from one Vagrant Box.

So, get your vagrant file and virtual machine ready. And let's get started.


How to create 2 vagrant files?

  • If you have one vagrant file. Copy it and Paste it into another directory by changing its IP address.

  • Just change the IP address.

  • Create two Vagrant files with different IP

  • In Hyper Terminal(recommended) or WSL Terminal open both vagrant files simultaneously

  • From one terminal tab ssh to another Vagrantfile using another IP address. Use sudo ssh vagrant@IPaddress command.

  • Here, as you can see I ssh into 192.168.33.11 IP address from 192.168.33.10 server.

  • Now, exit the 192.168.33.11 server and come back to the first vagrant file.

How to create an Inventory?

  • Create a file named "hosts" in the "ansible" named directory. Using mkdir ansible

  • In the hosts file put the IP address of the 2nd Vagrantfile

[servers]
server1 ansible_host=192.168.33.11

[all:vars]
ansible_python_interpreter=/usr/bin/python3
  • Use the command, to list the 2nd Vagrantfile IP

    address in the ansible inventory

    • ansible-inventory --list -y -i /home/vagrant/ansible/hosts
  • Now check whether the connection between the 1st Vagrantfile and the 2nd Vagrantfile is established or not.

    • By using the command:

    • ansible all -i /home/vagrant/ansible/hosts -m ping --ask-pass

    • Here, maybe you need to install sshpass. Use the command: sudo apt install sshpass

    • Now, enter the password: vagrant. If you didn't change it.

Now, we have established the connection between both the Vagrantfiles.

Let us create a playbook

Aim: Create a file in the 2nd Vagrantfile from the 1st Vagrantfile.

  • Come to /home/vagrant/ansible directory from whichever directory you are in.

  • And create a directory named "playbooks". Using the command: mkdir playbooks

  • Now, go to the playbook directory. Using cd playbooks

  • Now, create a YAML file named create_file.yaml

  • Using the command: vim create_file.yaml | nano create_file.yaml | touch create_file.yaml

      ---
      - name: This will create a file
        hosts: all
        become: true #meaning, it will become a root user
        tasks:
        - name: Create a file
          file:
           path: /home/vagrant/example.txt
           state: touch
    
  • Now run this command. And then the txt file will be created in the 2nd Vagrantfile.

  • ansible-playbook create_file.yaml -i /home/vagrant/ansible/hosts --ask-pass

  • Now, a text file will be created in the 2nd Vagrantfile.

  • Here, as you can see, example.txt is created in the server 192.168.33.11

  • That's all

References:

Here, Shubham sir had done all things in AWS. And I used vagrant for the same.