# salt.states.host

## Management of addresses and names in hosts file

The `/etc/hosts` file can be managed to contain definitions for specific hosts:

    salt-master:
      host.present:
        - ip: 192.168.0.42

Or using the `names` directive, you can put several names for the same IP. (Do not try one name with space-separated values).

    server1:
      host.present:
        - ip: 192.168.0.42
        - names:
          - server1
          - florida

Note

Changing the `names` in `host.present` does not cause an update to remove the old entry.

    server1:
      host.present:
        - ip:
          - 192.168.0.42
          - 192.168.0.43
          - 192.168.0.44
        - names:
          - server1

You can replace all existing names for a particular IP address:

    127.0.1.1:
      host.only:
        - hostnames:
          - foo.example.com
          - foo

Or delete all existing names for an address:

    203.0.113.25:
        host.only:
          - hostnames: []

You can also include comments:

    server1:
      host.present:
        - ip: 192.168.0.42
        - names:
          - server1
          - florida
        - comment: A very important comment

salt.states.host.absent(name, ip)

  Ensure that the named host is absent

  name

    The host to remove

  ip

    The ip addr(s) of the host to remove

salt.states.host.only(name, hostnames)

  Ensure that only the given hostnames are associated with the given IP address.

  New in version 2016.3.0.

  name

    The IP address to associate with the given hostnames.

  hostnames

    Either a single hostname or a list of hostnames to associate with the given IP address in the given order. Any other hostname associated with the IP address is removed. If no hostnames are specified, all hostnames associated with the given IP address are removed.

salt.states.host.present(name, ip, comment='', clean=False)

  Ensures that the named host is present with the given ip

  name

    The host to assign an ip to

  ip

    The ip addr(s) to apply to the host. Can be a single IP or a list of IP addresses.

  comment

    A comment to include for the host entry

    New in version 3001.

  clean

    Remove any entries which don't match those configured in the `ip` option. Default is `False`.

    New in version 2018.3.4.