Skip to content

Commit

Permalink
[PR #5883/dcc3d4f5 backport][stable-6] Add support for setenv paramet…
Browse files Browse the repository at this point in the history
…ers (#5920)

Add support for setenv parameters (#5883)

(cherry picked from commit dcc3d4f)

Co-authored-by: Renaud <33203203+redat00@users.noreply.github.com>
  • Loading branch information
patchback[bot] and redat00 authored Jan 30, 2023
1 parent 48bfba4 commit e5930aa
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- sudoers - add ``setenv`` parameters to support passing environment variables via sudo. (https://github.com/ansible-collections/community.general/pull/5883)
22 changes: 21 additions & 1 deletion plugins/modules/sudoers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
- Whether a password will be required to run the sudo'd command.
default: true
type: bool
setenv:
description:
- Whether to allow keeping the environment when command is run with sudo.
default: false
type: bool
version_added: 6.3.0
host:
description:
- Specify the host the rule is for.
Expand Down Expand Up @@ -123,6 +129,13 @@
community.general.sudoers:
name: alice-service
state: absent
- name: Allow alice to sudo /usr/local/bin/upload and keep env variables
community.general.sudoers:
name: allow-alice-upload
user: alice
commands: /usr/local/bin/upload
setenv: true
'''

import os
Expand All @@ -143,6 +156,7 @@ def __init__(self, module):
self.group = module.params['group']
self.state = module.params['state']
self.nopassword = module.params['nopassword']
self.setenv = module.params['setenv']
self.host = module.params['host']
self.runas = module.params['runas']
self.sudoers_path = module.params['sudoers_path']
Expand Down Expand Up @@ -185,12 +199,14 @@ def content(self):

commands_str = ', '.join(self.commands)
nopasswd_str = 'NOPASSWD:' if self.nopassword else ''
setenv_str = 'SETENV:' if self.setenv else ''
runas_str = '({runas})'.format(runas=self.runas) if self.runas is not None else ''
return "{owner} {host}={runas}{nopasswd} {commands}\n".format(
return "{owner} {host}={runas}{nopasswd}{setenv} {commands}\n".format(
owner=owner,
host=self.host,
runas=runas_str,
nopasswd=nopasswd_str,
setenv=setenv_str,
commands=commands_str
)

Expand Down Expand Up @@ -239,6 +255,10 @@ def main():
'type': 'bool',
'default': True,
},
'setenv': {
'type': 'bool',
'default': False,
},
'host': {
'type': 'str',
'default': 'ALL',
Expand Down
16 changes: 15 additions & 1 deletion tests/integration/targets/sudoers/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,20 @@
src: "{{ sudoers_path }}/my-sudo-rule-7"
register: rule_7_contents

- name: Create rule with setenv parameters
community.general.sudoers:
name: my-sudo-rule-8
state: present
user: alice
commands: /usr/local/bin/command
setenv: true
register: rule_8

- name: Grab contents of my-sudo-rule-8
ansible.builtin.slurp:
src: "{{ sudoers_path }}/my-sudo-rule-8"
register: rule_8_contents

- name: Revoke rule 1
community.general.sudoers:
name: my-sudo-rule-1
Expand Down Expand Up @@ -202,7 +216,6 @@
when: ansible_os_family != 'Darwin'
register: edge_case_3


- name: Revoke non-existing rule
community.general.sudoers:
name: non-existing-rule
Expand Down Expand Up @@ -243,6 +256,7 @@
- "rule_5_contents['content'] | b64decode == 'alice ALL=NOPASSWD: /usr/local/bin/command\n'"
- "rule_6_contents['content'] | b64decode == 'alice ALL=(bob)NOPASSWD: /usr/local/bin/command\n'"
- "rule_7_contents['content'] | b64decode == 'alice host-1=NOPASSWD: /usr/local/bin/command\n'"
- "rule_8_contents['content'] | b64decode == 'alice ALL=NOPASSWD:SETENV: /usr/local/bin/command\n'"

- name: Check revocation stat
ansible.builtin.assert:
Expand Down

0 comments on commit e5930aa

Please sign in to comment.