-
Notifications
You must be signed in to change notification settings - Fork 1
/
ansible-flag-parser-example.ynl
71 lines (64 loc) · 1.7 KB
/
ansible-flag-parser-example.ynl
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
71
#!/usr/bin/env ansible-flags-parser.py
# --- METADATA ---
# Embedded YAML metadata defining playbooks and variables
playbook: main_playbook.yml
flags:
flag1:
help: "First flag"
required: true
flag2:
help: "Second flag"
default: "value2"
no_ctrlc:
help: "Disable CTRL+C trapping"
required: false
default: false
rescuer:
help: "Execute rescuer playbook on failure"
required: false
default: false
environment:
MY_ENV_VAR: "some_value"
ansible_options:
--limit: "localhost"
--tags: "test"
# --- END METADATA ---
# --- PLAYBOOKS ---
# Embedded YAML playbooks
- name: Metadata Parsing Playbook
hosts: localhost
gather_facts: no
vars:
flag1:
help: "First flag"
required: true
flag2:
help: "Second flag"
default: "value2"
no_ctrlc:
help: "Disable CTRL+C trapping"
required: false
default: false
rescuer:
help: "Execute rescuer playbook on failure"
required: false
default: false
_ansible_flag_parser: false # Indicator that the parser has completed
tasks:
- name: No-op task to hold metadata
ansible.builtin.debug:
msg: "Metadata is loaded in variables."
when: _ansible_flag_parser is not defined or not _ansible_flag_parser
- name: Main Execution Playbook
hosts: localhost
gather_facts: no
vars:
_ansible_flag_parser: true # Set this to indicate metadata has been parsed
tasks:
- name: Parse Flags with Python Script
ansible.builtin.command: "./ansible_flag_parser.py"
when: not _ansible_flag_parser
- name: Main Task Execution
ansible.builtin.debug:
msg: "Executing main playbook tasks with parsed flags."
# --- END PLAYBOOKS ---