Execution
Date 11 Sep 2024 08:39:40 +0100
Duration 00:00:58.90
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
418 Results
4 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: Select random 10 hosts
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Select random 10 hosts from all available hosts
      ansible.builtin.set_fact:
        random_hosts: "{{ groups['all'] | shuffle | list | slice(10) }}"
      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: If Plesk server, add name to inventory file
      when: inventory_hostname in hostvars['localhost']['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