Ansible

How to Pass Ansible Username And Password?

Ansible is a modern open-source automation tool that makes it easier to configure and manage remote servers. Although other automation tools can match ansible’s usability, they are often too complex than a basic automation tool needs to be.

Ansible, on the other hand, is simple and easy to use for most users. It uses the YAML format to specify server configurations and tasks executed on remote machines. It also offers a great security feature by using SSH as the default mode of authentication.

However, in some instances, you may not have configured SSH keys on a remote host and thus need to specify the username and password explicitly

If that’s the case, this guide will discuss creating basic playbooks and running them on a remote host that does not have SSH keys configured.

Installing Ansible

Before you can use ansible to manage your remote machines, you need to install it on your machine, which acts as the control node.

In my example, I will show how to install it on REHL/CentOS and Fedora, in which case all I have to do is use the command:

$ sudo dnf install ansible

This command will install ansible on your machine, giving you all the tools to control all your remote machines from a single location.  The next step is to set up the host file.

Setting up Ansible Hosts Inventory

In ansible, an inventory is a file containing information about your remote hosts that will be under ansible’s management. Hosts in the inventory file can be organized into groups and sub-groups and specified either with an IP address of the remote machine or the hostname.

To add or remove hosts, edit the file located in /etc/ansible/hosts.

$ sudo vim /etc/ansible/hosts

NOTE: the default hosts file might not be included by default in some installations. Simply create one if none exists.

Inside the hosts file, add the entries as:

[myservers]

192.168.0.20

192.168.0.13

The above defines a group called myservers and their corresponding IP addresses. Be sure to change the above IP addresses to your hosts’ IP addresses.

Test Connection

Once you set up your inventory file, it’s best to check if your hosts are online and can receive commands from ansible. We do this by calling the Ansible ping module.  On your ansible local node, use the command shown below:

$ ansible all -m ping -u ubuntu --ask-pass

This will use the ubuntu username and ask for the SSH password. Feel free to change the username to any desired user on your remote machine. If the remote host is up and you provide the correct SSH password, you should see a response as:

192.168.0.7 | SUCCESS => {

    "ansible_facts": {

        "discovered_interpreter_python": "/usr/bin/python3"

    },

    "changed": false,

    "ping": "pong"

}

Quick Recap

As you can see from the above example, in ansible, to specify the username and password to use for your remote hosts, we use the -u [username] and –ask-pass for the password.

For example, to use the root account as the ansible login, use the command as:

$ ansible all -m ping -u root --ask-pass

NOTE: It is ideal to use username and password as the mode of authentication in ansible. Use SSH keys if possible.

Conclusion

In this tutorial, we discussed how to install and create ansible hosts. We also covered how to specify ansible username and password as the mode of authentication. All this can come in handy if you haven’t configured SSH keys on the remote machine.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list