-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_run_command.yml
70 lines (69 loc) · 2 KB
/
task_run_command.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
- name: Run command
block:
- name: Initialize a session with device named {{ item }}
uri:
url: "{{ CSAPIURL | default('https://api.crowdstrike.com') }}/real-time-response/entities/sessions/v1"
method: POST
headers:
accept: application/json
Authorization: "Bearer {{ jwt }}"
body_format: json
body:
device_id: "{{ hostvars[item].device_id }}"
#origin: "string"
queue_offline: "{{ queue_offline }}"
timeout: 60
status_code:
- 201
- -1
- 404
register: session
- name: Print session errors
ansible.builtin.debug:
msg:
- "{{ session.json.errors }}"
when: session.json is defined
- name: Run command in session
uri:
url: "{{ CSAPIURL | default('https://api.crowdstrike.com') }}/real-time-response/entities/admin-command/v1"
method: POST
headers:
accept: application/json
Authorization: "Bearer {{ jwt }}"
body_format: json
body:
base_command: runscript
session_id: "{{ session.json.resources[0].session_id }}"
command_string: "runscript -Raw=```{{ command }}```"
#command_string: "runscript -Raw=```cmd /c echo hello>hello.txt```"
timeout: 60
status_code:
- 201
- -1
register: response
when:
- session.json is defined
- session.json.errors == None
- name: Print the response
ansible.builtin.debug:
msg:
- "{{ response.json.resources }}"
- "{{ response.json.errors }}"
when: response.json is defined
rescue:
- name: Authenticate
include_tasks: task_auth.yml
- name: Store JWT
set_fact:
retries: "{{ retries|int + 1 }}"
jwt: "{{ response.json.access_token }}"
- name: Print current number of retries
ansible.builtin.debug:
msg:
- "{{ retries }}"
- name: Try command again
include_tasks: task_run_command.yml
when: retries|int < 2
- name: Reset retries after giving up on host
set_fact:
retries: 0