Execution
Date 16 Sep 2024 10:09:25 +0100
Duration 00:00:03.72
Controller ssh-gw-4.layershift.com
User root
Versions
Ansible 2.16.11
ara 1.7.2 / 1.7.2
Python 3.10.10
Summary
2 Hosts
7 Tasks
7 Results
1 Plays
1 Files
0 Records

File: /home/ssh-gateway/ansible/kuly/solus_old/create_inventory_from_solus.yaml

---
- hosts: localhost
  gather_facts: false
  vars:
    ara_playbook_name: create inventory
    ara_playbook_labels:
      - solus
      - playbook
      - create
      - inventory
    folder: /home/ssh-gateway/ansible/kuly
    file: crs.json
    inventory: computeresources_inv.ini

  tasks:

    - name: Get the json
      ansible.builtin.uri:
        url: https://solus.stage.town/api/v1/compute_resources
        method: GET
        headers:
          Content-Type: application/json
          Authorization: "{{ lookup('file','auth_bear') }}"
        follow_redirects: safe
        return_content: true
        status_code: 200
      register: result

    - name: Delete json file first
      tags:
        - delete_file
      ansible.builtin.file:
        path: "{{ folder }}/{{ file }}"
        state: absent

    - name: Delete the old inventory
      ansible.builtin.file:
        path: "{{ folder }}/{{ inventory }}"
        state: absent

    - name: Save to file the json
      tags:
        - save
      ansible.builtin.lineinfile:
        create: true
        path: "{{ folder }}/{{ file }}"
        mode: 0640
        line: "{{ result.json | to_nice_json }}"
      delegate_to: 127.0.0.1

    - name: Generate header
      ansible.builtin.copy:
        dest: "{{ folder }}/{{ inventory }}"
        content: |
          [compute_resources]
        mode: 0640
        owner: root

    - name: Read the file
      ansible.builtin.include_vars: "{{ folder }}/{{ file }}"

    - name: Add hosts
      ansible.builtin.lineinfile:
        path: "{{ folder }}/{{ inventory }}"
        line: "{{ item }} ansible_host={{ item }} ansible_port=2233"
        create: true
        mode: 0640
      delegate_to: 127.0.0.1
      loop: "{{ json.data | dict2items }}"