openstack.yml 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. ---
  2. - no_log: true
  3. block:
  4. - name: fetch all nodes from openstack shade dynamic inventory
  5. command: shade-inventory --list
  6. register: registered_nodes_output
  7. when: refresh_inventory|bool
  8. - name: set fact for openstack inventory cluster nodes
  9. set_fact:
  10. registered_nodes: "{{ (registered_nodes_output.stdout | from_json) | json_query(q) }}"
  11. vars:
  12. q: "[] | [?metadata.clusterid=='{{stack_name}}']"
  13. when:
  14. - refresh_inventory|bool
  15. - name: set_fact for openstack inventory nodes
  16. set_fact:
  17. registered_bastion_nodes: "{{ (registered_nodes_output.stdout | from_json) | json_query(q) }}"
  18. registered_nodes_floating: "{{ (registered_nodes_output.stdout | from_json) | json_query(q2) }}"
  19. vars:
  20. q: "[] | [?metadata.group=='infra.{{stack_name}}']"
  21. q2: "[] | [?metadata.clusterid=='{{stack_name}}'] | [?public_v4!='']"
  22. when:
  23. - refresh_inventory|bool
  24. - name: set_fact for openstack inventory nodes with provider network
  25. set_fact:
  26. registered_nodes_floating: "{{ (registered_nodes_output.stdout | from_json) | json_query(q) }}"
  27. vars:
  28. q: "[] | [?metadata.clusterid=='{{stack_name}}'] | [?public_v4=='']"
  29. when:
  30. - refresh_inventory|bool
  31. - openstack_provider_network_name|default(None)
  32. - name: Add cluster nodes w/o floating IPs to inventory
  33. with_items: "{{ registered_nodes|difference(registered_nodes_floating) }}"
  34. add_host:
  35. name: '{{ item.name }}'
  36. ansible_host: >-
  37. {% if use_bastion|bool -%}
  38. {{ item.name }}
  39. {%- else -%}
  40. {%- set node = registered_nodes | json_query("[?name=='" + item.name + "']") -%}
  41. {{ node[0].addresses[openstack_private_network|quote][0].addr }}
  42. {%- endif %}
  43. ansible_fqdn: '{{ item.name }}'
  44. ansible_user: '{{ ssh_user }}'
  45. ansible_private_key_file: '{{ private_ssh_key }}'
  46. ansible_ssh_extra_args: '-F {{ ssh_config_path }}'
  47. private_v4: >-
  48. {% set node = registered_nodes | json_query("[?name=='" + item.name + "']") -%}
  49. {{ node[0].addresses[openstack_private_network|quote][0].addr }}
  50. - name: Add cluster nodes with floating IPs to inventory
  51. with_items: "{{ registered_nodes_floating }}"
  52. add_host:
  53. name: '{{ item.name }}'
  54. ansible_host: >-
  55. {% if use_bastion|bool -%}
  56. {{ item.name }}
  57. {%- elif openstack_provider_network_name|default(None) -%}
  58. {{ item.private_v4 }}
  59. {%- else -%}
  60. {{ item.public_v4 }}
  61. {%- endif %}
  62. ansible_fqdn: '{{ item.name }}'
  63. ansible_user: '{{ ssh_user }}'
  64. ansible_private_key_file: '{{ private_ssh_key }}'
  65. ansible_ssh_extra_args: '-F {{ ssh_config_path }}'
  66. private_v4: >-
  67. {% set node = registered_nodes | json_query("[?name=='" + item.name + "']") -%}
  68. {{ node[0].addresses[openstack_private_network|quote][0].addr }}
  69. public_v4: >-
  70. {% if openstack_provider_network_name|default(None) -%}
  71. {{ item.private_v4 }}
  72. {%- else -%}
  73. {{ item.public_v4 }}
  74. {%- endif %}
  75. # Split registered_nodes into old nodes and new app nodes
  76. # Add new app nodes to new_nodes host group for upscaling
  77. - name: Create new_app_nodes variable
  78. set_fact:
  79. new_app_nodes: []
  80. - name: Filter new app nodes out of registered_nodes
  81. include: filter_out_new_app_nodes.yaml
  82. with_items: "{{ registered_nodes }}"
  83. loop_control:
  84. loop_var: node
  85. - name: Add new app nodes to the new_nodes section (if a deployment already exists)
  86. with_items: "{{ new_app_nodes }}"
  87. add_host:
  88. name: "{{ item.name }}"
  89. groups: new_nodes, app
  90. - name: Add the rest of cluster nodes to their corresponding groups
  91. with_items: "{{ registered_nodes }}"
  92. add_host:
  93. name: '{{ item.name }}'
  94. groups: '{{ item.metadata.group }}'
  95. - name: Add bastion node to inventory
  96. add_host:
  97. name: bastion
  98. groups: bastions
  99. ansible_host: '{{ registered_bastion_nodes[0].public_v4 }}'
  100. ansible_fqdn: '{{ registered_bastion_nodes[0].name }}'
  101. ansible_user: '{{ ssh_user }}'
  102. ansible_private_key_file: '{{ private_ssh_key }}'
  103. ansible_ssh_extra_args: '-F {{ ssh_config_path }}'
  104. private_v4: >-
  105. {% set node = registered_nodes | json_query("[?name=='" + registered_bastion_nodes[0].name + "']") -%}
  106. {{ node[0].addresses[openstack_private_network|quote][0].addr }}
  107. public_v4: '{{ registered_bastion_nodes[0].public_v4 }}'
  108. when:
  109. - registered_bastion_nodes is defined
  110. - use_bastion|bool