Search code examples
ansibleansible-awx

How to access the hostname from a new Python module in Ansible


I am planning to implement a new Ansible module which will run via a local connection and make remote API calls to the host. Is there any way in which I could access the name of the host/IP address from the module? I would like to make a decision on what API to use based on that.


Solution

  • I'm assuming that your doing something like, say, operating on a network appliance from a jump host.

    Right now I'm not seeing where you get the inventory_host in the ansiable module base class, but what I often do in this case is:

    ansible_connection: local
    tasks: 
      - name: config net app
        myconfig: action=create_rule host={{inventory_host}}
    

    Then make host a parameter of your module.

    Then my inventory file might be something like:

    [firewalls]
    host123
    host456
    host789