I made a CLI Application to make my DevOps life easier

As a DevOps Engineer, I specialize in working with Linux and Python. My experience and expertise in these areas have been the foundation of my career. In my current role, I frequently interact with servers, both cloud-based and dedicated, using SSH and Ansible. To efficiently manage a large number of servers and their information, I developed a CLI tool to assist me.

The Problem

Let’s say I have got a piece of server information in this format, which came from some project manager or customer:

IP: 192.168.0.101
User: ubuntu
Port: 22

And got a private key. What do I do normally?

I can try to get into the host via

% ssh -i Downloads/the-private-key ubuntu@192.168.0.101 -p 22

So, every time to create the communication I need to type that full line or dig through history by using some “up arrow”.

To make it easier, I create SSH configuration for servers.

In .ssh/config file, I do configure a host like below

Host secretServer1
HostName 192.168.0.101
Port 22
User ubuntu
IdentityFile Downloads/the-private-key

Thus, I can connect the server by using

% ssh secretServer1

What if I need to run some ansible-playbook on this server? Need an inventory entry again… :(

What does this tool do?

  1. It creates a host database.

  2. Create SSH config from that host database.

  3. Create Ansible inventory from that same host database.

Introduction to the Tool

I named the tool “sshc”. You can install it via the Pythons “pip” command.

% pip3 install --upgrade sshc

Underlying Bits

I love working with python. To make it simple and light, at first, I preferred JSON for Ansible Inventory. But later thought to give the option to generate Yaml.

CLI tool is generated using mostly

  1. argparse

  2. json

  3. pyyaml

  4. uuid

and others. I used argparsefor a similar reason. I could choose Fire, Click, or others, but preferred plain and simple argparse.

For publishing to PyPi, I have used Poetry. It helped me easily publish the tool in PyPi for easy distribution through pip. It uses a pyproject.toml file about publishing information.

The repository of sshc is hosted in GitHub and I have set up GitHub actions to do some automated tasks like a linting check, unit test, poetry install and check command, and finally publish upon releasing a tag/version.

When I am writing this article it has a solid release: 1.0.0

$ sshc --version
sshc, v1.0.0
$ sshc --help
usage: sshc [-h] [--version]
            {init,insert,delete,read,generate} ...

SSH Config and Ansible Inventory Generator !

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit

subcommands:
  The main command of this CLI tool.

  {init,insert,delete,read,generate}
                        The main commands have their own
                        arguments.
    init                Initiate Host DB !
    insert              Insert host information !
    delete              Delete host information !
    read                Read Database !
    generate            Generate necessary config files !

To know more I suggest you visit: https://github.com/fahadahammed/sshc

Conclusion

This tool is in the early stage. I welcome any suggestions or feedback on it. If you have any, please feel free to submit an issue in the repository or message me on LinkedIn.

Thank you.