Ansible arbitrary hostname resolution

Is there a way to resolve an arbitrary string as the host name in the Ansible file group_varsor in the Jinja2 template used by Ansible? Let's say I want to define a variable in global_vars/allwhich will contain one of several IP addresses allowed www.google.com. In this example, I used www.google.comas an example a string that can be resolved to multiple IP addresses, and yet I cannot use Ansible hostvarsfor the address, because I cannot ssh into it.

I tried connecting to Pythonic socket.gethostbyname()but could not get the syntax correctly. At best, my variable became literal "socket.gethostbyname (" my-host-1 ")".

I know that I can go back to the shell script and use the tools available in the shell, but I would like to see if there is an elegant way to do this in Ansible.

The more details of the question, the more I must fill out the Postgres HBA configuration file with the IP addresses of the allowed hosts. I cannot use my hostnames because the target deployment does not have the reverse DNS required for HBA based on the hostname.

I really want Postgres to resolve the names in the configuration file and map it to the client IP address instead of looking back at the client IP address, and then match the hostname strings. But this is too much to expect and wait too long. I need a workaround for now, and I would like to stay in Ansible for this, without having to unload it into an external script.

, !

+4
3

:

Ansible 1.x:

import ansible.utils as utils
import ansible.errors as errors
import socket

class LookupModule(object):

    def __init__(self, basedir=None, **kwargs):
        self.basedir = basedir

    def run(self, terms, inject=None, **kwargs):

        if not isinstance(terms, basestring):
            raise errors.AnsibleError("ip lookup expects a string (hostname)")

        return [socket.gethostbyname(terms)]

Ansible 2.x:

import ansible.utils as utils
import ansible.errors as errors
from ansible.plugins.lookup import LookupBase
import socket

class LookupModule(LookupBase):

    def __init__(self, basedir=None, **kwargs):
        self.basedir = basedir

    def run(self, terms, variables=None, **kwargs):

        hostname = terms[0]

        if not isinstance(hostname, basestring):
            raise errors.AnsibleError("ip lookup expects a string (hostname)")

        return [socket.gethostbyname(hostname)]

lookup_plugins/ip.py.

{{ lookup('ip', 'www.google.com') }}

+8

lookup udondan , "" ( API 2.0, , , ). - , :

- hosts: yourhosts
  tasks:
  - local_action: shell dig +short host-to-lookup-here.com
    changed_when: false
    register: dig_output

  - set_fact:
      looked_up_ips: "{{ dig_output.stdout_lines }}"

  - debug: msg="found ip {{ item }}"
    with_items: looked_up_ips

look_up_ips IP-, , , ...

+2

For completeness, apparently, we are sending a plugin to search for codes in the box, starting at 1.9, to wrap it nicely (h / t Duncan Hutty):

https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/lookup/dig.py

0
source

All Articles