From 088d941d224e896b4515fa086f6ec65d0b355898 Mon Sep 17 00:00:00 2001 From: Rick Box Date: Fri, 3 Feb 2017 04:15:09 +0000 Subject: [PATCH] Adds in DHCPD role - Ubuntu 16.04 Building on from the previous commit https://review.openstack.org/427869 this adds in DHCPD role to provide DHCP to the virtual machines which are booting up and point them towards our TFTP server. Change-Id: I73649562096a659dee7227b0cfb84f2c4a4f5ec4 --- .../roles/dhcpd_install/README.md | 23 ++++++++ .../roles/dhcpd_install/defaults/main.yml | 45 ++++++++++++++++ .../roles/dhcpd_install/handlers/main.yml | 21 ++++++++ .../roles/dhcpd_install/meta/main.yml | 22 ++++++++ .../roles/dhcpd_install/tasks/configure.yml | 24 +++++++++ .../roles/dhcpd_install/tasks/install.yml | 26 +++++++++ .../roles/dhcpd_install/tasks/main.yml | 18 +++++++ .../dhcpd_install/templates/dhcpd.conf.j2 | 53 +++++++++++++++++++ 8 files changed, 232 insertions(+) create mode 100644 multi-node-aio-xenial-ansible/roles/dhcpd_install/README.md create mode 100644 multi-node-aio-xenial-ansible/roles/dhcpd_install/defaults/main.yml create mode 100644 multi-node-aio-xenial-ansible/roles/dhcpd_install/handlers/main.yml create mode 100644 multi-node-aio-xenial-ansible/roles/dhcpd_install/meta/main.yml create mode 100644 multi-node-aio-xenial-ansible/roles/dhcpd_install/tasks/configure.yml create mode 100644 multi-node-aio-xenial-ansible/roles/dhcpd_install/tasks/install.yml create mode 100644 multi-node-aio-xenial-ansible/roles/dhcpd_install/tasks/main.yml create mode 100644 multi-node-aio-xenial-ansible/roles/dhcpd_install/templates/dhcpd.conf.j2 diff --git a/multi-node-aio-xenial-ansible/roles/dhcpd_install/README.md b/multi-node-aio-xenial-ansible/roles/dhcpd_install/README.md new file mode 100644 index 00000000..fed557ee --- /dev/null +++ b/multi-node-aio-xenial-ansible/roles/dhcpd_install/README.md @@ -0,0 +1,23 @@ +dhcpd_install +========= + +This module installs dhcpd + +Requirements +------------ + +This module requires Ansible 2.x + +Role Variables +-------------- + +See defaults for variables and descriptions + +Example Playbook +---------------- + +Example to call: + + - hosts: all + roles: + - { role: dhcpd_install } diff --git a/multi-node-aio-xenial-ansible/roles/dhcpd_install/defaults/main.yml b/multi-node-aio-xenial-ansible/roles/dhcpd_install/defaults/main.yml new file mode 100644 index 00000000..15deba58 --- /dev/null +++ b/multi-node-aio-xenial-ansible/roles/dhcpd_install/defaults/main.yml @@ -0,0 +1,45 @@ +--- +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# name: dhcpd_install/defaults +# description: ALL our default variables for dhcpd_install go in here +#------------------------------------------------------------------------------ +# Packages - All our required packages we need installing +#------------------------------------------------------------------------------ + +# - pre-requisites - +packages: # Packages required for dhcpd + - isc-dhcp-server + +# - general variables - +dhcp_default_lease_time: 21600 # Default lease time +dhcp_max_lease_time: 43200 # Max lease time +tftp_server: 10.0.2.100 # The server hosting the TFTP server +tftp_boot_path: /pxelinux.0 # Path of where to boot from first + +# - List of DHCP Subnets - These are iterated though and each will be created +dhcp_list: + - netmask: 255.255.255.0 # Netmask + gateway: 10.0.2.1 # Gateway + dns: 8.8.8.8 # DNS + subnet: 10.0.2.0 # Subnet mask + default_lease_time: 21600 # Subnet Default lease time - The default is used if this is not defined + max_lease_time: 43200 # Subnet Max lease time - The default is used if this is not defined + tftp_boot_path: /pxelinux.0 # Path for tftp of where to boot from first - The default is used if this is not defined + tftp_server: 10.0.2.100 # The server hosting the TFTP server - The default is used if this is not defined + +# - List of static DHCP hosts - These are iterated though and each will be created +server_list: + - name: host # Server hostname + hwaddr: "aa:aa:aa:aa:aa:aa" # Server MAC + ip: "10.0.2.2" # Server IP diff --git a/multi-node-aio-xenial-ansible/roles/dhcpd_install/handlers/main.yml b/multi-node-aio-xenial-ansible/roles/dhcpd_install/handlers/main.yml new file mode 100644 index 00000000..12c0b2a5 --- /dev/null +++ b/multi-node-aio-xenial-ansible/roles/dhcpd_install/handlers/main.yml @@ -0,0 +1,21 @@ +--- +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# name: dhcpd_install/handlers +# description: All our handlers for dhcpd_install go in here + +- name: restart dhcpd + service: + name: "{{ item }}" + state: restarted + with_items: "{{ packages }}" diff --git a/multi-node-aio-xenial-ansible/roles/dhcpd_install/meta/main.yml b/multi-node-aio-xenial-ansible/roles/dhcpd_install/meta/main.yml new file mode 100644 index 00000000..fe3e481e --- /dev/null +++ b/multi-node-aio-xenial-ansible/roles/dhcpd_install/meta/main.yml @@ -0,0 +1,22 @@ +--- +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +galaxy_info: + author: "Rick Box - BBC R&D" + license: Apache2 + min_ansible_version: 2.0 + platforms: + - name: Ubuntu + versions: + - xenial +dependencies: [] diff --git a/multi-node-aio-xenial-ansible/roles/dhcpd_install/tasks/configure.yml b/multi-node-aio-xenial-ansible/roles/dhcpd_install/tasks/configure.yml new file mode 100644 index 00000000..6ce8873e --- /dev/null +++ b/multi-node-aio-xenial-ansible/roles/dhcpd_install/tasks/configure.yml @@ -0,0 +1,24 @@ +--- +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# module: dhcpd_install/tasks/configure +# description: Configure dhcpd_install + +- name: Create a template in /etc/dhcp/dhcpd.conf + template: + src: dhcpd.conf.j2 + dest: /etc/dhcp/dhcpd.conf + mode: 0644 + owner: root + group: root + notify: restart dhcpd diff --git a/multi-node-aio-xenial-ansible/roles/dhcpd_install/tasks/install.yml b/multi-node-aio-xenial-ansible/roles/dhcpd_install/tasks/install.yml new file mode 100644 index 00000000..463ef474 --- /dev/null +++ b/multi-node-aio-xenial-ansible/roles/dhcpd_install/tasks/install.yml @@ -0,0 +1,26 @@ +--- +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# module: dhcpd_install/tasks/install +# description: Install our required packages for dhcpd_install + +- name: Install all required packages for dhcpd_install + apt: + pkg: "{{ packages }}" + state: latest + +- name: Make sure -{{ packages }}- are enabled + service: + name: "{{ item }}" + enabled: yes + with_items: "{{ packages }}" diff --git a/multi-node-aio-xenial-ansible/roles/dhcpd_install/tasks/main.yml b/multi-node-aio-xenial-ansible/roles/dhcpd_install/tasks/main.yml new file mode 100644 index 00000000..0bd2f9c7 --- /dev/null +++ b/multi-node-aio-xenial-ansible/roles/dhcpd_install/tasks/main.yml @@ -0,0 +1,18 @@ +--- +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# module: dhcpd_install/tasks +# description: Install dhcpd_install onto an Ubuntu 16.xx server + +- include: install.yml +- include: configure.yml diff --git a/multi-node-aio-xenial-ansible/roles/dhcpd_install/templates/dhcpd.conf.j2 b/multi-node-aio-xenial-ansible/roles/dhcpd_install/templates/dhcpd.conf.j2 new file mode 100644 index 00000000..a8fccd42 --- /dev/null +++ b/multi-node-aio-xenial-ansible/roles/dhcpd_install/templates/dhcpd.conf.j2 @@ -0,0 +1,53 @@ +#jinja2: lstrip_blocks: True +### MANAGED BY {{ role_path|basename }} ANSIBLE ROLE ### + +ddns-update-style none; + +allow booting; +allow bootp; + +log-facility local7; +authoritative; + +shared-network all-networks { + {% for dhcp in dhcp_list %} + subnet {{ dhcp.subnet }} netmask {{ dhcp.netmask }} { + option routers {{ dhcp.gateway }}; + option domain-name-servers {{ dhcp.dns }}; + option subnet-mask {{ dhcp.netmask }}; + {% if dhcp.default_lease_time is defined and dhcp.default_lease_time > 0 %} + default-lease-time {{ dhcp.default_lease_time }}; + {% else %} + default-lease-time {{ dhcp_default_lease_time }}; + {% endif %} + {% if dhcp.max_lease_time is defined and dhcp.max_lease_time > 0 %} + max-lease-time {{ dhcp.max_lease_time }}; + {% else %} + max-lease-time {{ dhcp_max_lease_time }}; + {% endif %} + {% if dhcp.tftp_server is defined and dhcp.tftp_server|ipaddr %} + next-server {{ dhcp.tftp_server }}; + {% elif tftp_server is defined and tftp_server|length > 0 %} + next-server {{ tftp_server }}; + {% endif %} + {% if dhcp.tftp_boot_path is defined and dhcp.tftp_boot_path|ipaddr %} + filename "{{ dhcp.tftp_boot_path }}"; + {% elif tftp_boot_path is defined and tftp_boot_path|length > 0 %} + filename "{{ tftp_boot_path }}"; + {% endif %} + } + + {% endfor %} + + {% if server_list.0.name != "blank" %} + group { + {% for host in server_list %} + host {{ host.name }} { + hardware ethernet {{ host.hwaddr }}; + fixed-address {{ host.ip }}; + option host-name "{{ host.name }}"; + } + {% endfor %} + } + {% endif %} +}