Change and Manage IP from cloned VM
There is a challenge to manage hosts of cloned copies because template can't assign or change any information.
For this, we are going to do this
- Running DHCP from ansible host
- Add ssh key to CentOS template to change configuration
- Preparing YAML
- Clone from template
- Read cloned mac address
- Add mac address to ansible dhcpconfig and assign ip address with static lease
- Start cloned copy
- Update hostname on the cloned copy
1) setup DHCP on ansible host
- Installing DHCP, sudo yum install dhcp
[ansible@ansible01 test]$ sudo yum install dhcp
Loaded plugins: fastestmirror, langpacks
base | 3.6 kB 00:00:00
code | 2.9 kB 00:00:00
epel/x86_64/metalink | 12 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/3): epel/x86_64/updateinfo | 881 kB 00:00:00
(2/3): code/primary_db | 44 kB 00:00:00
(3/3): epel/x86_64/primary_db | 6.2 MB 00:00:02
Loading mirror speeds from cached hostfile
* base: centos.les.net
* epel: muug.ca
* extras: centos.les.net
* updates: mirror.esecuredata.com
Resolving Dependencies
--> Running transaction check
---> Package dhcp.x86_64 12:4.2.5-58.el7.centos.1 will be installed
--> Processing Dependency: dhcp-libs(x86-64) = 12:4.2.5-58.el7.centos.1 for package: 12:dhcp-4.2.5-58.el7.centos.1.x86_64
--> Processing Dependency: dhcp-common = 12:4.2.5-58.el7.centos.1 for package: 12:dhcp-4.2.5-58.el7.centos.1.x86_64
--> Running transaction check
---> Package dhcp-common.x86_64 12:4.2.5-58.el7.centos will be updated
--> Processing Dependency: dhcp-common = 12:4.2.5-58.el7.centos for package: 12:dhclient-4.2.5-58.el7.centos.x86_64
---> Package dhcp-common.x86_64 12:4.2.5-58.el7.centos.1 will be an update
---> Package dhcp-libs.x86_64 12:4.2.5-58.el7.centos will be updated
---> Package dhcp-libs.x86_64 12:4.2.5-58.el7.centos.1 will be an update
--> Running transaction check
---> Package dhclient.x86_64 12:4.2.5-58.el7.centos will be updated
---> Package dhclient.x86_64 12:4.2.5-58.el7.centos.1 will be an update
--> Finished Dependency Resolution
Dependencies Resolved
===========================================================================================
Package Arch Version Repository Size
===========================================================================================
Installing:
dhcp x86_64 12:4.2.5-58.el7.centos.1 updates 513 k
Updating for dependencies:
dhclient x86_64 12:4.2.5-58.el7.centos.1 updates 282 k
dhcp-common x86_64 12:4.2.5-58.el7.centos.1 updates 174 k
dhcp-libs x86_64 12:4.2.5-58.el7.centos.1 updates 130 k
Transaction Summary
===========================================================================================
Install 1 Package
Upgrade ( 3 Dependent packages)
Total size: 1.1 M
Total download size: 513 k
Is this ok [y/d/N]: y
Downloading packages:
dhcp-4.2.5-58.el7.centos.1.x86_64.rpm | 513 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Updating : 12:dhcp-libs-4.2.5-58.el7.centos.1.x86_64 1/7
Updating : 12:dhcp-common-4.2.5-58.el7.centos.1.x86_64 2/7
Updating : 12:dhclient-4.2.5-58.el7.centos.1.x86_64 3/7
Installing : 12:dhcp-4.2.5-58.el7.centos.1.x86_64 4/7
Cleanup : 12:dhclient-4.2.5-58.el7.centos.x86_64 5/7
Cleanup : 12:dhcp-common-4.2.5-58.el7.centos.x86_64 6/7
Cleanup : 12:dhcp-libs-4.2.5-58.el7.centos.x86_64 7/7
Verifying : 12:dhcp-common-4.2.5-58.el7.centos.1.x86_64 1/7
Verifying : 12:dhcp-libs-4.2.5-58.el7.centos.1.x86_64 2/7
Verifying : 12:dhclient-4.2.5-58.el7.centos.1.x86_64 3/7
Verifying : 12:dhcp-4.2.5-58.el7.centos.1.x86_64 4/7
Verifying : 12:dhcp-common-4.2.5-58.el7.centos.x86_64 5/7
Verifying : 12:dhcp-libs-4.2.5-58.el7.centos.x86_64 6/7
Verifying : 12:dhclient-4.2.5-58.el7.centos.x86_64 7/7
Installed:
dhcp.x86_64 12:4.2.5-58.el7.centos.1
Dependency Updated:
dhclient.x86_64 12:4.2.5-58.el7.centos.1 dhcp-common.x86_64 12:4.2.5-58.el7.centos.1
dhcp-libs.x86_64 12:4.2.5-58.el7.centos.1
Complete!
- Check ethernet adaptor name, ifconfig
[ansible@ansible01 test]$ ifconfig
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.200.0.30 netmask 255.255.255.255 broadcast 10.200.0.30
inet6 fe80::9079:5fdd:75d8:b9a6 prefixlen 64 scopeid 0x20<link>
ether 00:50:56:8f:4b:e2 txqueuelen 1000 (Ethernet)
RX packets 44856 bytes 29756654 (28.3 MiB)
RX errors 0 dropped 43 overruns 0 frame 0
TX packets 21088 bytes 3974159 (3.7 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
- Add specific interface to /etc/sysconfig/dhcpd
[ansible@ansible01 test]$ cat /etc/sysconfig/dhcpd
# WARNING: This file is NOT used anymore.
# If you are here to restrict what interfaces should dhcpd listen on,
# be aware that dhcpd listens *only* on interfaces for which it finds subnet
# declaration in dhcpd.conf. It means that explicitly enumerating interfaces
# also on command line should not be required in most cases.
# If you still insist on adding some command line options,
# copy dhcpd.service from /lib/systemd/system to /etc/systemd/system and modify
# it there.
# https://fedoraproject.org/wiki/Systemd#How_do_I_customize_a_unit_file.2F_add_a_custom_unit_file.3F
# example:
# $ cp /usr/lib/systemd/system/dhcpd.service /etc/systemd/system/
# $ vi /etc/systemd/system/dhcpd.service
# $ ExecStart=/usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid <your_interface_name(s)>
# $ systemctl --system daemon-reload
# $ systemctl restart dhcpd.service
DHCPDARGS=ens160
[ansible@ansible01 test]$
- configure dhcp on ansible host
[ansible@ansible01 etc]$ sudo cat /etc/dhcp/dhcpd.conf
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.example
# see dhcpd.conf(5) man page
#
option domain-name "test.flair.local";
option domain-name-servers 10.200.0.30, 8.8.8.8;
default-lease-time 3600;
max-lease-time 7200;
authoritative;
subnet 10.200.0.0 netmask 255.255.255.0 {
option routers 10.200.0.1;
option subnet-mask 255.255.255.0;
option domain-search "test.local";
option domain-name-servers 10.200.0.30;
range 10.200.0.100 10.200.0.150;
}
[ansible@ansible01 etc]$
Add ssh key to template
- Generate and copy key to template. Ansible ip is 10.200.0.30 and template ip is 10.200.0.100 in my case and username is ansible. Before running this script, make sure that .ssh folder is not created under user home directory from the template folder.
[ansible@ansible01 ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ansible/.ssh/id_rsa):
Created directory '/home/ansible/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/ansible/.ssh/id_rsa.
Your public key has been saved in /home/ansible/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:tiyy8V8MUu+m9BS8KygKzIt0gceXPyTwAnyBZDacjic ansible@ansible01.flair.local
The key's randomart image is:
+---[RSA 2048]----+
|o=o. |
|++. . |
|oo o . |
|E.* o .. o |
| + = =..S + |
|o . + ++ = o |
|.+ .o .++ B |
|o.o .=.oo* . |
|........o.o |
+----[SHA256]-----+
[ansible@ansible01 ~]$ ls .ssh/
id_rsa id_rsa.pub
[ansible@ansible01 ~]$
[ansible@ansible01 ~]$ scp .ssh/id_rsa.pub ansible@10.200.0.100:~/.ssh/authorized_keys
ansible@10.200.0.100's password:
id_rsa.pub 100% 418 75.0KB/s 00:00
[ansible@ansible01 ~]$
[ansible@ansible01 .ssh]$ ssh-copy-id -i ~/.ssh/id_rsa.pub ansible@10.200.0.100
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/ansible/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
ansible@10.200.0.100's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'ansible@10.200.0.100'"
and check to make sure that only the key(s) you wanted were added.
[ansible@ansible01 .ssh]$ ssh 10.200.0.100
Last login: Wed Feb 14 16:23:31 2018 from 10.200.0.30
[ansible@localhost ~]$
-
Check whether network card is active during boot from /etc/sysconfig/network-scripts/ifcfg-ens160. The valule of ONBOOT should be yes, ONBOOT=yes
-
restart network, systemctl restart network
YAML file
- name: Create VM from Template
hosts: localhost
connection: local
become: yes
tasks:
- name: createVM from Template
vsphere_guest:
vcenter_hostname: 10.200.0.11
validate_certs: no
username: administrator@vsphere.local
password: vsphere_password
guest: centostemplate04
from_template: yes
template_src: CentOSTemplate
cluster: FlairCluster
resource_pool: "/Resources/Docker"
state: powered_off
vm_extra_config:
folder: Docker
- name: Retrieve Cloned VM
vsphere_guest:
vcenter_hostname: 10.200.0.11
validate_certs: no
username: administrator@vsphere.local
password: vsphere_password
guest: centostemplate04
vmware_guest_facts: yes
register: clonedvm
- name: Update DHCPD Entry
blockinfile:
path: /etc/dhcp/dhcpd.conf
marker: ""
state: present
insertafter: EOF
block: |
host centostemplate04 {
hardware ethernet {{ clonedvm.ansible_facts.hw_eth0.macaddress }};
fixed-address 10.200.0.151;
}
- name: Restart DHCPD from ansible host
shell: 'sudo systemctl restart dhcpd'
sudo: yes
async: 30
poll: 0
ignore_errors: true
# - name: Reboot
# shell: ssh {{ clonedvm.ansible_facts.hw_eth0.ipaddresses[0] }} 'sudo reboot'
# sudo: yes
# async: 30
# poll: 0
# ignore_errors: true
- name: Boot Cloned VM
vsphere_guest:
vcenter_hostname: 10.200.0.11
validate_certs: no
username: administrator@vsphere.local
password: vsphere_password
guest: centostemplate04
state: powered_on
- debug:
var: clonedvm
Result
[ansible@ansible01 test]$ ansible-playbook createtemplate.yml --extra-vars "ansible_sudo_pass=rootpassword"
[WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the
implicit localhost does not match 'all'
[WARNING]: Ignoring invalid attribute: sudo
PLAY [Create VM from Template] ************************************************************
TASK [Gathering Facts] ********************************************************************
ok: [localhost]
TASK [createVM from Template] *************************************************************
changed: [localhost]
TASK [Retrieve Cloned VM] *****************************************************************
ok: [localhost]
TASK [Update DHCPD Entry] *****************************************************************
changed: [localhost]
TASK [Restart DHCPD from ansible host] ****************************************************
changed: [localhost]
TASK [Boot Cloned VM] *********************************************************************
ok: [localhost]
TASK [debug] ******************************************************************************
ok: [localhost] => {
"clonedvm": {
"ansible_facts": {
"hw_eth0": {
"addresstype": "assigned",
"ipaddresses": null,
"label": "Network adapter 1",
"macaddress": "00:50:56:8f:3e:9a",
"macaddress_dash": "00-50-56-8f-3e-9a",
"summary": "VM Network"
},
"hw_guest_full_name": "CentOS 4/5/6/7 (64-bit)",
"hw_guest_id": "centos64Guest",
"hw_instance_uuid": "500f129e-d580-af9b-0a99-85d794ffe12c",
"hw_interfaces": [
"eth0"
],
"hw_memtotal_mb": 4096,
"hw_name": "centostemplate04",
"hw_power_status": "POWERED ON",
"hw_processor_count": 1,
"hw_product_uuid": "420fcd82-9a66-b389-1029-fab5922d40da",
"module_hw": true
},
"changed": false,
"failed": false
}
}
PLAY RECAP ********************************************************************************
localhost : ok=7 changed=3 unreachable=0 failed=0
[ansible@ansible01 test]$
Summary
From this playbook, we've cloned new VM from template and assigned fixed IP using DHCP function. The tutorial, we will integrate DHCP with DNS and will update hostname.
Comments
Post a Comment