Debian Codegraph @

test/integration/targets/become_sudo/tasks/main.yml

raw
1 - name: create unprivileged become user pair
2 include_role:
3 name: setup_become_user_pair
4 public: true
5
6 - name: capture config values
7 set_fact:
8 # this needs to be looked up and stored before setting ansible_become_flags
9 default_become_flags: "{{ lookup('config', 'become_flags', plugin_type='become', plugin_name='sudo') }}"
10 intermediate_flags: --intermediate-user {{ intermediate_user_name | quote }}
11
12 - name: deploy sudo shim
13 copy:
14 src: sudoshim.sh
15 dest: /tmp/sudoshim.sh
16 mode: a+rx
17
18 - name: apply shared become vars to all tasks that use the sudo test shim
19 vars:
20 ansible_become: yes
21 ansible_become_method: sudo
22 ansible_become_user: '{{ target_user_name }}'
23 ansible_become_password: '{{ intermediate_user_password }}'
24 ansible_become_exe: /tmp/sudoshim.sh
25 ansible_become_flags: '{{ default_become_flags }} {{ intermediate_flags }}'
26 ansible_local_become_strip_preamble: true
27 block:
28 - name: basic success check
29 raw: whoami
30 register: success
31 # NOTE: The ssh connection plugin does not properly strip noise from raw stdout, unlike the local connection plugin.
32 # Once that is fixed, this can be changed to a comparison against stdout, not stdout_lines[-1].
33 failed_when: success.stdout_lines[-1] != target_user_name
34
35 - name: validate that a password prompt is being used and that the shim is invalidating the sudo timestamp
36 vars:
37 ansible_become_password: BOGUSPASS
38 raw: exit 99
39 ignore_errors: true
40 register: bogus_password
41
42 - assert:
43 that:
44 - bogus_password.msg is contains "Incorrect sudo password" or bogus_password.msg is contains "Duplicate become password prompt encountered"
45
46 - name: request sudo chdir to a nonexistent root dir; expected failure
47 raw: echo himom
48 vars:
49 ansible_sudo_chdir: /nonexistent_dir
50 ignore_errors: true
51 register: nonexistent_chdir
52
53 - assert:
54 that:
55 - nonexistent_chdir is failed
56 # deal with inconsistent failure behavior across different connection plugins
57 - (nonexistent_chdir.msg ~ (nonexistent_chdir.stdout | default('')) ~ (nonexistent_chdir.stderr | default(''))) is search "cd.*/nonexistent_dir"
58
59 - name: request sudo chdir to /; cwd should successfully be / before sudo runs
60 raw: echo "CWD IS <$(pwd)>"
61 vars:
62 ansible_sudo_chdir: /
63 register: chdir_root
64
65 - assert:
66 that:
67 - chdir_root.stdout is contains 'CWD IS </>'
68
69 - name: become with custom sudo `--` flags (similar to defaults)
70 vars:
71 ansible_become_flags: --set-home --stdin --non-interactive {{ intermediate_flags }}
72 raw: whoami
73 register: custom_flags
74
75 - name: become with no user
76 vars:
77 ansible_become_user: ""
78 raw: whoami
79 register: no_user
80
81 - assert:
82 that:
83 - custom_flags.stdout.strip() == test_user_name
84 - no_user.stdout.strip() == "root"