#$ cat bbb.yml
---
- hosts: localhost
gather_facts: False
vars_prompt:
- name: epcrange
prompt: Enter the number of EPCs that you want to configure
private: False
default: "1"
- name: serverrange
prompt: Enter the number of Cleints that you want to configure
private: False
defualt: "1"
pre_tasks:
- name: Set EPC node id variables
set_fact:
# start: "{{ epcrange.split('..')[0] }}"
# stop: "{{ serverrange.split('..')[-1] }}"
start: 1
stop: "{{epcrange}}"
- name: "Add EPC hosts:"
add_host: name="vm{{item}}" groups=just_created_epcs
with_sequence: "start={{start}} end={{stop}} "
- name: Set Client node id variable
set_fact:
start: 1
stop: "{{serverrange}}"
- name: "Add Client hosts:"
add_host: name="vm{{item}}" groups=just_created_clients
with_sequence: "start={{start}} end={{stop}} "
- hosts: just_created_epcs
serial: 1
gather_facts: False
tasks:
serial: 1
gather_facts: False
vars_prompt:
- name: ServerIP
prompt: Enter the ServerIP to replace
private: False
default: '11.11.4.10'
- name: ServerIPRange
prompt: Enter the ServerIP range
private: False
default: '128'
vars:
- String1: '"ServerIP"'
- String2: '"ServerIPRange"'
#
#
- name: Replace ServerIP in config_file
shell: cd /home/imran/Desktop/tobefetched; sed -i '/{{String1}}/ c "ServerIP" ':' "{{ServerIP}}" ' config_file
when: inventory_hostname == "vm1" # workaround, run_once won't work
ignore_errors: yes
- name: Replace ServerIPRange in config_file
shell: cd /home/imran/Desktop/tobefetched; sed -i '/{{String2}}/ c "ServerIPRange" ':' "{{ServerIPRange}}" ' config_file
when: inventory_hostname == "vm1" # workaround, run_once won't work
ignore_errors: yes
#The first time ,the address that the user enters is copied onto config_file on remote server as is
#Now I need to do some mathematics here and so that I can write a value into the config_file of the remote machine based on my calculations here.
#Something like: The next VM(s) gets values written into the config_file according to some formula (explained in c++ below)
int x=0;
for (int i=1; i<epcrange; i+=pow(2,x)) //pow is a math function with pow(2,x)= 2^x
{
New_LastIPOctet = Last_ServerIPOctet +ServerIPRange/epcrange; //user always enters values in the power of 2, Last_ServerIPOctet is accessed from the IP that the user enters
NewIP=append (ServerIP, New_LastIPOctet); //some function that appends a new value to the last IPV4 octet of a previous IPV4 address.
SendToFileOnRemoteVM = send (); //send to my remote vm and make changes to config_file
x++;
}
//all values are integers
As we can see here, epc range is decided by the user, so the loop is not hard-coded.
I am sorry for using c++ and jumbling it all up, but there is no easy way to explain this