-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_yaml.py
120 lines (111 loc) · 3.92 KB
/
setup_yaml.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {
'metadata_version': '0.1',
'status': ['preview'],
'supported_by': 'community'
}
DOCUMENTATION = '''
---
module: setup_yaml
short_description: Resolves variables from a remote YAML file
version_added: "2.9"
description:
- This module maps contents of a remote YAML file to ansible_facts according to a given variable mapping.
- Within a YAML file, it is also possible to point to other YAML files, which will then be resolved recursively.
- This module can pick up some variables "magically", which makes the module very versatile
but also causes some conventions that need to be followed when using this module.
- Please check out the examples of how to use it.
options:
files:
description:
- A list of files to be resolved.
- The module will not fail but skip if this variable is not provided.
- This parameter can be "magically" provided by defining the variable `setup_yaml`
required: false
smart:
description:
- Will make this module skip in case it was already executed.
Sets a marker into the vars to prevent repeated execution.
- This parameter can be "magically" provided by defining the variable `setup_yaml_smart`
required: false
default: true
recursive:
description:
- Can be used to disable recursive resolution of other files. This can be useful in certain situations.
required: false
default: true
replace:
description:
- A list of replacements that can be used for recursively replacing string values for given keys in
the remote file content.
required: false
author:
- metal-stack
notes:
- We typically use this module for dynamically providing docker image version variables to ansible roles.
In the metal-stack docs, we will also call the contents of the downloaded YAML file a release vector.
'''
EXAMPLES = '''
# Assume a YAML file containing release versions of docker-images at https://example.com/v1.0.0/example.yaml, which
# looks like this:
#
# ---
# docker-images:
# hello-world:
# name: hello-world
# tag: v0.2.0
# ...
#
# Let's now define the following task:
- name: gather release versions
setup_yaml:
files:
- url: https://example.com/v1.0.0/example.yaml
mapping:
hello_world_image_tag: "docker-images.hello-world.tag"
# The expected module return will be:
# {"ansible_facts": {"hello_world_image_tag": "v0.2.0"}}
#
# Remember that ansible_facts are automatically added to the host vars by ansible, such that they are immediately
# available for further usage.
#
#
# It would also be possible to pickup the module parameters "magically". Let's create the following variables somewhere
# in your playbook:
#
# ---
# setup_yaml:
# - url: https://example.com/v1.0.0/example.yaml
# meta_var: example_release
#
# example_release:
# mapping:
# hello_world_image_tag: "docker-images.hello-world.tag"
# ...
#
# Then, you could just write a task definition like this:
- name: gather release versions
setup_yaml:
# The "magic" lookup is extremely helpful because the "example_release" variable can be provided by external roles.
# This could mean that the direct consumer does not need to know the variable mapping.
#
# It is also possible to nest yaml files into each other, making a recursive resolution:
#
#
- name: gather release versions
setup_yaml:
files:
- url: https://example.com/v1.0.0/example.yaml
mapping:
hello_world_image_tag: "docker-images.hello-world.tag"
nested:
- url_path: "other-files.example-2.url"
mapping:
hello_world_2_image_tag: "docker-images.hello-world-2.tag"
#
# Recursive resolution can also use the "magic" lookup!
#
'''