|
| 1 | +--- |
| 2 | +- name: Install Elastic Agent and Monitoring Tools |
| 3 | + hosts: win*,lin* |
| 4 | + gather_facts: no |
| 5 | + vars: |
| 6 | + elastic_user: "{{ lookup('env', 'ELASTIC_USER') | default('elastic') }}" |
| 7 | + elastic_password: "{{ lookup('env', 'ELASTIC_PASSWORD') }}" |
| 8 | + kibana_port: 5601 |
| 9 | + es_port: 9200 |
| 10 | + fleet_port: 8220 |
| 11 | + agent_arch: "x86_64" |
| 12 | + temp_dir: "/tmp" |
| 13 | + win_temp_dir: "C:\\Temp" |
| 14 | + sysmon_url: "https://download.sysinternals.com/files/Sysmon.zip" |
| 15 | + sysmon_config_url: "https://raw.githubusercontent.com/SwiftOnSecurity/sysmon-config/master/sysmonconfig-export.xml" |
| 16 | + audit_rules_url: "https://raw.githubusercontent.com/Neo23x0/auditd/master/audit.rules" |
| 17 | + |
| 18 | + pre_tasks: |
| 19 | + - name: Fail if elastic_password is not provided |
| 20 | + fail: |
| 21 | + msg: "elastic_password must be provided as an environment variable or extra var (e.g., -e elastic_password=yourpass)" |
| 22 | + when: elastic_password | length == 0 |
| 23 | + run_once: true |
| 24 | + |
| 25 | + - name: Set fleet server URL from lme inventory host |
| 26 | + set_fact: |
| 27 | + fleet_server: "https://{{ hostvars['lme']['ansible_host'] }}:{{ fleet_port }}" |
| 28 | + when: fleet_server is not defined |
| 29 | + delegate_to: localhost |
| 30 | + run_once: true |
| 31 | + |
| 32 | + - name: Fetch Elastic version from Elasticsearch API on lme |
| 33 | + when: elastic_version is not defined |
| 34 | + block: |
| 35 | + - uri: |
| 36 | + url: "https://{{ hostvars['lme']['ansible_host'] }}:{{ es_port }}/" |
| 37 | + method: GET |
| 38 | + user: "{{ elastic_user }}" |
| 39 | + password: "{{ elastic_password }}" |
| 40 | + force_basic_auth: yes |
| 41 | + validate_certs: no |
| 42 | + return_content: yes |
| 43 | + register: es_response |
| 44 | + delegate_to: localhost |
| 45 | + run_once: true |
| 46 | + |
| 47 | + - set_fact: |
| 48 | + elastic_version: "{{ es_response.json.version.number }}" |
| 49 | + delegate_to: localhost |
| 50 | + run_once: true |
| 51 | + |
| 52 | + - name: Fetch Fleet enrollment token from Kibana API on lme |
| 53 | + when: enrollment_token is not defined |
| 54 | + block: |
| 55 | + - uri: |
| 56 | + url: "https://{{ hostvars['lme']['ansible_host'] }}:{{ kibana_port }}/api/fleet/enrollment_api_keys" |
| 57 | + method: GET |
| 58 | + user: "{{ elastic_user }}" |
| 59 | + password: "{{ elastic_password }}" |
| 60 | + force_basic_auth: yes |
| 61 | + validate_certs: no |
| 62 | + headers: |
| 63 | + Content-Type: application/json |
| 64 | + kbn-xsrf: true |
| 65 | + return_content: yes |
| 66 | + register: fleet_response |
| 67 | + delegate_to: localhost |
| 68 | + run_once: true |
| 69 | + |
| 70 | + - set_fact: |
| 71 | + enrollment_token: "{{ fleet_response.json['items'] | selectattr('active') | map(attribute='api_key') | list | first }}" |
| 72 | + when: fleet_response.json['items'] | selectattr('active') | list | length > 0 |
| 73 | + |
| 74 | + - fail: |
| 75 | + msg: "No active Fleet enrollment API keys found. Generate one in Kibana." |
| 76 | + when: fleet_response.json['items'] | selectattr('active') | list | length == 0 |
| 77 | + delegate_to: localhost |
| 78 | + run_once: true |
| 79 | + |
| 80 | + tasks: |
| 81 | + - name: Set hostname on Linux |
| 82 | + when: ansible_system == 'Linux' |
| 83 | + become: true |
| 84 | + become_method: sudo |
| 85 | + hostname: |
| 86 | + name: "{{ inventory_hostname }}" |
| 87 | + |
| 88 | + - name: Set hostname on Windows |
| 89 | + when: ansible_os_family == 'Windows' |
| 90 | + become: true |
| 91 | + become_method: runas |
| 92 | + become_user: "localuser" |
| 93 | + win_hostname: |
| 94 | + name: "{{ inventory_hostname }}" |
| 95 | + |
| 96 | + - name: Reboot Windows host |
| 97 | + when: ansible_os_family == 'Windows' |
| 98 | + win_reboot: |
| 99 | + reboot_timeout: 600 |
| 100 | + post_reboot_delay: 30 |
| 101 | + |
| 102 | + - name: Install Elastic Agent on Linux |
| 103 | + when: ansible_system == 'Linux' |
| 104 | + become: true |
| 105 | + become_method: sudo |
| 106 | + block: |
| 107 | + - name: Gather service facts |
| 108 | + service_facts: |
| 109 | + |
| 110 | + #- name: Debug service facts to verify elastic-agent.service presence |
| 111 | + # debug: |
| 112 | + # var: ansible_facts.services |
| 113 | + |
| 114 | + - name: Skip Elastic Agent installation if already running |
| 115 | + meta: end_host |
| 116 | + when: "'elastic-agent.service' in ansible_facts.services and ansible_facts.services['elastic-agent.service'].status == 'enabled'" |
| 117 | + |
| 118 | + - name: Download Elastic Agent tar.gz |
| 119 | + get_url: |
| 120 | + url: "https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-{{ elastic_version }}-linux-{{ agent_arch }}.tar.gz" |
| 121 | + dest: "{{ temp_dir }}/elastic-agent-{{ elastic_version }}-linux-{{ agent_arch }}.tar.gz" |
| 122 | + validate_certs: yes |
| 123 | + |
| 124 | + - name: Extract Elastic Agent |
| 125 | + unarchive: |
| 126 | + src: "{{ temp_dir }}/elastic-agent-{{ elastic_version }}-linux-{{ agent_arch }}.tar.gz" |
| 127 | + dest: "{{ temp_dir }}" |
| 128 | + remote_src: yes |
| 129 | + |
| 130 | + - name: Install Elastic Agent |
| 131 | + shell: | |
| 132 | + cd {{ temp_dir }}/elastic-agent-{{ elastic_version }}-linux-{{ agent_arch }} |
| 133 | + ./elastic-agent install --url={{ fleet_server }} --enrollment-token={{ enrollment_token }} --insecure -n |
| 134 | + args: |
| 135 | + creates: /opt/Elastic/Agent/elastic-agent |
| 136 | + |
| 137 | + - name: Install Elastic Agent on Windows |
| 138 | + when: ansible_os_family == 'Windows' |
| 139 | + become: true |
| 140 | + become_method: runas |
| 141 | + become_user: "localuser" |
| 142 | + block: |
| 143 | + - name: Check if Elastic Agent service is running |
| 144 | + win_service: |
| 145 | + name: Elastic Agent |
| 146 | + register: elastic_service_status |
| 147 | + |
| 148 | + - name: Set fact for skipping installation |
| 149 | + set_fact: |
| 150 | + skip_elastic_install: "{{ elastic_service_status.exists and elastic_service_status.state == 'running' }}" |
| 151 | + |
| 152 | + - name: Skip Elastic Agent installation if service is running |
| 153 | + debug: |
| 154 | + msg: "Elastic Agent service is already running, skipping installation." |
| 155 | + when: skip_elastic_install |
| 156 | + |
| 157 | + - name: Install Elastic Agent when service is not running |
| 158 | + when: not skip_elastic_install |
| 159 | + block: |
| 160 | + - name: Create temp directory if not exists |
| 161 | + win_file: |
| 162 | + path: "{{ win_temp_dir }}" |
| 163 | + state: directory |
| 164 | + |
| 165 | + - name: Download Elastic Agent ZIP |
| 166 | + win_get_url: |
| 167 | + url: "https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-{{ elastic_version }}-windows-{{ agent_arch }}.zip" |
| 168 | + dest: "{{ win_temp_dir }}\\elastic-agent-{{ elastic_version }}-windows-{{ agent_arch }}.zip" |
| 169 | + |
| 170 | + - name: Extract Elastic Agent |
| 171 | + win_unzip: |
| 172 | + src: "{{ win_temp_dir }}\\elastic-agent-{{ elastic_version }}-windows-{{ agent_arch }}.zip" |
| 173 | + dest: "{{ win_temp_dir }}" |
| 174 | + |
| 175 | + - name: Install Elastic Agent |
| 176 | + win_shell: | |
| 177 | + cd {{ win_temp_dir }}\elastic-agent-{{ elastic_version }}-windows-{{ agent_arch }} |
| 178 | + .\elastic-agent.exe install --url={{ fleet_server }} --enrollment-token={{ enrollment_token }} --insecure -n |
| 179 | + args: |
| 180 | + creates: 'C:\Program Files\Elastic\Agent\elastic-agent.exe' |
| 181 | + |
| 182 | + - name: Install auditd on Linux |
| 183 | + when: ansible_system == 'Linux' |
| 184 | + become: true |
| 185 | + become_method: sudo |
| 186 | + block: |
| 187 | + - name: Gather auditd service facts |
| 188 | + service_facts: |
| 189 | + |
| 190 | + - name: Skip auditd installation if already enabled |
| 191 | + meta: end_host |
| 192 | + when: "'auditd.service' in ansible_facts.services and ansible_facts.services['auditd.service'].status == 'enabled'" |
| 193 | + |
| 194 | + - name: Install auditd package |
| 195 | + package: |
| 196 | + name: |
| 197 | + - auditd |
| 198 | + - "{{ 'audispd-plugins' if ansible_distribution in ['Ubuntu', 'Debian'] else omit }}" |
| 199 | + state: present |
| 200 | + |
| 201 | + - name: Download audit rules |
| 202 | + get_url: |
| 203 | + url: "{{ audit_rules_url }}" |
| 204 | + dest: /etc/audit/rules.d/audit.rules |
| 205 | + mode: "0644" |
| 206 | + |
| 207 | + - name: Load audit rules |
| 208 | + shell: augenrules --load |
| 209 | + changed_when: false |
| 210 | + |
| 211 | + - name: Restart auditd service |
| 212 | + service: |
| 213 | + name: auditd |
| 214 | + state: restarted |
| 215 | + |
| 216 | + - name: Install Sysmon on Windows |
| 217 | + when: ansible_os_family == 'Windows' |
| 218 | + become: true |
| 219 | + become_method: runas |
| 220 | + become_user: "localuser" |
| 221 | + block: |
| 222 | + - name: Check if Sysmon64 service is running |
| 223 | + win_service: |
| 224 | + name: Sysmon64 |
| 225 | + register: sysmon_service_status |
| 226 | + |
| 227 | + - name: Set fact for skipping Sysmon installation |
| 228 | + set_fact: |
| 229 | + skip_sysmon_install: "{{ sysmon_service_status.exists and sysmon_service_status.state == 'running' }}" |
| 230 | + |
| 231 | + - name: Skip Sysmon installation if service is running |
| 232 | + debug: |
| 233 | + msg: "Sysmon64 service is already running, skipping installation." |
| 234 | + when: skip_sysmon_install |
| 235 | + |
| 236 | + - name: Install Sysmon when service is not running |
| 237 | + when: not skip_sysmon_install |
| 238 | + block: |
| 239 | + - name: Download Sysmon ZIP |
| 240 | + win_get_url: |
| 241 | + url: "{{ sysmon_url }}" |
| 242 | + dest: "{{ win_temp_dir }}\\Sysmon.zip" |
| 243 | + |
| 244 | + - name: Extract Sysmon |
| 245 | + win_unzip: |
| 246 | + src: "{{ win_temp_dir }}\\Sysmon.zip" |
| 247 | + dest: "{{ win_temp_dir }}\\Sysmon" |
| 248 | + |
| 249 | + - name: Download Sysmon config |
| 250 | + win_get_url: |
| 251 | + url: "{{ sysmon_config_url }}" |
| 252 | + dest: "{{ win_temp_dir }}\\Sysmon\\sysmonconfig-export.xml" |
| 253 | + |
| 254 | + - name: Install Sysmon |
| 255 | + win_shell: | |
| 256 | + cd {{ win_temp_dir }}\Sysmon |
| 257 | + .\Sysmon64.exe -accepteula -i sysmonconfig-export.xml |
| 258 | + args: |
| 259 | + creates: 'HKLM:\SYSTEM\CurrentControlSet\Services\SysmonDrv' |
| 260 | + # Restart the Windows host to apply Sysmon configuration |
0 commit comments