pyinfra-dev / pyinfra

pyinfra turns Python code into shell commands and runs them on your servers. Execute ad-hoc commands and write declarative operations. Target SSH servers, local machine and Docker containers. Fast and scales from one server to thousands.
https://pyinfra.com
MIT License
3.91k stars 382 forks source link

How to obtain currently connected host name or IP address? #749

Open jmpolom opened 2 years ago

jmpolom commented 2 years ago

Example inventory:

hardware = [
    'hostname1',
    'an.fqdn.example.org',
    '192.168.1.100'
]

How can I obtain, from within a pyinfra deploy, the value of the current inventory item the deploy is running on? This is not clear from the documentation.

host.hostname seems to gather a fact but I'm not looking to query the host for anything -- just find out the current connection as described in the inventory.

Fizzadar commented 2 years ago

host.name should achieve this for you @jmpolom! Will keep this open as a docs issue 👍

PermanAtayev commented 2 years ago

I also think it would be great to have this in documentation :)

Taisgeal commented 2 years ago

And me.

sanzoghenzo commented 1 year ago

What about if one wants to know the IP of the target host, even if it's specified in the inventory as host name?

My use case is that my inventory uses the names from .ssh/config file, and I would like to get the IP to do some stuff (like setting and internal dnsmasq DNS server to point to the containerized web services in it).

If I get the Ipv4Addrs fact, I get the IPs of all the virtual networks (docker, kvm) along with the physical interface, so it's hard to extract something useful without prior knowledge of the name of the ethernet interface name.