Execution
Date 11 Sep 2024 11:59:27 +0100
Duration 00:00:11.58
Controller ssh-gw-4.layershift.com
User root
Versions
Ansible 2.16.4
ara 1.7.1 / 1.7.1
Python 3.10.10
Summary
21 Hosts
11 Tasks
68 Results
3 Plays
6 Files
0 Records

File: /home/ssh-gateway/ansible/kuly/create3.yaml

---
- name: Manage inventory files
  hosts: localhost
  gather_facts: false
  vars:
    files:
      - /home/ssh-gateway/ansible/kuly/canary-inventory
      - /tmp/plesk_hosts.txt
  tasks:
    - name: Delete existing inventory and temp files
      ansible.builtin.file:
        path: "{{ item }}"
        state: absent
      loop: "{{ files }}"

    - name: Create inventory file and header
      ansible.builtin.lineinfile:
        path: /home/ssh-gateway/ansible/kuly/canary-inventory
        line: "[canary]\n"
        create: true
        mode: '0644'

- name: Collect Plesk hosts
  hosts: all
  gather_facts: false
  roles:
    - plesk
  tasks:
    - name: Save Plesk hostnames to a temp file
      when: plesk_found
      ansible.builtin.lineinfile:
        path: /tmp/plesk_hosts.txt
        line: "{{ inventory_hostname }}"
        create: true
        mode: '0644'
      delegate_to: localhost

- name: Process Plesk hosts
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Read Plesk hosts from file
      ansible.builtin.set_fact:
        plesk_hosts_content: "{{ lookup('file', '/tmp/plesk_hosts.txt') }}"

    - name: Convert Plesk hosts content into list
      ansible.builtin.set_fact:
        plesk_hosts: "{{ plesk_hosts_content.split('\n') | select('match', '^.+$') | list }}"

    - name: Debug collected Plesk hosts
      debug:
        msg: "{{ plesk_hosts }}"

    - name: Debug length of plesk_hosts
      debug:
        msg: "Length of plesk_hosts is {{ plesk_hosts | length }}"

    - name: Ensure there are hosts to select
      ansible.builtin.fail:
        msg: "No Plesk hosts found to select from."
      when: plesk_hosts | length == 0

    - name: Select 10 random Plesk hosts
      ansible.builtin.set_fact:
        random_plesk_hosts: >
          {% if plesk_hosts | length > 10 %}
            {{ plesk_hosts | community.general.random(10) }}
          {% else %}
            {{ plesk_hosts }}
          {% endif %}

    - name: Debug selected random Plesk hosts
      debug:
        msg: "{{ random_plesk_hosts }}"

    - name: Store selected Plesk hosts in a local variable
      ansible.builtin.set_fact:
        canary_hosts: "{{ random_plesk_hosts }}"

- name: Add selected Plesk hosts to inventory
  hosts: localhost
  gather_facts: false
  vars:
    inv_file: /home/ssh-gateway/ansible/kuly/canary-inventory
  tasks:
    - name: Add selected Plesk hosts to inventory file
      ansible.builtin.lineinfile:
        path: "{{ inv_file }}"
        line: "{{ item }}"
        insertafter: "[canary]"
      loop: "{{ canary_hosts }}"

- name: Add footer to inventory file
  hosts: localhost
  gather_facts: false
  vars:
    inv_file: /home/ssh-gateway/ansible/kuly/canary-inventory
  tasks:
    - name: Add vars to the inventory file
      ansible.builtin.lineinfile:
        path: "{{ inv_file }}"
        line: |
          [canary:vars]
          ansible_user=root
          ansible_port=2233
        insertafter: EOF