Execution
Date 14 Jan 2026 14:48:38 +0000
Duration 00:02:50.13
Controller ssh-gw-4.layershift.com
User root
Versions
Ansible 2.16.11
ara 1.7.3 / 1.7.3
Python 3.10.10
Summary
396 Hosts
9 Tasks
795 Results
5 Plays
1 Files
0 Records

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

---
- name: Manage inventory file
  hosts: localhost
  gather_facts: false
  vars:
    files:
      - /home/ssh-gateway/ansible/kuly/canary-inventory
      - /tmp/plesk_hosts.txt
  tasks:
    - name: Delete files first
      ansible.builtin.file:
        path: "{{ item }}"
        state: absent
      with_items: "{{ files }}"
    - name: Create file and header
      ansible.builtin.lineinfile:
        path: /home/ssh-gateway/ansible/kuly/canary-inventory
        line: "[canary]"
        create: true
        mode: '0644'

- name: Initialize and collect Plesk hosts
  hosts: all
  gather_facts: false
  tasks:
    - name: Get server info
      plesk_info:
      register: plsk
    - name: Collect Plesk hosts
      when: plsk.plesk_found and plsk.imunify_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: Select 10 random Plesk hosts
      ansible.builtin.set_fact:
        random_plesk_hosts: >
          {% set number_to_select = 10 %}
          {% set lines = plesk_hosts_content.splitlines() %}
          {% if lines | length > number_to_select %}
            {% set shuffled = lines | shuffle %}
            {{ shuffled[:number_to_select] }}
          {% else %}
            {{ lines }}
          {% endif %}

- 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 one by one
      ansible.builtin.lineinfile:
        path: "{{ inv_file }}"
        line: "{{ item }}"
        insertafter: "[canary]"
      with_items: "{{ random_plesk_hosts | from_yaml }}"
      loop_control:
        loop_var: item

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