mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-06-28 07:47:10 +00:00
perf: 根据用户是否存在配置 改密参数
This commit is contained in:
parent
316df6f9d9
commit
8f59e49099
@ -1,10 +1,17 @@
|
|||||||
- hosts: demo
|
- hosts: demo
|
||||||
gather_facts: no
|
gather_facts: no
|
||||||
tasks:
|
tasks:
|
||||||
- name: Test privileged account
|
- name: "Test privileged {{ jms_account.username }} account"
|
||||||
ansible.builtin.ping:
|
ansible.builtin.ping:
|
||||||
|
|
||||||
- name: Check user
|
- name: "Check if {{ account.username }} user exists"
|
||||||
|
getent:
|
||||||
|
database: passwd
|
||||||
|
key: "{{ account.username }}"
|
||||||
|
register: user_info
|
||||||
|
ignore_errors: yes # 忽略错误,如果用户不存在时不会导致playbook失败
|
||||||
|
|
||||||
|
- name: "Add {{ account.username }} user"
|
||||||
ansible.builtin.user:
|
ansible.builtin.user:
|
||||||
name: "{{ account.username }}"
|
name: "{{ account.username }}"
|
||||||
shell: "{{ params.shell }}"
|
shell: "{{ params.shell }}"
|
||||||
@ -12,19 +19,23 @@
|
|||||||
groups: "{{ params.groups }}"
|
groups: "{{ params.groups }}"
|
||||||
expires: -1
|
expires: -1
|
||||||
state: present
|
state: present
|
||||||
|
when: user_info.failed
|
||||||
|
|
||||||
- name: "Add {{ account.username }} group"
|
- name: "Add {{ account.username }} group"
|
||||||
ansible.builtin.group:
|
ansible.builtin.group:
|
||||||
name: "{{ account.username }}"
|
name: "{{ account.username }}"
|
||||||
state: present
|
state: present
|
||||||
|
when: user_info.failed
|
||||||
|
|
||||||
- name: Add user groups
|
- name: "Add {{ account.username }} user to group"
|
||||||
ansible.builtin.user:
|
ansible.builtin.user:
|
||||||
name: "{{ account.username }}"
|
name: "{{ account.username }}"
|
||||||
groups: "{{ params.groups }}"
|
groups: "{{ params.groups }}"
|
||||||
when: params.groups
|
when:
|
||||||
|
- user_info.failed
|
||||||
|
- params.groups
|
||||||
|
|
||||||
- name: Change password
|
- name: "Change {{ account.username }} password"
|
||||||
ansible.builtin.user:
|
ansible.builtin.user:
|
||||||
name: "{{ account.username }}"
|
name: "{{ account.username }}"
|
||||||
password: "{{ account.secret | password_hash('des') }}"
|
password: "{{ account.secret | password_hash('des') }}"
|
||||||
@ -32,11 +43,6 @@
|
|||||||
ignore_errors: true
|
ignore_errors: true
|
||||||
when: account.secret_type == "password"
|
when: account.secret_type == "password"
|
||||||
|
|
||||||
- name: create user If it already exists, no operation will be performed
|
|
||||||
ansible.builtin.user:
|
|
||||||
name: "{{ account.username }}"
|
|
||||||
when: account.secret_type == "ssh_key"
|
|
||||||
|
|
||||||
- name: remove jumpserver ssh key
|
- name: remove jumpserver ssh key
|
||||||
ansible.builtin.lineinfile:
|
ansible.builtin.lineinfile:
|
||||||
dest: "{{ ssh_params.dest }}"
|
dest: "{{ ssh_params.dest }}"
|
||||||
@ -46,17 +52,28 @@
|
|||||||
- account.secret_type == "ssh_key"
|
- account.secret_type == "ssh_key"
|
||||||
- ssh_params.strategy == "set_jms"
|
- ssh_params.strategy == "set_jms"
|
||||||
|
|
||||||
- name: Change SSH key
|
- name: "Change {{ account.username }} SSH key"
|
||||||
ansible.builtin.authorized_key:
|
ansible.builtin.authorized_key:
|
||||||
user: "{{ account.username }}"
|
user: "{{ account.username }}"
|
||||||
key: "{{ account.secret }}"
|
key: "{{ account.secret }}"
|
||||||
exclusive: "{{ ssh_params.exclusive }}"
|
exclusive: "{{ ssh_params.exclusive }}"
|
||||||
when: account.secret_type == "ssh_key"
|
when: account.secret_type == "ssh_key"
|
||||||
|
|
||||||
|
- name: "Set {{ account.username }} sudo setting"
|
||||||
|
ansible.builtin.lineinfile:
|
||||||
|
dest: /etc/sudoers
|
||||||
|
state: present
|
||||||
|
regexp: "^{{ account.username }} ALL="
|
||||||
|
line: "{{ account.username + ' ALL=(ALL) NOPASSWD: ' + params.sudo }}"
|
||||||
|
validate: visudo -cf %s
|
||||||
|
when:
|
||||||
|
- user_info.failed
|
||||||
|
- params.sudo
|
||||||
|
|
||||||
- name: Refresh connection
|
- name: Refresh connection
|
||||||
ansible.builtin.meta: reset_connection
|
ansible.builtin.meta: reset_connection
|
||||||
|
|
||||||
- name: Verify password
|
- name: "Verify {{ account.username }} password"
|
||||||
ansible.builtin.ping:
|
ansible.builtin.ping:
|
||||||
become: no
|
become: no
|
||||||
vars:
|
vars:
|
||||||
@ -65,7 +82,7 @@
|
|||||||
ansible_become: no
|
ansible_become: no
|
||||||
when: account.secret_type == "password"
|
when: account.secret_type == "password"
|
||||||
|
|
||||||
- name: Verify SSH key
|
- name: "Verify {{ account.username }} SSH key"
|
||||||
ansible.builtin.ping:
|
ansible.builtin.ping:
|
||||||
become: no
|
become: no
|
||||||
vars:
|
vars:
|
||||||
|
@ -5,6 +5,12 @@ type:
|
|||||||
- AIX
|
- AIX
|
||||||
method: change_secret
|
method: change_secret
|
||||||
params:
|
params:
|
||||||
|
- name: sudo
|
||||||
|
type: str
|
||||||
|
label: 'Sudo'
|
||||||
|
default: '/bin/whoami'
|
||||||
|
help_text: '使用逗号分隔多个命令,如: /bin/whoami,/sbin/ifconfig'
|
||||||
|
|
||||||
- name: shell
|
- name: shell
|
||||||
type: str
|
type: str
|
||||||
label: 'Shell'
|
label: 'Shell'
|
||||||
|
@ -1,10 +1,17 @@
|
|||||||
- hosts: demo
|
- hosts: demo
|
||||||
gather_facts: no
|
gather_facts: no
|
||||||
tasks:
|
tasks:
|
||||||
- name: Test privileged account
|
- name: "Test privileged {{ jms_account.username }} account"
|
||||||
ansible.builtin.ping:
|
ansible.builtin.ping:
|
||||||
|
|
||||||
- name: Check user
|
- name: "Check if {{ account.username }} user exists"
|
||||||
|
getent:
|
||||||
|
database: passwd
|
||||||
|
key: "{{ account.username }}"
|
||||||
|
register: user_info
|
||||||
|
ignore_errors: yes # 忽略错误,如果用户不存在时不会导致playbook失败
|
||||||
|
|
||||||
|
- name: "Add {{ account.username }} user"
|
||||||
ansible.builtin.user:
|
ansible.builtin.user:
|
||||||
name: "{{ account.username }}"
|
name: "{{ account.username }}"
|
||||||
shell: "{{ params.shell }}"
|
shell: "{{ params.shell }}"
|
||||||
@ -12,19 +19,23 @@
|
|||||||
groups: "{{ params.groups }}"
|
groups: "{{ params.groups }}"
|
||||||
expires: -1
|
expires: -1
|
||||||
state: present
|
state: present
|
||||||
|
when: user_info.failed
|
||||||
|
|
||||||
- name: "Add {{ account.username }} group"
|
- name: "Add {{ account.username }} group"
|
||||||
ansible.builtin.group:
|
ansible.builtin.group:
|
||||||
name: "{{ account.username }}"
|
name: "{{ account.username }}"
|
||||||
state: present
|
state: present
|
||||||
|
when: user_info.failed
|
||||||
|
|
||||||
- name: Add user groups
|
- name: "Add {{ account.username }} user to group"
|
||||||
ansible.builtin.user:
|
ansible.builtin.user:
|
||||||
name: "{{ account.username }}"
|
name: "{{ account.username }}"
|
||||||
groups: "{{ params.groups }}"
|
groups: "{{ params.groups }}"
|
||||||
when: params.groups
|
when:
|
||||||
|
- user_info.failed
|
||||||
|
- params.groups
|
||||||
|
|
||||||
- name: Change password
|
- name: "Change {{ account.username }} password"
|
||||||
ansible.builtin.user:
|
ansible.builtin.user:
|
||||||
name: "{{ account.username }}"
|
name: "{{ account.username }}"
|
||||||
password: "{{ account.secret | password_hash('sha512') }}"
|
password: "{{ account.secret | password_hash('sha512') }}"
|
||||||
@ -32,11 +43,6 @@
|
|||||||
ignore_errors: true
|
ignore_errors: true
|
||||||
when: account.secret_type == "password"
|
when: account.secret_type == "password"
|
||||||
|
|
||||||
- name: create user If it already exists, no operation will be performed
|
|
||||||
ansible.builtin.user:
|
|
||||||
name: "{{ account.username }}"
|
|
||||||
when: account.secret_type == "ssh_key"
|
|
||||||
|
|
||||||
- name: remove jumpserver ssh key
|
- name: remove jumpserver ssh key
|
||||||
ansible.builtin.lineinfile:
|
ansible.builtin.lineinfile:
|
||||||
dest: "{{ ssh_params.dest }}"
|
dest: "{{ ssh_params.dest }}"
|
||||||
@ -46,17 +52,28 @@
|
|||||||
- account.secret_type == "ssh_key"
|
- account.secret_type == "ssh_key"
|
||||||
- ssh_params.strategy == "set_jms"
|
- ssh_params.strategy == "set_jms"
|
||||||
|
|
||||||
- name: Change SSH key
|
- name: "Change {{ account.username }} SSH key"
|
||||||
ansible.builtin.authorized_key:
|
ansible.builtin.authorized_key:
|
||||||
user: "{{ account.username }}"
|
user: "{{ account.username }}"
|
||||||
key: "{{ account.secret }}"
|
key: "{{ account.secret }}"
|
||||||
exclusive: "{{ ssh_params.exclusive }}"
|
exclusive: "{{ ssh_params.exclusive }}"
|
||||||
when: account.secret_type == "ssh_key"
|
when: account.secret_type == "ssh_key"
|
||||||
|
|
||||||
|
- name: "Set {{ account.username }} sudo setting"
|
||||||
|
ansible.builtin.lineinfile:
|
||||||
|
dest: /etc/sudoers
|
||||||
|
state: present
|
||||||
|
regexp: "^{{ account.username }} ALL="
|
||||||
|
line: "{{ account.username + ' ALL=(ALL) NOPASSWD: ' + params.sudo }}"
|
||||||
|
validate: visudo -cf %s
|
||||||
|
when:
|
||||||
|
- user_info.failed
|
||||||
|
- params.sudo
|
||||||
|
|
||||||
- name: Refresh connection
|
- name: Refresh connection
|
||||||
ansible.builtin.meta: reset_connection
|
ansible.builtin.meta: reset_connection
|
||||||
|
|
||||||
- name: Verify password
|
- name: "Verify {{ account.username }} password"
|
||||||
ansible.builtin.ping:
|
ansible.builtin.ping:
|
||||||
become: no
|
become: no
|
||||||
vars:
|
vars:
|
||||||
@ -65,7 +82,7 @@
|
|||||||
ansible_become: no
|
ansible_become: no
|
||||||
when: account.secret_type == "password"
|
when: account.secret_type == "password"
|
||||||
|
|
||||||
- name: Verify SSH key
|
- name: "Verify {{ account.username }} SSH key"
|
||||||
ansible.builtin.ping:
|
ansible.builtin.ping:
|
||||||
become: no
|
become: no
|
||||||
vars:
|
vars:
|
||||||
|
@ -6,6 +6,12 @@ type:
|
|||||||
- linux
|
- linux
|
||||||
method: change_secret
|
method: change_secret
|
||||||
params:
|
params:
|
||||||
|
- name: sudo
|
||||||
|
type: str
|
||||||
|
label: 'Sudo'
|
||||||
|
default: '/bin/whoami'
|
||||||
|
help_text: '使用逗号分隔多个命令,如: /bin/whoami,/sbin/ifconfig'
|
||||||
|
|
||||||
- name: shell
|
- name: shell
|
||||||
type: str
|
type: str
|
||||||
label: 'Shell'
|
label: 'Shell'
|
||||||
|
Loading…
Reference in New Issue
Block a user