Execution
Date 11 Sep 2024 08:29:25 +0100
Duration 00:01:14.91
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
139 Hosts
7 Tasks
555 Results
3 Plays
6 Files
0 Records

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

- name: Delete file if exists and create header
  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
      run_once: true

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

- name: Playbook to generate inventory with plesk servers only
  hosts: all
  gather_facts: false
  vars:
    inv_file: /home/ssh-gateway/ansible/kuly/canary-inventory
  roles:
    - plesk
  tasks:
    - name: Select random 10 hosts
      ansible.builtin.set_fact:
        random_hosts: "{{ groups['all'] | select('match', inventory_hostname) | shuffle | list | slice(10) }}"
      run_once: true
      delegate_to: localhost

    - name: If Plesk server add name to list
      when: inventory_hostname in random_hosts and plesk_found
      ansible.builtin.lineinfile:
        path: "{{ inv_file }}"
        line: "{{ inventory_hostname }}"
        insertafter: "[canary]"
      delegate_to: localhost

- 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
      run_once: true
      delegate_to: localhost