Execution
Date 11 Sep 2024 09:46:19 +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
0 Hosts
0 Tasks
0 Results
0 Plays
2 Files
0 Records

File: /home/ssh-gateway/ansible/kuly/create1.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: Collect and select Plesk hosts
  hosts: all
  gather_facts: true
  roles:
    - plesk
  tasks:
    - name: Initialize plesk_hosts
      set_fact:
        plesk_hosts: []
      run_once: true
    - name: Gather hosts with Plesk
      when: plesk_found
      ansible.builtin.set_fact:
        plesk_hosts: "{{ plesk_hosts + [inventory_hostname] }}"
- name: Save it locally
  hosts: localhost
  tasks:
    - name: Select 10 random Plesk hosts
      ansible.builtin.set_fact:
        random_plesk_hosts: "{{ plesk_hosts | random(10) }}"
    - name: Store selected Plesk hosts in a local variable
      add_host:
        name: "{{ item }}"
        groups: canary
      with_items: "{{ random_plesk_hosts }}"
      delegate_to: localhost

- 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]"
      with_items: "{{ hostvars['localhost']['groups']['canary'] }}"
      run_once: true

- 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