Sfoglia il codice sorgente

Issue:212 Issue in setting the hostname using mapping file

Signed-off-by: Shubhangi-dell <shubhangi_srivastava@dell.com>
Lucas A. Wilson 4 anni fa
parent
commit
3b6d73fc9d

+ 1 - 1
appliance/roles/common/tasks/password_config.yml

@@ -17,7 +17,7 @@
   command: cat {{ input_config_filename }}
   command: cat {{ input_config_filename }}
   changed_when: false
   changed_when: false
   register: config_content
   register: config_content
-S
+
 - name: Decrpyt appliance_config.yml
 - name: Decrpyt appliance_config.yml
   command: >-
   command: >-
     ansible-vault decrypt {{ input_config_filename }}
     ansible-vault decrypt {{ input_config_filename }}

+ 6 - 2
appliance/roles/inventory/files/add_host.yml

@@ -35,9 +35,13 @@
   lineinfile:
   lineinfile:
     path:  "/root/inventory"
     path:  "/root/inventory"
     line: "    {{ item }}:\n      _awx_description: {{ host_description }}"
     line: "    {{ item }}:\n      _awx_description: {{ host_description }}"
-  when: not check_host.stdout | regex_search(item)
+  when:
+    - not check_host.stdout | regex_search(item)
+    - host_description != "Description Unavailable"
 
 
 - name: Host added msg
 - name: Host added msg
   debug:
   debug:
     msg: "{{ host_added_msg + item }}"
     msg: "{{ host_added_msg + item }}"
-  when: not check_host.stdout | regex_search(item)
+  when:
+    - not check_host.stdout | regex_search(item)
+    - host_description != "Description Unavailable"

+ 26 - 12
appliance/roles/inventory/files/create_inventory.yml

@@ -47,53 +47,66 @@
 - name: Set hostname on reachable nodes and gather facts
 - name: Set hostname on reachable nodes and gather facts
   hosts: reachable
   hosts: reachable
   gather_facts: False
   gather_facts: False
+  ignore_unreachable: true
   remote_user: "{{ cobbler_username }}"
   remote_user: "{{ cobbler_username }}"
   vars:
   vars:
     ansible_password: "{{ cobbler_password }}"
     ansible_password: "{{ cobbler_password }}"
     ansible_become_pass: "{{ cobbler_password }}"
     ansible_become_pass: "{{ cobbler_password }}"
     ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
     ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
+    mapping_file_present: ""
   tasks:
   tasks:
     - name: Setup
     - name: Setup
       setup:
       setup:
        filter: ansible_*
        filter: ansible_*
-    
+
     - name: Check hostname of server
     - name: Check hostname of server
       command: hostname
       command: hostname
       register: hostname_check
       register: hostname_check
-      changed_when: false     
+      changed_when: false
+      ignore_errors: true
 
 
     - name: Check if IP present in mapping file
     - name: Check if IP present in mapping file
-      shell: grep "{{ inventory_hostname }}" {{ role_path }}/files/new_mapping_file.csv
+      command: grep "{{ inventory_hostname }}" ../../provision/files/new_mapping_file.csv
       delegate_to: localhost
       delegate_to: localhost
       register: file_present
       register: file_present
-      changed_when: false
+      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 | bool == true
+      ignore_errors: true
 
 
     - name: Get the static hostname from mapping file
     - name: Get the static hostname from mapping file
-      shell:  grep -Po ".* (?="{{ inventory_hostname }}")" {{ role_path }}/files/new_mapping_file.csv| awk -F',' '{print $2}'
+      shell: awk -F',' '$3 == "{{ inventory_hostname }}" { print $2 }' ../../provision/files/new_mapping_file.csv
       delegate_to: localhost
       delegate_to: localhost
-      when: ('localhost' in hostname_check.stdout) and (file_present.stdout != "")
-      changed_when: false
+      when: ('localhost' in hostname_check.stdout) and (mapping_file_present != "" ) and ( mapping_file | bool == true )
       register: host_name
       register: host_name
+      ignore_errors: true
 
 
     - name: Set the hostname from mapping file
     - name: Set the hostname from mapping file
       hostname:
       hostname:
         name: "{{ host_name.stdout }}"
         name: "{{ host_name.stdout }}"
       register: result_host_name
       register: result_host_name
-      when: ('localhost' in hostname_check.stdout) and (file_present.stdout != "")
+      when: ('localhost' in hostname_check.stdout) and (mapping_file_present != "" ) and  (mapping_file | bool == true )
+      ignore_errors: true
 
 
     - name: Set the system hostname
     - name: Set the system hostname
       hostname:
       hostname:
         name: "compute{{ inventory_hostname.split('.')[-2] + '-' + inventory_hostname.split('.')[-1] }}"
         name: "compute{{ inventory_hostname.split('.')[-2] + '-' + inventory_hostname.split('.')[-1] }}"
       register: result_name
       register: result_name
-      when: ('localhost' in hostname_check.stdout) and (file_present.stdout == "")
+      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:
       lineinfile:
         dest: /etc/hosts
         dest: /etc/hosts
         regexp: '^127\.0\.0\.1[ \t]+localhost'
         regexp: '^127\.0\.0\.1[ \t]+localhost'
         line: "127.0.0.1 localhost {{ host_name.stdout }}"
         line: "127.0.0.1 localhost {{ host_name.stdout }}"
         state: present
         state: present
-      when: "'localhost' in hostname_check.stdout" and (file_present.stdout != "")
+      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
     - name: Add new hostname to /etc/hosts
       lineinfile:
       lineinfile:
@@ -101,7 +114,8 @@
         regexp: '^127\.0\.0\.1[ \t]+localhost'
         regexp: '^127\.0\.0\.1[ \t]+localhost'
         line: "127.0.0.1 localhost 'compute{{ inventory_hostname.split('.')[-2] + '-' + inventory_hostname.split('.')[-1] }}'"
         line: "127.0.0.1 localhost 'compute{{ inventory_hostname.split('.')[-2] + '-' + inventory_hostname.split('.')[-1] }}'"
         state: present
         state: present
-      when: "'localhost' in hostname_check.stdout" and (file_present.stdout == "" )
+      when: ('localhost' in hostname_check.stdout) and (mapping_file | bool == false )
+      ignore_errors: true
 
 
 - name: Update inventory
 - name: Update inventory
   hosts: localhost
   hosts: localhost

+ 4 - 3
appliance/roles/inventory/tasks/main.yml

@@ -49,6 +49,7 @@
     - name: Save input variables from file
     - name: Save input variables from file
       set_fact:
       set_fact:
         cobbler_password: "{{ provision_password }}"
         cobbler_password: "{{ provision_password }}"
+        mapping_file: "{{ mapping_file_exists }}"
       no_log: True
       no_log: True
 
 
     - name: Encrypt input config file
     - name: Encrypt input config file
@@ -79,16 +80,16 @@
           command: >-
           command: >-
             ansible-playbook -i {{ role_path }}/files/provisioned_hosts.yml
             ansible-playbook -i {{ role_path }}/files/provisioned_hosts.yml
             {{ role_path }}/files/create_inventory.yml
             {{ role_path }}/files/create_inventory.yml
-            --extra-vars "cobbler_username={{ cobbler_username }} cobbler_password={{ cobbler_password }}"
+            --extra-vars "cobbler_username={{ cobbler_username }} cobbler_password={{ cobbler_password }} mapping_file={{ mapping_file | bool }}"
           no_log: True
           no_log: True
           register: register_error
           register: register_error
       rescue:
       rescue:
         - name: Fail if host addition was not successful
         - name: Fail if host addition was not successful
           fail:
           fail:
-            msg: "{{ register_error.stdout | regex_replace(cobbler_username) | regex_replace(cobbler_password) }}"
+            msg: "{{ register_error.stderr + register_error.stdout | regex_replace(cobbler_username) | regex_replace(cobbler_password) }}"
 
 
   when: provisioned_file_result.stat.exists
   when: provisioned_file_result.stat.exists
 
 
 - name: push inventory to AWX
 - name: push inventory to AWX
   command: awx-manage inventory_import --inventory-name {{ omnia_inventory_name }} --source /root/inventory
   command: awx-manage inventory_import --inventory-name {{ omnia_inventory_name }} --source /root/inventory
-  when: provisioned_file_result.stat.exists
+  when: provisioned_file_result.stat.exists

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

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

+ 1 - 1
appliance/roles/provision/tasks/provision_password.yml

@@ -34,7 +34,7 @@
   tags: install
   tags: install
 
 
 - name: Copy cobbler password to cobbler config file
 - name: Copy cobbler password to cobbler config file
-  shell: printf "%s:%s:%s\n" "{{ username }}" "Cobbler" "{{ encrypt_SSS.stdout }}" > "{{ role_path }}/files/.users.digest"
+  shell: printf "%s:%s:%s\n" "{{ username }}" "Cobbler" "{{ encrypt_password.stdout }}" > "{{ role_path }}/files/.users.digest"
   changed_when: false
   changed_when: false
   no_log: true
   no_log: true
   tags: install
   tags: install