├── .gitignore ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── defaults └── main.yml ├── handlers └── main.yml ├── meta └── main.yml ├── tasks └── main.yml ├── templates └── etc_hosts.j2 └── vars └── main.yml /.gitignore: -------------------------------------------------------------------------------- 1 | # .gitignore 2 | 3 | # Hidden Vagrant-directory 4 | .vagrant 5 | 6 | # Backup files (e.g. Vim, Gedit, etc.) 7 | *~ 8 | 9 | # Vagrant base boxes (you never know when someone puts one in the repository) 10 | *.box 11 | 12 | # Compiled Python 13 | *.pyc 14 | 15 | # Test directory (sits in a separate branch) 16 | tests/ 17 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change log 2 | 3 | This file contains al notable changes to the hosts Ansible role. 4 | 5 | This file adheres to the guidelines of [http://keepachangelog.com/](http://keepachangelog.com/). Versioning follows [Semantic Versioning](http://semver.org/). 6 | 7 | ## 1.2.0 - 2017-07-03 8 | 9 | ### Added 10 | 11 | - (GH-3) Added host file backup flag (Credit: [Dheeraj Dwivedi](https://github.com/dheerajdwivedi)) 12 | 13 | ## 1.1.0 - 2016-05-31 14 | 15 | ### Added 16 | 17 | - (GH-2) Allow setting the network interface and IP protocol when adding entries for Ansible managed hosts. (credit: [Ernestas Poskus](https://github.com/ernestas-poskus)) 18 | 19 | 20 | ## 1.0.2 - 2016-05-21 21 | 22 | ### Changed 23 | 24 | - Fixed Ansible 2.0 deprecation warnings in role (GH-1) and test code. (partial credits to [Ernestas Poskus](https://github.com/ernestas-poskus)) 25 | 26 | ## 1.0.1 - 2015-08-18 27 | 28 | ### Changed 29 | 30 | - When adding entries for Ansible managed hosts, skip hosts that do not have `ansible_default_ipv4` set. 31 | 32 | ## 1.0.0 - 2015-07-30 33 | 34 | First release! 35 | 36 | ### Added 37 | 38 | - Host file template 39 | - Add the default localhost entry; 40 | - Add an entry for the host name bound to the host's default external IPv4 address (optional); 41 | - Add entries for basic IPv6 addresses, e.g. ip6-localnet (optional); 42 | - Add entries for Ansible managed hosts (optional); 43 | - Add entries specified in Yaml (optional); 44 | - Add entries specified in text files (optional). 45 | 46 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # BSD License 2 | 3 | Copyright (c) 2014, Bert Van Vreckem, (bert.vanvreckem@gmail.com) 4 | 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 10 | 11 | 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 12 | 13 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ansible role `hosts` 2 | 3 | An Ansible role for managing the hosts file (`/etc/hosts`). Specifically, the responsibilities of this role are to: 4 | 5 | - Add the default localhost entry; 6 | - Add an entry for the host name bound to the host's default external IPv4 address (optional); 7 | - Add entries for basic IPv6 addresses, e.g. ip6-localnet (optional); 8 | - Add entries for Ansible managed hosts (optional); 9 | - Add entries specified in Yaml (optional, see below); 10 | - Add entries specified in text files (optional). 11 | 12 | ## Requirements 13 | 14 | No specific requirements 15 | 16 | ## Role Variables 17 | 18 | None of the variables below are required. When not set, the default setting is applied. 19 | 20 | | Variable | Default | Comments | 21 | | :--- | :--- | :--- | 22 | | `hosts_add_default_ipv4` | true | If true, an entry for the host name is added, bound to the host's default IPv4 address. | 23 | | `hosts_add_basic_ipv6` | false | If true, basic IPv6 entries are added (e.g. localhost6, ip6-localnet, etc.) | 24 | | `hosts_add_ansible_managed_hosts` | false | If true, an entry for hosts managed by Ansible is added. (†) | 25 | | `hosts_add_ansible_managed_hosts_groups` | ['all'] | Control which host entries are created when using `hosts_add_ansible_managed_hosts` | 26 | | `hosts_entries` | [] | A list of dicts with custom entries to be added to the hosts file. See below for an example. | 27 | | `hosts_file_snippets` | [] | A list of files containing host file snippets to be added to the hosts file verbatim. | 28 | | `hosts_ip_protocol` | `ipv4` | When adding Ansible managed hosts, this specifies the IP protocol (`ipv4` or `ipv6`) | 29 | | `hosts_network_interface` | `{{ansible_default_ipv4.interface}}` | When adding Ansible managed hosts, this specifies the network interface for which the IP address should be added. | 30 | | `hosts_file_backup` | no | If yes, backup of host file is created with timestamp | 31 | | | | | 32 | 33 | (†) When setting `hosts_add_ansible_managed_hosts`, an entry for the current host will also be added. Consequently, `hosts_add_default_ipv4` doesn't need to be set. 34 | 35 | Individual hosts file entries can be added with `hosts_entries`, a list of dicts with keys `name`, `ip` and (optional) `aliases`. Example: 36 | 37 | ```Yaml 38 | hosts_entries: 39 | - name: slashdot 40 | ip: 216.34.181.45 41 | - name: gns1 42 | ip: 8.8.8.8 43 | aliases: 44 | - googledns1 45 | - googlens1 46 | - name: gns2 47 | ip: 8.8.4.4 48 | aliases: 49 | - googledns2 50 | - googlens2 51 | ``` 52 | 53 | ## Dependencies 54 | 55 | No dependencies. 56 | 57 | ## Example Playbook 58 | 59 | See the [test playbook](https://github.com/bertvv/ansible-role-hosts/blob/tests/test.yml) 60 | 61 | Tests for this role are provided in the form of a Vagrant environment that is kept in a separate branch, `tests`. I use [git-worktree(1)](https://git-scm.com/docs/git-worktree) to include the test code into the working directory. Instructions for running the tests: 62 | 63 | 1. Fetch the tests branch: `git fetch origin tests` 64 | 2. Create a Git worktree for the test code: `git worktree add tests tests` (remark: this requires at least Git v2.5.0). This will create a directory `tests/`. 65 | 3. `cd tests/` 66 | 4. `vagrant up` will then create a VM and apply a test playbook (`test.yml`). 67 | 68 | You may want to change the base box into one that you like. The current one, [bertvv/centos72](https://atlas.hashicorp.com/bertvv/boxes/centos72) was generated using a Packer template from the [Boxcutter project](https://github.com/boxcutter/centos) with a few modifications. 69 | 70 | ## Contributing 71 | 72 | Issues, feature requests, ideas are appreciated and can be posted in the Issues section. Pull requests are also very welcome. Preferably, create a topic branch and when submitting, squash your commits into one (with a descriptive message). 73 | 74 | ## License 75 | 76 | BSD, see 77 | 78 | ## Contributors 79 | 80 | This role was inspired by the work of [soplakanets](https://github.com/soplakanets/ansible-role-hosts/) (including the [contribution by astrorafael](https://github.com/soplakanets/ansible-role-hosts/pull/1/files)) and [mivok](https://github.com/mivok/ansible-hosts/). 81 | 82 | - [Bert Van Vreckem](https://github.com/bertvv/) (maintainer) 83 | - [Dheeraj Dwivedi](https://github.com/dheerajdwivedi) 84 | - [Ernestas Poskus](https://github.com/ernestas-poskus) 85 | - [Mohammed Naser](https://github.com/mnaser) 86 | -------------------------------------------------------------------------------- /defaults/main.yml: -------------------------------------------------------------------------------- 1 | # roles/hosts/defaults/main.yml 2 | --- 3 | 4 | hosts_playbook_version: "1.0.1" 5 | 6 | # If set to true, an entry for `ansible_hostname`, bound to the host's default IPv4 address is added added. 7 | hosts_add_default_ipv4: true 8 | 9 | # If set to true, basic IPv6 entries (localhost6, ip6-localnet, etc) are added. 10 | hosts_add_basic_ipv6: false 11 | 12 | # If set to true, an entry for every host managed by Ansible is added. Remark that this makes `hosts_add_default_ipv4` unnecessary, as it will be added as wel by this setting. 13 | hosts_add_ansible_managed_hosts: false 14 | 15 | # Select specific groups of Ansible managed hosts to be added in the hosts file. 16 | hosts_add_ansible_managed_hosts_groups: ['all'] 17 | 18 | # Custom hosts entries to be added 19 | hosts_entries: [] 20 | 21 | # Custom host file snippets to be added 22 | hosts_file_snippets: [] 23 | 24 | # IP protocol to use 25 | hosts_ip_protocol: 'ipv4' 26 | 27 | # Network interface to use 28 | hosts_network_interface: "{{ ansible_default_ipv4.interface }}" 29 | 30 | # Backup of previous host 31 | host_file_backup: no 32 | -------------------------------------------------------------------------------- /handlers/main.yml: -------------------------------------------------------------------------------- 1 | # roles/hosts/handlers/main.yml 2 | --- 3 | 4 | - name: restart hosts 5 | service: 6 | name: hostsd 7 | state: restarted 8 | -------------------------------------------------------------------------------- /meta/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | galaxy_info: 3 | author: Bert Van Vreckem 4 | description: An Ansible role for managing the hosts file (`/etc/hosts`). 5 | license: BSD 6 | min_ansible_version: 1.7 7 | platforms: 8 | - name: EL 9 | versions: 10 | - all 11 | - name: GenericUNIX 12 | versions: 13 | - all 14 | - any 15 | - name: Fedora 16 | versions: 17 | - all 18 | - name: opensuse 19 | versions: 20 | - all 21 | - name: Ubuntu 22 | versions: 23 | - all 24 | - name: SLES 25 | versions: 26 | - all 27 | - name: GenericLinux 28 | versions: 29 | - all 30 | - any 31 | - name: Debian 32 | versions: 33 | - all 34 | galaxy_tags: 35 | - system 36 | - networking 37 | dependencies: [] 38 | -------------------------------------------------------------------------------- /tasks/main.yml: -------------------------------------------------------------------------------- 1 | # roles/hosts/tasks/main.yml 2 | --- 3 | 4 | - name: Install custom hosts file 5 | template: 6 | src: etc_hosts.j2 7 | dest: "{{ hosts_file }}" 8 | owner: root 9 | group: root 10 | mode: 0644 11 | backup: "{{ host_file_backup }}" 12 | tags: hosts 13 | -------------------------------------------------------------------------------- /templates/etc_hosts.j2: -------------------------------------------------------------------------------- 1 | # Hosts file -- Don't edit manually! 2 | # 3 | # {{ ansible_managed }} 4 | 5 | 127.0.0.1 localhost.localdomain localhost 6 | {% if hosts_add_default_ipv4 %} 7 | {{ ansible_default_ipv4.address }} {{ ansible_fqdn }} {{ ansible_hostname }} 8 | {% endif %} 9 | {% if hosts_add_basic_ipv6 %} 10 | 11 | # Basic IPv6 entries 12 | ::1 localhost6.localdomain6 localhost6 ip6-localhost ip6-loopback 13 | fe00::0 ip6-localnet 14 | ff00::0 ip6-mcastprefix 15 | ff02::1 ip6-allnodes 16 | ff02::2 ip6-allrouters 17 | {% endif %} 18 | {% if hosts_add_ansible_managed_hosts %} 19 | 20 | # Entries for Ansible managed hosts 21 | {% for group in hosts_add_ansible_managed_hosts_groups %} 22 | {% for host in groups[group] if hostvars[host][hosts_ansible_network_interface] is defined %} 23 | {{ hostvars[host][hosts_ansible_network_interface][hosts_ip_protocol]['address'] }} {{ hostvars[host]['ansible_fqdn'] }} {{ hostvars[host]['ansible_hostname'] }} 24 | {% endfor %} 25 | {% endfor %} 26 | {% endif %} 27 | {% if hosts_entries|length != 0 %} 28 | 29 | # Custom entries 30 | {% for host in hosts_entries %} 31 | {{ host.ip }} {{ host.name }} {{ host.aliases | default('') | join(' ') }} 32 | {% endfor %} 33 | {% endif %} 34 | {% if hosts_file_snippets|length != 0 %} 35 | 36 | # 37 | # Hosts file snippets 38 | # 39 | {% for snippet in hosts_file_snippets %} 40 | {% include snippet %} 41 | 42 | {% endfor %} 43 | {% endif %} 44 | -------------------------------------------------------------------------------- /vars/main.yml: -------------------------------------------------------------------------------- 1 | # roles/hosts/vars/main.yml 2 | --- 3 | 4 | hosts_file: /etc/hosts 5 | 6 | hosts_ansible_network_interface: "ansible_{{ hosts_network_interface }}" 7 | --------------------------------------------------------------------------------