Execution
Date 10 Sep 2024 13:21:37 +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
1 Hosts
3 Tasks
2 Results
2 Plays
1 Files
0 Records

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

---
- name: Delete file if exists and create header
  hosts: localhost
  gather_facts: false
  vars:
    inv_file: /home/ssh-gateway/ansible/kuly/plesks-without-canary.ini
  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: "[plesks]\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/plesks-without-canary.ini
  tasks:
    - name: Get list of canary hosts
      ansible.builtin.command: "grep -E '^\\w.*\\.plesk-server\\.com' /home/ssh-gateway/ansible/kuly/canary-inventory"
      register: canary_hosts
      changed_when: false

    - name: If plesk server add name to list (skip if in canary inventory)
      when:
        - plesk_found
        - imunify_found
        - inventory_hostname not in canary_hosts.stdout_lines
      ansible.builtin.lineinfile:
        path: "{{ inv_file }}"
        line: "{{ inventory_hostname }}"
        insertafter: "[plesks]"
      delegate_to: localhost

- name: Add footer
  hosts: localhost
  gather_facts: false
  vars:
    inv_file: /home/ssh-gateway/ansible/kuly/plesks-without-canary.ini
  tasks:
    - name: Add vars
      ansible.builtin.lineinfile:
        path: "{{ inv_file }}"
        line: |
          [plesks:vars]
          ansible_user=root
          ansible_port=2233
        insertafter: EOF
      run_once: true
      delegate_to: localhost