Execution
Date 11 Sep 2024 11:33:52 +0100
Duration None
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
10 Hosts
3 Tasks
10 Results
2 Plays
1 Files
0 Records

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

---
- name: Manage inventory file
  hosts: localhost
  gather_facts: false
  vars:
    inv_file: /home/ssh-gateway/ansible/kuly/canary-inventory
  tasks:
    - name: Delete inventory file if it exists
      ansible.builtin.file:
        path: "{{ inv_file }}"
        state: absent

    - name: Create file and header
      ansible.builtin.lineinfile:
        path: "{{ inv_file }}"
        line: "[canary]\n"
        create: true
        mode: '0644'

- name: Initialize and collect Plesk hosts
  hosts: all
  gather_facts: true
  tasks:
    - name: Aggregate Plesk hosts if plesk_found is true
      when: plesk_found
      ansible.builtin.set_fact:
        plesk_hosts_temp: "{{ plesk_hosts_temp | default([]) + (ansible_facts.get('plesk_hosts', []) | default([])) }}"
      delegate_to: localhost

- name: Process and save Plesk hosts
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Debug collected Plesk hosts
      debug:
        msg: "{{ plesk_hosts_temp }}"

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

    - name: Select 10 random Plesk hosts
      ansible.builtin.set_fact:
        random_plesk_hosts: "{{ plesk_hosts_temp | random(10) if plesk_hosts_temp | length > 10 else plesk_hosts_temp }}"

    - 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
  hosts: localhost
  gather_facts: false
  vars:
    inv_file: /home/ssh-gateway/ansible/kuly/canary-inventory
  tasks:
    - name: Add vars
      ansible.builtin.lineinfile:
        path: "{{ inv_file }}"
        line: |
          [canary:vars]
          ansible_user=root
          ansible_port=2233
        insertafter: EOF