Browse Source

Merge pull request #213 from Shubhangi-dell/devel

Issue:#212 Issue in setting the hostname using mapping file
Lucas A. Wilson 4 years ago
parent
commit
132ef46910

+ 9 - 9
appliance/roles/inventory/files/create_inventory.yml

@@ -69,19 +69,19 @@
       command: grep "{{ inventory_hostname }}" ../../provision/files/new_mapping_file.csv
       delegate_to: localhost
       register: file_present
-      when: mapping_file == "true"
+      when: mapping_file | bool == true
       ignore_errors: true
 
     - name: Set fact if mapping file present
       set_fact:
-        mapping_file_present: file_present.stdout
-      when: mapping_file == "true"
+        mapping_file_present: "{{ file_present.stdout }}"
+      when: mapping_file | bool == true
       ignore_errors: true
 
     - name: Get the static hostname from mapping file
       shell: awk -F',' '$3 == "{{ inventory_hostname }}" { print $2 }' ../../provision/files/new_mapping_file.csv
       delegate_to: localhost
-      when: ('localhost' in hostname_check.stdout) and (mapping_file_present != "" ) and ( mapping_file == "true" )
+      when: ('localhost' in hostname_check.stdout) and (mapping_file_present != "" ) and ( mapping_file | bool == true )
       register: host_name
       ignore_errors: true
 
@@ -89,23 +89,23 @@
       hostname:
         name: "{{ host_name.stdout }}"
       register: result_host_name
-      when: ('localhost' in hostname_check.stdout) and (mapping_file_present != "" ) and  (mapping_file == "true" )
+      when: ('localhost' in hostname_check.stdout) and (mapping_file_present != "" ) and  (mapping_file | bool == true )
       ignore_errors: true
 
     - name: Set the system hostname
       hostname:
         name: "compute{{ inventory_hostname.split('.')[-2] + '-' + inventory_hostname.split('.')[-1] }}"
       register: result_name
-      when: ('localhost' in hostname_check.stdout) and (mapping_file_present == "")
+      when: ('localhost' in hostname_check.stdout) and (mapping_file | bool == false)
       ignore_errors: true
 
-    - name: Add new hostname to /etc/hosts
+    - name: Add new hostname to /etc/hosts from mapping file
       lineinfile:
         dest: /etc/hosts
         regexp: '^127\.0\.0\.1[ \t]+localhost'
         line: "127.0.0.1 localhost {{ host_name.stdout }}"
         state: present
-      when: ('localhost' in hostname_check.stdout) and ( mapping_file_present != "" ) and ( mapping_file == "true" )
+      when: ('localhost' in hostname_check.stdout) and ( mapping_file_present != "" ) and ( mapping_file | bool == true )
       ignore_errors: true
 
     - name: Add new hostname to /etc/hosts
@@ -114,7 +114,7 @@
         regexp: '^127\.0\.0\.1[ \t]+localhost'
         line: "127.0.0.1 localhost 'compute{{ inventory_hostname.split('.')[-2] + '-' + inventory_hostname.split('.')[-1] }}'"
         state: present
-      when: ('localhost' in hostname_check.stdout) and (mapping_file_present == "" )
+      when: ('localhost' in hostname_check.stdout) and (mapping_file | bool == false )
       ignore_errors: true
 
 - name: Update inventory

+ 1 - 1
appliance/roles/inventory/tasks/main.yml

@@ -80,7 +80,7 @@
           command: >-
             ansible-playbook -i {{ role_path }}/files/provisioned_hosts.yml
             {{ role_path }}/files/create_inventory.yml
-            --extra-vars "cobbler_username={{ cobbler_username }} cobbler_password={{ cobbler_password }} mapping_file={{ mapping_file }}"
+            --extra-vars "cobbler_username={{ cobbler_username }} cobbler_password={{ cobbler_password }} mapping_file={{ mapping_file | bool }}"
           no_log: True
           register: register_error
       rescue:

+ 2 - 0
appliance/roles/provision/files/inventory_creation.yml

@@ -28,6 +28,7 @@
     - name: Create the static ip
       shell: awk -F',' 'NR >1{print $3}' omnia/appliance/roles/provision/files/new_mapping_file.csv > static_hosts.yml
       changed_when: false
+      ignore_errors: true
 
     - name: Create the dynamic inventory
       shell: |
@@ -35,6 +36,7 @@
         echo "{{ vars_new }}" > temp.txt
         egrep -o '[1-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' temp.txt >>dynamic_hosts.yml
       changed_when: false
+      ignore_errors: true
 
     - name: Final inventory
       shell: cat dynamic_hosts.yml static_hosts.yml| sort -ur  >> omnia/appliance/roles/inventory/files/provisioned_hosts.yml