Debian Codegraph @

ansible_collections/community/sops/README.md

raw
1 <!--
2 Copyright (c) Ansible Project
3 GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 SPDX-License-Identifier: GPL-3.0-or-later
5 -->
6
7 # Community SOPS Collection
8 [![Documentation](https://img.shields.io/badge/docs-brightgreen.svg)](https://docs.ansible.com/ansible/devel/collections/community/sops/)
9 [![CI](https://github.com/ansible-collections/community.sops/actions/workflows/nox.yml/badge.svg?branch=main)](https://github.com/ansible-collections/community.sops/actions)
10 [![Codecov](https://img.shields.io/codecov/c/github/ansible-collections/community.sops)](https://codecov.io/gh/ansible-collections/community.sops)
11 [![REUSE status](https://api.reuse.software/badge/github.com/ansible-collections/community.sops)](https://api.reuse.software/info/github.com/ansible-collections/community.sops)
12
13 The `community.sops` collection allows integrating [CNCF SOPS (`getsops/sops`)](https://github.com/getsops/sops) in Ansible.
14
15 `getsops/sops` is a tool for encryption and decryption of files using secure keys (GPG, KMS, age). It can be leveraged in Ansible to provide an easy to use and flexible to manage way to manage ecrypted secrets' files.
16
17 Please note that this collection does **not** support Windows targets.
18
19 ## SOPS version compatibility
20
21 The following table shows which versions of SOPS are tested with the latest version of the collection (for the current major release branch). Older (or newer) versions of SOPS can still work fine, it just means that we did not test them. In some cases, it could be that a minimal required version of SOPS is explicitly documented for a specific feature. This is the case from community.sops 1.8.0 on; from that version on the collection automatically detects the SOPS version to determine whether a feature is supported or not.
22
23 |SOPS release train|SOPS versions|
24 |---|---|
25 |`3.5.x`|`3.5.0` (latest)|
26 |`3.6.x`|`3.6.0`, `3.6.1` (latest)|
27 |`3.7.x`|`3.7.0`, `3.7.3` (latest)|
28 |`3.8.x`|`3.8.0`, `3.8.1` (latest)|
29 |`3.9.x`|`3.9.0`, `3.9.1`, `3.9.2`, `3.9.3`, `3.9.4` (latest)|
30 |`3.10.x`|`3.10.0`, `3.10.1`, `3.10.2` (latest)|
31 |`3.11.x`|`3.11.0` (latest)|
32 |`3.12.x`|`3.12.1`|
33
34 ## Code of Conduct
35
36 We follow [Ansible Code of Conduct](https://docs.ansible.com/ansible/latest/community/code_of_conduct.html) in all our interactions within this project.
37
38 If you encounter abusive behavior violating the [Ansible Code of Conduct](https://docs.ansible.com/ansible/latest/community/code_of_conduct.html), please refer to the [policy violations](https://docs.ansible.com/ansible/latest/community/code_of_conduct.html#policy-violations) section of the Code of Conduct for information on how to raise a complaint.
39
40 ## Communication
41
42 * Join the Ansible forum:
43 * [Get Help](https://forum.ansible.com/c/help/6): get help or help others.Please add appropriate tags if you start new discussions, for example the `sops` tag.
44 * [Posts tagged with 'sops'](https://forum.ansible.com/tag/sops): subscribe to participate in SOPS related conversations.
45 * [Social Spaces](https://forum.ansible.com/c/chat/4): gather and interact with fellow enthusiasts.
46 * [News & Announcements](https://forum.ansible.com/c/news/5): track project-wide announcements including social events.
47
48 * The Ansible [Bullhorn newsletter](https://docs.ansible.com/ansible/devel/community/communication.html#the-bullhorn): used to announce releases and important changes.
49
50 For more information about communication, see the [Ansible communication guide](https://docs.ansible.com/ansible/devel/community/communication.html).
51
52 ## Tested with Ansible
53
54 Tested with the current ansible-core 2.15, ansible-core 2.16, ansible-core 2.17, ansible-core 2.18, ansible-core 2.19, ansible-core 2.20, ansible-core 2.21 releases, and the current development version of ansible-core. Ansible versions before 2.15.0 are not supported.
55
56 ## External requirements
57
58 <!-- List any external resources the collection depends on, for example minimum versions of an OS, libraries, or utilities. Do not list other Ansible collections here. -->
59
60 You will need to install [the `sops` CLI tool](https://github.com/getsops/sops) manually before using plugins provided by this
61 collection.
62
63 ## Collection Documentation
64
65 Browsing the [**latest** collection documentation](https://docs.ansible.com/ansible/latest/collections/community/sops) will show docs for the _latest version released in the Ansible package_, not the latest version of the collection released on Galaxy.
66
67 Browsing the [**devel** collection documentation](https://docs.ansible.com/ansible/devel/collections/community/sops) shows docs for the _latest version released on Galaxy_.
68
69 We also separately publish [**latest commit** collection documentation](https://ansible-collections.github.io/community.sops/branch/main/) which shows docs for the _latest commit in the `main` branch_.
70
71 If you use the Ansible package and do not update collections independently, use **latest**. If you install or update this collection directly from Galaxy, use **devel**. If you are looking to contribute, use **latest commit**.
72
73 ## Included content
74
75 <!-- Galaxy will eventually list the module docs within the UI, but until that is ready, you may need to either describe your plugins etc here, or point to an external docsite to cover that information. -->
76
77 This collection provides:
78
79 - a [lookup plugin](https://docs.ansible.com/ansible/latest/user_guide/playbooks_lookups.html#playbooks-lookups) `community.sops.sops` that allows looking up a SOPS-encrypted file content;
80 - a [filter plugin](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_filters.html) `community.sops.decrypt` that allows decrypting SOPS-encrypted data;
81 - a [vars plugin](https://docs.ansible.com/ansible/latest/plugins/vars.html) `community.sops.sops` that allows loading Ansible vars from SOPS-encrypted files for hosts and groups;
82 - an [action plugin](https://docs.ansible.com/ansible/latest/plugins/action.html) `community.sops.load_vars` that allows loading Ansible vars from a SOPS-encrypted file dynamically during a playbook or role;
83 - a [module](https://docs.ansible.com/ansible/latest/user_guide/basic_concepts.html#modules) `community.sops.sops_encrypt` which allows to encrypt data with SOPS.
84 - a [role](https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html) `community.sops.install` which allows to install SOPS and GNU Privacy Guard.
85 - two [playbooks](https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html) `community.sops.install` and `community.sops.install_localhost` which allow to install SOPS and GNU Privacy Guard.
86
87 ## Using this collection
88
89 ### Installing SOPS
90
91 To install SOPS, you can use the `community.sops.install` role. The role also installs [GNU Privacy Guard (GPG)](https://en.wikipedia.org/wiki/GNU_Privacy_Guard).
92
93 Examples:
94
95 ```yaml
96 tasks:
97 # To use the sops_encrypt module on a remote host, you need to install SOPS on it:
98 - name: Install SOPS on remote hosts
99 ansible.builtin.include_role:
100 name: community.sops.install
101 vars:
102 sops_version: 2.7.0 # per default installs the latest version
103
104 # To use the lookup plugin, filter plugin, vars plugin, or the load_vars action,
105 # you need SOPS installed on localhost:
106 - name: Install SOPS on localhost
107 ansible.builtin.include_role:
108 name: community.sops.install
109 vars:
110 sops_install_on_localhost: true
111 ```
112
113 ### lookup plugin
114
115 The lookup plugin can be accessed with the `community.sops.sops` key.
116
117 Examples:
118
119 ```yaml
120 tasks:
121 - name: Output secrets to screen (BAD IDEA!)
122 ansible.builtin.debug:
123 msg: "Content: {{ lookup('community.sops.sops', '/path/to/sops-encrypted-file.enc.yaml') }}"
124
125 - name: Add SSH private key
126 ansible.builtin.copy:
127 content: "{{ lookup('community.sops.sops', user + '-id_rsa') }}"
128 dest: /home/{{ user }}/.ssh/id_rsa
129 owner: "{{ user }}"
130 group: "{{ user }}"
131 mode: 0600
132 no_log: true # avoid content to be written to log
133 ```
134
135 See [Lookup Plugins](https://docs.ansible.com/ansible/latest/plugins/lookup.html) for more details on lookup plugins.
136
137 ### filter plugin
138
139 The filter plugin can be used in Jinja2 expressions by the name `community.sops.decrypt`. It can decrypt SOPS-encrypted data coming from other sources than files.
140
141 Example:
142
143 ```yaml
144 tasks:
145 - name: Load SOPS encrypted data
146 ansible.builtin.set_fact:
147 encrypted_data: "{{ lookup('file', '/path/to/sops-encrypted-file.enc.yaml') }}"
148
149 - name: Output secrets to screen (BAD IDEA!)
150 ansible.builtin.debug:
151 msg: "Content: {{ encrypted_data | community.sops.decrypt(output_type='yaml') }}"
152 ```
153
154 See [Filter Plugins](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html) for more details on filters.
155
156 Please note that if you put a Jinja2 expression in a variable, it will be evaluated **every time it is used**. Decrypting data takes a certain amount of time. If you need to use an expression multiple times, it is better to store its evaluated form as a fact with `ansible.bulitin.set_fact` first:
157
158 ```yaml
159 tasks:
160 - name: Decrypt data once
161 ansible.builtin.set_fact:
162 decrypted_data: "{{ encrypted_data | community.sops.decrypt }}"
163 run_once: true # if encrypted_data is identical on all hosts
164
165 - name: Use decrypted secrets multiple times
166 ansible.builtin.openssl_privatekey:
167 path: "/path/to/private_{{ item }}.pem"
168 passphrase: "{{ decrypted_data }}"
169 cipher: auto
170 loop:
171 - foo
172 - bar
173 - baz
174 ```
175
176 By using `{{ encrypted_data | community.sops.decrypt }}` instead of `{{ decrypted_data }}` in the `openssl_privatekey` task, the data would be decrypted three times for every host this is executed for. With the `ansible.builtin.set_fact` and `run_once: true`, it is evaluated only once.
177
178 ### vars plugin
179
180 Vars plugins only work in ansible >= 2.10 and require explicit enabling. One
181 way to enable the plugin is by adding the following to the `defaults` section of
182 your `ansible.cfg`:
183
184 ```ini
185 vars_plugins_enabled = host_group_vars,community.sops.sops
186 ```
187
188 See [VARIABLE_PLUGINS_ENABLED](https://docs.ansible.com/ansible/devel/reference_appendices/config.html#variable-plugins-enabled) for more details.
189
190 After the plugin is enabled, correctly named group and host vars files will be
191 transparently decrypted with SOPS.
192
193 The files must end with one of these extensions:
194
195 * `.sops.yaml`
196 * `.sops.yml`
197 * `.sops.json`
198
199 (These extensions can be adjusted, check out the vars plugin's options for details.) Here is an example file structure:
200
201 ```
202 ├── inventory/
203 │ ├── group_vars/
204 │ │ └── all.sops.yml
205 │ ├── host_vars/
206 │ │ ├── server1.sops.yml
207 │ │ └── server2/
208 │ │ └── data.sops.yml
209 │ └── hosts
210 ├── playbooks/
211 │ └── setup-server.yml
212 └── ansible.cfg
213 ```
214
215 You could execute the playbook in this example with the following command. The
216 SOPS-encrypted vars files would be decrypted and used.
217
218 ```console
219 $ ansible-playbook playbooks/setup-server.yml -i inventory/hosts
220 ```
221
222 #### Determine when to load variables
223
224 Ansible 2.10 allows to determine [when vars plugins load the data](https://docs.ansible.com/ansible/latest/plugins/vars.html#using-vars-plugins).
225
226 To run the SOPS vars plugin right after importing inventory, you can add the following to `ansible.cfg`:
227
228 ```ini
229 [community.sops]
230 vars_stage = inventory
231 ```
232
233 #### Caching variable files
234
235 By default, the vars plugin caches decrypted files to avoid having to decrypt them every task. If this is not wanted, it can be explicitly disabled in `ansible.cfg`:
236
237 ```ini
238 [community.sops]
239 vars_cache = false
240 ```
241
242 Please note that when using vars plugin staging, this setting only has effect if the variables are not only loaded during the `inventory` stage. See the documentation of the `community.sops.sops` vars plugin for more details.
243
244 ### load_vars action plugin
245
246 The `load_vars` action plugin can be used similarly to Ansible's `include_vars`, except that it right now only supports single files. Also, it does not allow to load proper variables (i.e. "unsafe" Jinja2 expressions which evaluate on usage), but only facts. This is a restriction imposed by ansible-core. The plugin does allow to evaluate expressions on load-time, though.
247
248 Examples:
249
250 ```yaml
251 tasks:
252 - name: Load variables from file and store them in a variable
253 community.sops.load_vars:
254 file: path/to/sops-encrypted-file.sops.yaml
255 name: variable_to_store_contents_in
256
257 - name: Load variables from file into global namespace, and evaluate Jinja2 expressions
258 community.sops.load_vars:
259 file: path/to/sops-encrypted-file-with-jinja2-expressions.sops.yaml
260 # The following allows to use Jinja2 expressions in the encrypted file!
261 # They are evaluated right now, i.e. not later like when loaded with include_vars.
262 expressions: evaluate-on-load
263 ```
264
265 ### sops_encrypt module
266
267 The `sops_encrypt` module can be used to create and update SOPS-encrypted files. It assumes that SOPS is configured via environment variables or a `.sops.yaml` file.
268
269 Examples:
270
271 ```yaml
272 tasks:
273 - name: Store secret text SOPS-encrypted
274 community.sops.sops_encrypt:
275 path: path/to/sops-encrypted-file.sops
276 content_text: This is some secret text.
277
278 - name: Store secret binary data SOPS-encrypted
279 community.sops.sops_encrypt:
280 path: path/to/sops-encrypted-file.sops
281 content_binary: "{{ some_secret_binary_data | b64encode }}"
282
283 - name: Store secret JSON data
284 community.sops.sops_encrypt:
285 path: path/to/sops-encrypted-file.sops.json
286 content_json:
287 key1: value1
288 key2:
289 - value2
290 - key3: value3
291 key4: value5
292
293 - name: Store secret YAML data
294 community.sops.sops_encrypt:
295 path: path/to/sops-encrypted-file.sops.yaml
296 content_yaml:
297 key1: value1
298 key2:
299 - value2
300 - key3: value3
301 key4: value5
302 ```
303
304 ## Troubleshooting
305
306 ### Spurious failures during encryption and decryption with gpg
307
308 Older versions of SOPS calls `gpg` with `--use-agent`. When running multiple of these in parallel, for example when loading variables or looking up files for various hosts at once, some of these can randomly fail with messages such as
309 ```
310 Failed to get the data key required to decrypt the SOPS file.
311
312 Group 0: FAILED
313 D13xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx: FAILED
314 - | could not decrypt data key with PGP key:
315 | golang.org/x/crypto/openpgp error: Reading PGP message
316 | failed: openpgp: incorrect key; GPG binary error: exit
317 | status 2
318
319 828xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx: FAILED
320 - | could not decrypt data key with PGP key:
321 | golang.org/x/crypto/openpgp error: Reading PGP message
322 | failed: openpgp: incorrect key; GPG binary error: exit
323 | status 2
324
325 Recovery failed because no master key was able to decrypt the file. In
326 order for SOPS to recover the file, at least one key has to be successful,
327 but none were.
328 ```
329 This is a limitation of gpg-agent which can be fixed by adding `auto-expand-secmem` to `~/.gnupg/gpg-agent.conf` ([reference on option](https://www.gnupg.org/documentation/manuals/gnupg/Agent-Options.html#index-ssh_002dfingerprint_002ddigest), [reference on config file](https://www.gnupg.org/documentation/manuals/gnupg/Agent-Configuration.html)).
330
331 (See https://github.com/ansible-collections/community.sops/issues/34 and https://dev.gnupg.org/T4146 for more details.)
332
333 ## Contributing to this collection
334
335 <!--Describe how the community can contribute to your collection. At a minimum, include how and where users can create issues to report problems or request features for this collection. List contribution requirements, including preferred workflows and necessary testing, so you can benefit from community PRs. If you are following general Ansible contributor guidelines, you can link to - [Ansible Community Guide](https://docs.ansible.com/ansible/latest/community/index.html). -->
336
337 See [CONTRIBUTING.md](https://github.com/ansible-collections/community.sops/blob/main/CONTRIBUTING.md)
338
339 ## Release notes
340
341 See [CHANGELOG.md](https://github.com/ansible-collections/community.sops/blob/main/CHANGELOG.md).
342
343 ## Releasing, Versioning and Deprecation
344
345 This collection follows [Semantic Versioning](https://semver.org/). More details on versioning can be found [in the Ansible docs](https://docs.ansible.com/ansible/latest/dev_guide/developing_collections.html#collection-versions).
346
347 We plan to regularly release new minor or bugfix versions once new features or bugfixes have been implemented.
348
349 Releasing the current major version happens from the `main` branch. We will create a `stable-1` branch for 1.x.y versions once we start working on a 2.0.0 release, to allow backporting bugfixes and features from the 2.0.0 branch (`main`) to `stable-1`. A `stable-2` branch will be created once we work on a 3.0.0 release, and so on.
350
351 We currently are not planning any deprecations or new major releases like 2.0.0 containing backwards incompatible changes. If backwards incompatible changes are needed, we plan to deprecate the old behavior as early as possible. We also plan to backport at least bugfixes for the old major version for some time after releasing a new major version. We will not block community members from backporting other bugfixes and features from the latest stable version to older release branches, under the condition that these backports are of reasonable quality.
352
353 ## More information
354
355 <!-- List out where the user can find additional information, such as working group meeting times, slack/IRC channels, or documentation for the product this collection automates. At a minimum, link to: -->
356
357 - [Ansible Collection overview](https://github.com/ansible-collections/overview)
358 - [Ansible User guide](https://docs.ansible.com/ansible/latest/user_guide/index.html)
359 - [Ansible Developer guide](https://docs.ansible.com/ansible/latest/dev_guide/index.html)
360
361 ## Licensing
362
363 This collection is primarily licensed and distributed as a whole under the GNU General Public License v3.0 or later.
364
365 See [LICENSES/GPL-3.0-or-later.txt](https://github.com/ansible-collections/community.sops/blob/main/COPYING) for the full text.
366
367 Parts of the collection are licensed under the [BSD 2-Clause license](https://github.com/ansible-collections/community.sops/blob/main/LICENSES/BSD-2-Clause.txt).
368
369 All files have a machine readable `SDPX-License-Identifier:` comment denoting its respective license(s) or an equivalent entry in an accompanying `.license` file. Only changelog fragments (which will not be part of a release) are covered by a blanket statement in `REUSE.toml`. This conforms to the [REUSE specification](https://reuse.software/spec/).