---
- name: Create /etc/nodepool directory
  file:
    path: /etc/nodepool
    state: directory
    owner: "{{ ansible_user }}"
    group: "{{ ansible_user }}"
    mode: 0755

- name: Create /etc/nodepool/node_private
  lineinfile:
    create: true
    state: present
    name: /etc/nodepool/node_private
    owner: "{{ ansible_user }}"
    group: "{{ ansible_user }}"
    line: "{{ subnode_private_ip }}"

- name: Create /etc/nodepool/primary_node_private
  lineinfile:
    create: true
    state: present
    name: /etc/nodepool/primary_node_private
    owner: "{{ ansible_user }}"
    group: "{{ ansible_user }}"
    line: "{{ hostvars['subnode-0'].subnode_private_ip }}"

- name: Create /etc/nodepool/provider
  copy:
    content: |
      NODEPOOL_PROVIDER={% if rdo_cloud_provider|bool %}
      rdo-cloud-tripleo
      {% else %}
      fake-cloud
      {% endif %}
      NODEPOOL_CLOUD=fake-cloud
      NODEPOOL_REGION=FAKE
      NODEPOOL_AZ=
    dest: /etc/nodepool/provider

# Required for single node jobs
- name: Create empty /etc/nodepool/sub_nodes
  copy:
    dest: /etc/nodepool/sub_nodes
    content: ''

- name: Create /etc/nodepool/sub_nodes
  lineinfile:
    create: true
    state: present
    name: /etc/nodepool/sub_nodes
    line: "{{ hostvars[item].subnode_public_ip }}"
  with_inventory_hostnames:
    - subnodes:!subnode-0

# Required for single node jobs
- name: Create empty /etc/nodepool/sub_nodes_private
  copy:
    dest: /etc/nodepool/sub_nodes_private
    content: ''

- name: Create /etc/nodepool/sub_nodes_private
  lineinfile:
    create: true
    state: present
    name: /etc/nodepool/sub_nodes_private
    line: "{{ hostvars[item].subnode_private_ip }}"
  with_inventory_hostnames:
    - subnodes:!subnode-0

- name: Create /etc/nodepool/id_rsa
  shell: >
    ssh-keygen -N "" -f /etc/nodepool/id_rsa;
    chmod 600  /etc/nodepool/id_rsa;
  args:
    creates: /etc/nodepool/id_rsa

- name: Make sure /etc/nodepool/id_rsa is owned by zuul
  file:
    path: '/etc/nodepool/id_rsa{{ item }}'
    owner: "{{ ansible_user }}"
    group: "{{ ansible_user }}"
  with_items:
    - ''
    - '.pub'
