How to use SSH config to create Ansible Inventory.

Jagrit Thapar

522 words • 3 min read

To build an Ansible inventory file from an ssh config file with all ssh hosts and their private key, you can follow these steps:

1. Install Ansible on your system.

You can install Ansible using pip, the Python package manager. Open a terminal and run the following command:

pip install ansible

2. Create an Ansible inventory file.

Ansible inventory files can be in INI or YAML format. For this example, we will use the INI format. Create a file called inventory.ini and add the following content:

[webservers]
webserver01 ansible_host=192.0.2.140 http_port=80
webserver02 ansible_host=192.0.2.150 http_port=443

[datacenter]
webservers

[all:vars]
ansible_user=my_server_user

Replace webserver01 and webserver02 with the hostnames or IP addresses of your servers. Replace 192.0.2.140 and 192.0.2.150 with the IP addresses of your servers. Replace my_server_user with your SSH user.

3. Add variables to the inventory file.

You can add variables to the inventory file to specify the private key for each server. Add the following content to the inventory.ini file:

[webservers:vars]
ansible_ssh_private_key_file=/path/to/private/key/for/webserver01

[webservers]
webserver01 ansible_host=192.0.2.140 http_port=80
webserver02 ansible_host=192.0.2.150 http_port=443

[datacenter]
webservers

[all:vars]
ansible_user=my_server_user

Replace /path/to/private/key/for/webserver01 with the path to the private key for webserver01. Repeat this step for each server.

4. Verify the inventory file.

You can verify the inventory file using the ansible-inventory command. Run the following command:

ansible-inventory -i inventory.ini --list

This command will display the hosts and groups in the inventory file.

5. Test the inventory file.

You can test the inventory file by running an Ansible playbook. Create a file called playbook.yml and add the following content:

---
- hosts: webservers
  tasks:
    - name: Ping webservers
      ping:

Run the following command to test the inventory file:

ansible-playbook -i inventory.ini playbook.yml

This command will ping the servers specified in the webservers group.

You can also use the ansible-inventory-to-ssh-config tool to update the ssh config file with the information from the Ansible inventory file[1]. This tool can update the ssh config file with the information from the Ansible inventory file, including the hostnames, IP addresses, private key files, and any other variables that are defined in the inventory file.

For example, to update the ssh config file from an inventory file called hosts and output the result to a file called newconfig, you would use the following command:

$ aitsc hosts -o newconfig

This would update the ssh config file with the information from the hosts inventory file and save the result to the newconfig file.

Conclusion

In summary, to build an Ansible inventory file from an ssh config file with all ssh hosts and their private key, you can create an Ansible inventory file in INI or YAML format, add variables to the inventory file to specify the private key for each server, verify the inventory file using the ansible-inventory command, and test the inventory file by running an Ansible playbook. You can also use the ansible-inventory-to-ssh-config tool to update the ssh config file with the information from the Ansible inventory file.

Citations:

[1] https://docs.ansible.com/ansible/latest/network/getting_started/first_inventory.html [2] https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html [3] https://docs.ansible.com/ansible/latest/getting_started/get_started_inventory.html [4] https://www.redhat.com/sysadmin/ansible-dynamic-inventories [5] https://www.youtube.com/watch?v=IPml_U9DCnM