ansible/ansible — pull request #1217 by dagwieers (2012-10-03 12:58:22+00:00)
Title: Module network_facts gathers facts based on network information for provisioning

This module enables you to map a system to a network, and import the facts related to this network. Anything can be a network-related fact just by adding it to the network-inventory yaml file. The module has its own name-space.

The syntax of the yaml file only requires that a cidr-attribute exists for each entry. Based on the gw-option, a gateway strategy will add a gateway attribute (first or last address) if one is missing. Everything else is optional depending on your use-case.

Here is an example *network-inventory.yml*:
```yaml
---
- vlan: 10
cidr: '10.1.10.0/25'
description: Production servers in DMZ2
nameservers: [ 10.1.2.10, 10.1.3.10 ]
domains: [ prod.corp.intra, hw.corp.intra, mgmt.corp.intra ]
environment: prod
datacenter: brussels
tier: dmz2
gateway: 10.1.10.232
- vlan: 15
cidr: '10.2.12.0/25'
description: QA servers in DMZ3
nameservers: [ 10.1.2.10, 10.1.3.10 ]
domains: [ nonprod.corp.intra, hw.corp.intra, mgmt.corp.intra ]
environment: nonprod
datacenter: antwerp
tier: dmz3
- vlan: 20
cidr: 10.3.148.0/22
description: Management servers in Trusted
environment: nonprod
datacenter: antwerp
tier: trusted
```

And here's how one would be using it for provisioning systems:
```yaml
- name: kickstart
hosts: all
gather_facts: False

vars:
rhel_version: 6.3
kickstart_file: /var/www/html/ks/${inventory_hostname_short}.cfg
iso_image: /iso/ks/${inventory_hostname_short}.iso
rhel_pxeboot: /var/www/mrepo/rhel${rhel_version}-x86_64/disc1/images/pxeboot/.
isolinux_bin: /usr/share/syslinux/isolinux.bin
syslinux_dir: /usr/share/syslinux/.

tasks:
### Get network facts
- local_action: network_facts host='${inventory_hostname_short}' inventory=../network-inventory.yml full=yes

### Get iLO facts
- local_action: ilo_facts host='${cmdb_ilo_address}.${hwdomain}' login='${ilologin}' password='${ilopassword}' match='${inventory_hostname_short}'
only_if: " '${cmdb_hwtype}'.startswith('HP ') "

### Get ESX facts
- local_action: esx_facts host='${cmdb_esx_server}.${hwdomain}' login='${esxlogin}' password='${esxpassword}' vmname='${cmdb_uuid}'
only_if: " '${cmdb_hwtype}'.startswith('VMWare ') "

### Create a custom boot ISO (use network_facts info for kickstart templating)
- local_action: command mktemp -d
register: tempdir
- local_action: command cp -av ${rhel_pxeboot} ${isolinux_bin} ${tempdir.stdout}
- local_action: template src=../templates/kickstart/isolinux.cfg dest=${tempdir.stdout}/isolinux.cfg
- local_action: template src=../templates/kickstart/ks.cfg dest=${tempdir.stdout}/ks.cfg
- local_action: command mkisofs -r -N -allow-leading-dots -d -J -T -b isolinux.bin -c boot.cat -no-emul-boot -V "Custom RHEL${rhel_version} for ${inventory_hostname_short}" -boot-load-size 4 -boot-info-table -o ${iso_image} ${tempdir.stdout}
- local_action: command rm -rf ${tempdir.stdout}

### Create a custome PXE configuration (based on iLO or ESX information), we do everything BECAUSE WE CAN !!
- local_action: template src=../templates/kickstart/ks.cfg dest=${kickstart_file}
- local_action: template src=../templates/kickstart/pxelinux.cfg dest=/var/lib/tftpboot/pxelinux.cfg/${inventory_hostname_short}
- local_action: template src=../templates/kickstart/pxelinux.cfg dest=/var/lib/tftpboot/pxelinux.cfg/${network_ipaddress_hex}
- local_action: template src=../templates/kickstart/pxelinux.cfg dest=/var/lib/tftpboot/pxelinux.cfg/${hw_eth0.macaddress_dash}
- local_action: template src=../templates/kickstart/pxelinux.cfg dest=/var/lib/tftpboot/pxelinux.cfg/${hw_product_uuid}

### Safeguard to protect production systems
- local_action: fail msg="System is not set to 'to-be-staged' in CMDB"
only_if: "'$cmdb_status' != 'to-be-staged'"

### Kick off iLO provisioning
- local_action: ilo_boot host='${cmdb_ilo_address}.${hwdomain}' login='${ilologin}' password='${ilopassword}' media='cdrom' image='http://${ansible_server}/iso/ks/${inventory_hostname_short}.iso' state='boot_once' match='${inventory_hostname_short}' force='yes'
only_if: " '${cmdb_hwtype}'.startswith('HP ') "

### Kick off ESX provisioning
- local_action: esx_boot host='${cmdb_esx_server}.${hwdomain}' login='${esxlogin}' password='${esxpassword}' vmname='${cmdb_uuid}' media='cdrom' image='[nfs-datastore] /iso/ks/${inventory_hostname_short}.iso' state='boot_once' match='${inventory_hostname_short}' force='yes'
only_if: " '${cmdb_hwtype}'.startswith('VMWare ') "

### Revoke any existing host keys (needs a separate ansible module)
- local_action: command ssh-keygen -R ${inventory_hostname_short}
ignore_errors: True
- local_action: command ssh-keygen -R ${inventory_hostname}
ignore_errors: True
- local_action: command ssh-keygen -R ${network_ipaddress}
ignore_errors: True

### Wait for the post-install SSH to become available
- local_action: wait_for host=$inventory_hostname port=22 state=started timeout=1800 delay=180

### Remove PXE boot configuration
- local_action: file dest=/var/lib/tftpboot/pxelinux.cfg/${network_ipaddress_hex} state=absent
- local_action: file dest=/var/lib/tftpboot/pxelinux.cfg/${hw_eth0.macaddress_dash} state=absent
- local_action: file dest=/var/lib/tftpboot/pxelinux.cfg/${hw_product_uuid} state=absent

### Disconnect images
- local_action: esx_boot host='${cmdb_esx_server}.${hwdomain}' login='${esxlogin}' password='${esxpassword}' vmname='${cmdb_uuid}' media='cdrom' state='disconnect' match='${inventory_hostname_short}'
only_if: " '${cmdb_hwtype}'.startswith('VMWare ')"

### Now continue with provisioning !
```

This module was already mentioned in #1085, #1125 and #1206.