Ansible's dynamic inventory file (2)

  The last part mainly explained the installation and configuration of Ansible, and stored the server information in Ansible's Inventory according to different business scenarios. In fact, every time such data is stored, we need to automatically add and delete it, so that we can maintain it. It is very inconvenient, and Ansible provides support for dynamically obtaining host list and host group data from the outside; in general, dynamically obtaining host information will be used in conjunction with CMDB, Zabbix, Cpbbler, and cloud host management platforms (such as AWS, Alibaba Cloud);

  Ansible dynamic inventory generates json data in a specified format by calling an external script. There is no language restriction here, as long as the specified format can be returned. Let's see what format we need to return;

{
         " webserver " :   #define webserver group             {
             " hosts " : [ " 39.105.0.244 " ],   # webserver group host " vars " : {   # parameter " ansible_ssh_pass " : " 123456 " ,   # Inventory built-in variable " ansible_port " : " 61821 "

            
                
                
                }
            },
        "dbserver":
            {
            "hosts": ["47.104.149.180"],
            "vars": {
                "ansible_ssh_pass": "123456",
                "ansible_port": "61821"
                    }
            }
        }

  The variables above hosts, vars and children are fixed. We cannot define them freely in the returned json format. If the definition is something else, an error will occur. Here we convert the above json data into static inventory data. is how to map

[webserver]
    39.105.0.244

[dbserver]
    47.104.149.180

[web server: vars]
    ansible_ssh_pass: "123456",
     ansible_port: "61821"

[dbserver: vars]
     ansible_ssh_pass: "123456",
     ansible_port: "61821"

  In this way, we can correspond the mapping relationship between static inventory and dynamic inventory data, and dynamic only obtains data from other places and combines them into data identifiable by Ansible Inventory;

Next, we implement a demo program through the python language. Here we define our data source as a variable. Let's take a look at the example below.

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import json


def inventory_groups():
    groups = \
        {
        " webserver " :   #define webserver group             {
             " hosts " : [ " 39.105.0.244 " ],   # webserver group host " vars " : {   # parameter " ansible_ssh_pass " : " 123456 " ,   # Inventory built-in variable " ansible_port " : " 61821 "

            
                
                
                }
            },
        "dbserver":
            {
            "hosts": ["47.104.149.180"],
            "vars": {
                "ansible_ssh_pass": "123456",
                "ansible_port": "61821"
                    }
            }
        }

    return json.dumps(groups, indent=4)


if __name__ == '__main__':
    print(inventory_groups())

We run the script we wrote under Ansible, first we specify the webserver to perform the ping operation

    ansible -i inventory.py webserver -m ping

After executing the command it returns:

The result set returned here is consistent with the result returned by the server group we configured in /etc/ansible/hosts. In fact, dynamic just makes it more convenient for you to maintain the server group data.

Notice:

  The ansible -i parameter represents the specified Inventory file, but after we specify the file, we also need to specify the running server group or server. If all servers are required to be executed, we will implement all here.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324802435&siteId=291194637