├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── Vagrantfile ├── defaults └── main.yml ├── meta └── main.yml ├── tasks └── main.yml ├── test.yml ├── vagrant-inventory └── vars └── main.yml /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .AppleDouble 3 | .LSOverride 4 | Icon 5 | ._* 6 | .Spotlight-V100 7 | .Trashes 8 | .vagrant 9 | test 10 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | language: python 3 | python: "2.7" 4 | env: 5 | matrix: 6 | - ANSIBLE_VERSION="2.4.2.0" 7 | - ANSIBLE_VERSION="2.7" 8 | before_install: 9 | - sudo apt-get update -qq 10 | - sudo apt-get install -qq python-apt python-pycurl 11 | install: 12 | - pip install ansible=="$ANSIBLE_VERSION" 13 | script: 14 | - echo localhost > inventory 15 | - ansible-playbook -i inventory test.yml --syntax-check 16 | - ansible-playbook -i inventory test.yml --connection=local --sudo 17 | - > 18 | ansible-playbook -i inventory test.yml --connection=local --sudo 19 | | grep -q 'changed=0.*failed=0' 20 | && (echo 'Idempotence test: pass' && exit 0) 21 | || (echo 'Idempotence test: fail' && exit 1) 22 | notifications: 23 | webhooks: https://galaxy.ansible.com/api/v1/notifications/ 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2014 Pieterjan Vandaele 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## ANXS - hostname [![Build Status](https://travis-ci.com/ANXS/hostname.png)](https://travis-ci.com/ANXS/hostname) 2 | 3 | Ansible role that sets the hostname and FQDN of the node. 4 | 5 | 6 | #### Variables 7 | 8 | This depends on your ansible hosts inventory. 9 | 10 | Add the hosts to your inventory with their FQDN (e.g. foo.bar.com), and the role will take care of setting your hostname accordingly (hostname: foo, FQDN: foo.bar.com). 11 | 12 | If you just name it with the hostname in the inventory, it will similarly work (hostname set, but no FQDN attached to it). 13 | 14 | ```yaml 15 | hostname_avahi: no # You may optionall install avahi-mdns and libnss-mdns. This is useful in vagrant. 16 | ``` 17 | 18 | #### Example 19 | 20 | Your inventory file should look like this: 21 | 22 | ```yaml 23 | foo.bar.com ansible_ssh_host=xxx.xxx.xxx.xxx ansible_ssh_port=22 24 | baz.bar.com ansible_ssh_host=xxx.xxx.xxx.xxx ansible_ssh_port=22 25 | ``` 26 | 27 | And the structure of the files in your host_vars folders should match accordingly: 28 | 29 | ``` 30 | - host_vars 31 | |- foo.bar.com 32 | |- baz.bar.com 33 | ``` 34 | 35 | 36 | #### Testing 37 | This project comes with a VagrantFile, this is a fast and easy way to test changes to the role, fire it up with `vagrant up` 38 | 39 | See [vagrant docs](https://docs.vagrantup.com/v2/) for getting setup with vagrant 40 | 41 | 42 | #### License 43 | 44 | Licensed under the MIT License. See the LICENSE file for details. 45 | 46 | 47 | #### Feedback, bug-reports, requests, ... 48 | 49 | Are [welcome](https://github.com/ANXS/hostname/issues)! 50 | -------------------------------------------------------------------------------- /Vagrantfile: -------------------------------------------------------------------------------- 1 | # -*- mode: ruby -*- 2 | # vi: set ft=ruby : 3 | 4 | hostname_avahi = ENV.key?('HOSTNAME_AVAHI') ? ENV['HOSTNAME_AVAHI'] : 'false' 5 | Vagrant.configure('2') do |config| 6 | config.vm.define 'anxs' do |c| 7 | c.vm.box = 'ubuntu/trusty64' 8 | c.vm.network :private_network, ip: '192.168.88.12' 9 | c.vm.hostname = 'anxs.local' 10 | c.vm.provision 'ansible' do |ansible| 11 | ansible.playbook = 'test.yml' 12 | ansible.sudo = true 13 | ansible.inventory_path = 'vagrant-inventory' 14 | ansible.host_key_checking = false 15 | ansible.extra_vars = { 16 | hostname_avahi: hostname_avahi 17 | } 18 | end 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | hostname_avahi: no 4 | -------------------------------------------------------------------------------- /meta/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | galaxy_info: 3 | author: pjan vandaele 4 | company: ANXS 5 | description: sets the hostname and FQDN 6 | min_ansible_version: 2.4.2.0 7 | license: MIT 8 | platforms: 9 | - name: Ubuntu 10 | versions: 11 | - all 12 | categories: 13 | - system 14 | 15 | dependencies: 16 | - role: ANXS.apt 17 | -------------------------------------------------------------------------------- /tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Hostname | Ensure dbus installed 3 | apt: 4 | name: "{{anxs_hostname_packages}}" 5 | state: present 6 | tags: hostname 7 | when: ansible_os_family == "Debian" 8 | 9 | - name: Hostname | Update the hostname (pt. 1) - hostname cmd 10 | hostname: 11 | name: "{{inventory_hostname_short}}" 12 | 13 | - name: Hostname | Update the hostname (pt. 2) - (/etc/hostname) 14 | copy: 15 | content: "{{inventory_hostname_short}}{{'\n'}}" 16 | dest: /etc/hostname 17 | owner: root 18 | group: root 19 | mode: 0644 20 | 21 | - name: Hostname | Update the IPv4 hostname (pt. 3) - (/etc/hosts) 22 | lineinfile: 23 | dest: /etc/hosts 24 | regexp: "^127.0.0.1" 25 | line: "127.0.0.1{{'\t'}}{{inventory_hostname}}{% if inventory_hostname != inventory_hostname_short %}{{'\t'}}{{inventory_hostname_short}}{% endif %}{{'\t'}}localhost" 26 | state: present 27 | 28 | - name: Hostname | Update the IPv6 hostname (pt. 3) - (/etc/hosts) 29 | lineinfile: 30 | dest: /etc/hosts 31 | regexp: "^::1" 32 | line: "::1{{'\t\t'}}{{inventory_hostname}}{% if inventory_hostname != inventory_hostname_short %}{{'\t'}}{{inventory_hostname_short}}{% endif %}{{'\t'}}localhost ip6-localhost ip6-loopback" 33 | state: present 34 | 35 | - name: Hostname | Install MDNS Components 36 | apt: 37 | name: "{{anxs_hostname_mdns_packages}}" 38 | state: present 39 | when: 'ansible_os_family == "Debian" and hostname_avahi' 40 | -------------------------------------------------------------------------------- /test.yml: -------------------------------------------------------------------------------- 1 | - hosts: all 2 | vars_files: 3 | - 'defaults/main.yml' 4 | - 'vars/main.yml' 5 | tasks: 6 | - include_tasks: 'tasks/main.yml' 7 | -------------------------------------------------------------------------------- /vagrant-inventory: -------------------------------------------------------------------------------- 1 | [anxs] 2 | anxs.local ansible_ssh_host=192.168.88.12 ansible_ssh_port=22 3 | -------------------------------------------------------------------------------- /vars/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | anxs_hostname_packages: 3 | - dbus 4 | - policykit-1 5 | anxs_hostname_mdns_packages: 6 | - avahi-daemon 7 | - libnss-mdns 8 | --------------------------------------------------------------------------------