undercloud-predictable_NIC_names.yaml 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. ---
  2. - name: Rename network interfaces
  3. hosts: exampletarget
  4. become: true
  5. vars:
  6. src_prefix: "eth"
  7. dst_prefix: "em"
  8. osnet_conf: "/etc/os-net-config/config.json"
  9. src_interfaces: "{{ ansible_interfaces | select('match', src_prefix ~ '.*') | sort | list }}"
  10. undercloud_conf: "~/undercloud.conf"
  11. tasks:
  12. - debug:
  13. msg: "{{ src_interfaces }}"
  14. - name: Update udev rules
  15. tags:
  16. - udev
  17. lineinfile:
  18. line: >
  19. SUBSYSTEM=="net",
  20. ACTION=="add",
  21. DRIVERS=="?*",
  22. ATTR{address}=="{{ ansible_facts[item]['perm_macaddress'] | default(ansible_facts[item]['macaddress']) }}",
  23. NAME="{{ item | replace(src_prefix, dst_prefix) }}"
  24. path: /etc/udev/rules.d/70-rhosp-persistent-net.rules
  25. create: true
  26. with_items: "{{ src_interfaces | reject('match', '^.*\\..*$') | list }}"
  27. - name: Rename ifcfg files
  28. tags:
  29. - ifcfg
  30. block:
  31. - name: Check that src_prefix files exists
  32. stat:
  33. path: /etc/sysconfig/network-scripts/ifcfg-{{ item }}
  34. register: nic_result
  35. with_items: "{{ src_interfaces }}"
  36. - name: Copy ifcfg files using dst_prefix
  37. copy:
  38. remote_src: true
  39. src: "{{ item.stat.path }}"
  40. dest: "{{ item.stat.path | replace(src_prefix, dst_prefix) }}"
  41. with_items: "{{ nic_result.results }}"
  42. when: item.stat.exists
  43. loop_control:
  44. label: "{{ item.item }}"
  45. - name: Edit NAME in new network-script files
  46. lineinfile:
  47. regexp: "^NAME=.*"
  48. line: "NAME={{ item.item | replace(src_prefix, dst_prefix) }}"
  49. path: "{{ item.stat.path | replace(src_prefix, dst_prefix) }}"
  50. with_items: "{{ nic_result.results }}"
  51. when: item.stat.exists
  52. loop_control:
  53. label: "{{ item.item }}"
  54. - name: Edit DEVICE in new network-script files
  55. lineinfile:
  56. regexp: "^DEVICE=.*"
  57. line: "DEVICE={{ item.item | replace(src_prefix, dst_prefix) }}"
  58. path: "{{ item.stat.path | replace(src_prefix, dst_prefix) }}"
  59. with_items: "{{ nic_result.results }}"
  60. when: item.stat.exists
  61. loop_control:
  62. label: "{{ item.item }}"
  63. - name: Backup old ifcfg network-script files
  64. copy:
  65. remote_src: true
  66. src: "{{ item.stat.path }}"
  67. dest: "{{ item.stat.path }}.bak"
  68. with_items: "{{ nic_result.results }}"
  69. when: item.stat.exists
  70. loop_control:
  71. label: "{{ item.item }}"
  72. - name: Remove old ifcfg network-script files
  73. file:
  74. path: "{{ item.stat.path }}"
  75. state: absent
  76. with_items: "{{ nic_result.results }}"
  77. when: item.stat.exists
  78. loop_control:
  79. label: "{{ item.item }}"
  80. - name: Rename route files
  81. tags:
  82. - routes
  83. block:
  84. - name: Check that route files exists
  85. stat:
  86. path: /etc/sysconfig/network-scripts/route-{{ item }}
  87. register: route_result
  88. with_items: "{{ src_interfaces }}"
  89. - name: Copy route files using dst_prefix
  90. copy:
  91. remote_src: true
  92. src: "{{ item.stat.path }}"
  93. dest: "{{ item.stat.path | replace(src_prefix, dst_prefix) }}"
  94. with_items: "{{ route_result.results }}"
  95. when: item.stat.exists
  96. loop_control:
  97. label: "{{ item.item }}"
  98. - name: Update dst_prefix in route files that use IP command arguments format
  99. replace:
  100. regexp: "{{ src_prefix }}"
  101. replace: "{{ dst_prefix }}"
  102. path: "{{ item.stat.path | replace(src_prefix, dst_prefix) }}"
  103. with_items: "{{ route_result.results }}"
  104. when: item.stat.exists
  105. loop_control:
  106. label: "{{ item.item }}"
  107. - name: Backup old route files
  108. copy:
  109. remote_src: true
  110. src: "{{ item.stat.path }}"
  111. dest: "{{ item.stat.path }}.bak"
  112. with_items: "{{ route_result.results }}"
  113. when: item.stat.exists
  114. loop_control:
  115. label: "{{ item.item }}"
  116. - name: Remove old route files
  117. file:
  118. path: "{{ item.stat.path }}"
  119. state: absent
  120. with_items: "{{ route_result.results }}"
  121. when: item.stat.exists
  122. loop_control:
  123. label: "{{ item.item }}"
  124. - name: Perform a final regex for any remaining src_prefix in ifcfg files
  125. tags:
  126. - ifcfg
  127. block:
  128. - name: Get a list of all ifcfg files
  129. find:
  130. paths: /etc/sysconfig/network-scripts/
  131. patterns: 'ifcfg-*'
  132. excludes: '*.bak'
  133. register: ifcfg_files
  134. - name: Perform final regex on ifcfg files
  135. replace:
  136. path: "{{ item[0].path }}"
  137. regexp: "{{ item[1] }}"
  138. replace: "{{ item[1] | replace(src_prefix, dst_prefix) }}"
  139. with_nested:
  140. - "{{ ifcfg_files.files }}"
  141. - "{{ src_interfaces }}"
  142. loop_control:
  143. label: "{{ item[1] }} -> {{ item[0].path }}"
  144. - name: Replace interface name in TripleO configuration files
  145. tags:
  146. - tripleo
  147. block:
  148. - name: Check if undercloud.conf exists
  149. stat:
  150. path: "{{ undercloud_conf }}"
  151. register: undercloud_conf_stat
  152. - name: Replace interface name in undercloud.conf
  153. replace:
  154. path: "{{ undercloud_conf }}"
  155. regexp: "{{ item }}"
  156. replace: "{{ item | replace(src_prefix, dst_prefix) }}"
  157. when: undercloud_conf_stat.stat.exists
  158. with_items: "{{ src_interfaces }}"
  159. - name: Check if os-net-config's config.json exists
  160. stat:
  161. path: "{{ osnet_conf }}"
  162. register: osnet_conf_stat
  163. - name: Replace interface name in config.json
  164. replace:
  165. path: "{{ osnet_conf }}"
  166. regexp: "{{ item }}"
  167. replace: "{{ item | replace(src_prefix, dst_prefix) }}"
  168. when: osnet_conf_stat.stat.exists
  169. with_items: "{{ src_interfaces }}"