Execution
Date 03 Mar 2026 22:14:01 +0000
Duration 00:00:52.71
Controller ssh-gw-4.layershift.com
User root
Versions
Ansible 2.16.13
ara 1.7.4 / 1.7.4
Python 3.10.10
Summary
141 Hosts
9 Tasks
287 Results
5 Plays
1 Files
0 Records

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

---
- name: Manage inventory file
  hosts: localhost
  gather_facts: false
  vars:
    files:
      - /home/ssh-gateway/ansible/ansible-luca/ioncube-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/ansible-luca/ioncube-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) or (plsk.plesk_found and plsk.cloudlinux_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 5 random Plesk hosts
      ansible.builtin.set_fact:
        random_plesk_hosts: >
          {% set number_to_select = 5 %}
          {% 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/ansible-luca/ioncube-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/ansible-luca/ioncube-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