main.yml 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # block 1
  2. - block:
  3. - name: set bin facts
  4. set_fact:
  5. sudo_nopasswd_bin_lists:
  6. - /home/{{ansible_user_id}}/.local/bin/ansible
  7. - /home/{{ansible_user_id}}/.local/bin/ansible-playbook
  8. - /opt/yunion/bin/ocadm
  9. - /usr/bin/chown
  10. - /usr/bin/cp
  11. - /usr/bin/docker
  12. - /usr/bin/kubeadm
  13. - /usr/bin/kubectl
  14. - /usr/bin/kubelet
  15. - /usr/bin/mkdir
  16. - /usr/bin/mount
  17. - /usr/bin/mv
  18. - /usr/bin/python
  19. - /usr/bin/python3
  20. - /usr/bin/sed
  21. - /usr/bin/sh
  22. - /usr/bin/systemctl
  23. - /usr/bin/umount
  24. - /usr/sbin/dmidecode
  25. - /usr/sbin/iptables
  26. - /usr/sbin/iptables-save
  27. - /usr/sbin/modprobe
  28. - /usr/sbin/service
  29. - /usr/sbin/update-alternatives
  30. - name: Add executable commands to my_list
  31. set_fact:
  32. sudo_nopasswd_bin_lists: "{{ sudo_nopasswd_bin_lists + [item] }}"
  33. loop:
  34. - /usr/bin/yum
  35. - /usr/bin/dnf
  36. - /usr/bin/apt
  37. when:
  38. - lookup('file', item,errors='ignore') | default(None) is not none
  39. - name: Test NOPASSWD ALL privilege
  40. shell: |
  41. sudo -lU "{{ansible_user_id}}" | grep -w NOPASSWD| sed -e 's#.*NOPASSWD##' | grep -iwq ALL
  42. register: with_all_privilege
  43. ignore_errors: true
  44. failed_when: false
  45. # block 2
  46. - block:
  47. - name: Test each file in sudo_nopasswd_bin_lists has been granted with NOPASSWD privilege.
  48. shell: |
  49. sudo -lU "{{ansible_user_id}}" | grep -w NOPASSWD| sed -e 's#.*NOPASSWD##' | grep -wo "{{item}}"
  50. loop: "{{sudo_nopasswd_bin_lists | list}}"
  51. register: show
  52. ignore_errors: true
  53. # block 3
  54. - block:
  55. - name: Privilege test result
  56. debug:
  57. msg: "following commands should be in user {{ansible_user_id}}'s sudoer file: {{ show.results | selectattr('rc', 'ne', 0) | map(attribute='item') | list | join(',') }}"
  58. - shell: exit 1
  59. # when3
  60. when: show.results | selectattr('rc', 'ne', 0) | map(attribute='item') | list | length > 0
  61. # when2
  62. when: with_all_privilege.rc != 0
  63. # when 1
  64. when: ansible_user_id != 'root'
  65. tags:
  66. - sudoer