{"id":16779646,"url":"https://github.com/ahuffman/ansible-hosts","last_synced_at":"2025-04-10T20:52:37.935Z","repository":{"id":69819573,"uuid":"62260475","full_name":"ahuffman/ansible-hosts","owner":"ahuffman","description":"An Ansible role to set a host's hostname and manage it's aliases along with additional host entries.","archived":false,"fork":false,"pushed_at":"2022-01-10T20:17:31.000Z","size":16,"stargazers_count":4,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T18:21:29.949Z","etag":null,"topics":["ansible","ansible-role","etc-hosts","hosts"],"latest_commit_sha":null,"homepage":null,"language":"Jinja","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ahuffman.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-06-29T21:55:26.000Z","updated_at":"2021-07-19T21:40:33.000Z","dependencies_parsed_at":"2023-02-24T14:00:43.162Z","dependency_job_id":null,"html_url":"https://github.com/ahuffman/ansible-hosts","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahuffman%2Fansible-hosts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahuffman%2Fansible-hosts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahuffman%2Fansible-hosts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahuffman%2Fansible-hosts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ahuffman","download_url":"https://codeload.github.com/ahuffman/ansible-hosts/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248297011,"owners_count":21080309,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ansible","ansible-role","etc-hosts","hosts"],"created_at":"2024-10-13T07:31:17.878Z","updated_at":"2025-04-10T20:52:37.924Z","avatar_url":"https://github.com/ahuffman.png","language":"Jinja","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Ansible Role](https://img.shields.io/ansible/role/d/10653)\n\n# ahuffman.hosts\n\nAn Ansible role to set a host's hostname and manage /etc/hosts entries.\n\n## Role Variables\n|Variable Name|Description|Required|Default Value|Type|\n|---|---|:---:|:---:|:---:|\n|hosts_hostname|Defines the system's hostname, and manages its /etc/hosts entries.|no|{}|dictionary|\n|hosts_add_hosts|Defines additional /etc/hosts entries.|no|[{}]|list of dictionaries|\n|hosts_backup|Backup /etc/hosts when changing the file.|no|True|boolean|\n|hosts_set_hostname|Set hostname on system. Set False to only manage /etc/hosts|no|True|boolean|\n\n***if you do not set hosts_hostname (not required) a default ipv4 loopback line will be created in /etc/hosts if hosts_add_hosts is defined, but ipv6 loopback will not get created***\n\n## `hosts_hostname` Dictionary Fields\n|Field Name|Description|Required|Default Value|Type|\n|---|---|:---:|:---:|:---:|\n|hostname|Short (non-fqdn) hostname of the server|yes|n/a|string|\n|domain|Domain name of the server|yes|n/a|string|\n|alias|Additional aliases the server is known by (cnames)|no|n/a|list|\n|alias_loopback|Whether or not to add the hostname and aliases to the /etc/hosts loopback line(s).|yes|n/a|boolean|\n|add_ips|Additional IP addresses for the server in /etc/hosts.|no|n/a|list|\n|ipv6|Whether IPv6 is used on the system (sets IPv6 loopback line)|yes|n/a|boolean|\n\n### `hosts_hostname` Usage Example\nHere is an example of using the hosts_hostname dictionary in a variable file:\n```yaml\n---\nhosts_hostname:\n  hostname: \"foo\"\n  domain: \"bar.com\"\n  alias:\n    - \"web1\"\n    - \"intranet\"\n  alias_loopback: False # Don't modify localhost loopback in /etc/hosts\n  add_ips:\n    - \"192.168.122.100\" # main NIC\n    - \"10.10.10.100\" # another NIC\n    # you could add ipv6 addresses to this list as well\n  ipv6: False # Don't create ipv6 loopback entries  \n```\nThis will produce lines in /etc/hosts as such:\n```\n127.0.0.1        localhost localhost.localdomain localhost4 localhost4.localdomain4\n192.168.122.100  foo.bar.com foo web1 intranet\n10.10.10.100     foo.bar.com foo web1 intranet\n```\n## `hosts_add_hosts` Dictionary Fields\n|Field Name|Description|Required|Default Value|Type|\n|---|---|:---:|:---:|:---:|\n|hostname|Short (non-fqdn) hostname of the server|yes|n/a|string|\n|domain|Domain name of the server|yes|n/a|string|\n|ip_addr|IP address of the server|yes|n/a|string|\n|comment|Comment line to put above entry in /etc/hosts|no|n/a|string|\n|alias|Additional aliases the server is known by (cnames)|no|n/a|list|\n\n### `hosts_add_hosts` Usage Example\nHere is an example of using the hosts_hostname dictionary in a variable file:\n```yaml\nhosts_add_hosts:\n  - hostname: \"otherserver1\"\n    domain: \"bar.net\"\n    ip_addr: \"10.100.5.66\"\n    comment: \"External database server\"\n    alias:\n      - \"db1\"\n      - \"database\"\n      - \"db\"\n  - hostname: \"anotherserver\"\n    domain: \"coolsite.com\"\n    ip_addr: \"1.2.3.4\"\n    comment: \"A cool website\"\n```\nThis will produce lines in /etc/hosts as such:\n```\n# External database server\n10.100.5.66      otherserver1.bar.net otherserver1 db1 database db\n# A cool website\n1.2.3.4          anotherserver.coolsite.com anotherserver\n```\n## Example Playbook\nhost_vars/myserver1.yml contents:\n```yaml\n---\n# Set host specific hostname and /etc/hosts entries\nhosts_hostname:\n  hostname: \"myserver1\"\n  domain: \"foo.bar.com\"\n  alias:\n    - \"webserver1.someotherdomain.com\"\n    - \"www\"\n    - \"db1\"\n    - \"dubdubdub.dubs.com\"\n  alias_loopback: False\n  add_ips:\n    - \"192.168.122.50\"\n    - \"192.168.122.51\"\n    - \"192.168.122.52\"\n  ipv6: True\n```\n```yaml\n---\n- name: \"Set hostname and /etc/hosts entries\"\n  hosts: \"all\"\n  tasks:\n    - name: \"Ensure hostname and /etc/hosts are in desired state\"\n      include_role:\n        name: \"ahuffman.hosts\"\n      vars:\n        hosts_backup: False\n        hosts_set_hostname: True\n        hosts_add_hosts:\n          - hostname: \"myserver2\"\n            domain: \"foo.bar.com\"\n            ip_addr: \"192.168.122.60\"\n            comment: 'The other webserver that does stuff'\n            alias:\n              - \"intranet.externaldomain.com\"\n              - \"intranet\"\n              - \"helpdesk.helpme.com\"\n              - \"helpdesk\"\n          - name: \"Another App server\"\n            hostname: \"appserver2\"\n            domain: \"bar.com\"\n            ip_addr: \"192.168.122.70\"\n            comment: \"App server 1\"\n```\nThe /etc/hosts file produced by the above example would look like:\n```\n# Ansible managed\n127.0.0.1        localhost localhost.localdomain localhost4 localhost4.localdomain4\n::1              localhost localhost.localdomain localhost6 localhost6.localdomain6\n192.168.122.50   myserver1.foo.bar.com myserver1 webserver1.someotherdomain.com www db1 dubdubdub.dubs.com\n192.168.122.51   myserver1.foo.bar.com myserver1 webserver1.someotherdomain.com www db1 dubdubdub.dubs.com\n192.168.122.52   myserver1.foo.bar.com myserver1 webserver1.someotherdomain.com www db1 dubdubdub.dubs.com\n\n# The other webserver that does stuff\n192.168.122.60   myserver2.foo.bar.com myserver2 intranet.externaldomain.com intranet helpdesk.helpme.com helpdesk\n# App server 1\n192.168.122.70   appserver2.bar.com appserver2\n```\n## License\n[MIT](LICENSE)\n\n## Author\n[Andrew J. Huffman](https://github.com/ahuffman)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahuffman%2Fansible-hosts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fahuffman%2Fansible-hosts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahuffman%2Fansible-hosts/lists"}