2022-10-09 12:54:11 +00:00
|
|
|
|
- hosts: demo
|
2022-10-10 05:56:42 +00:00
|
|
|
|
gather_facts: no
|
2022-10-09 12:54:11 +00:00
|
|
|
|
tasks:
|
2023-07-28 09:00:55 +00:00
|
|
|
|
- name: "Test privileged {{ jms_account.username }} account"
|
2022-10-14 08:33:24 +00:00
|
|
|
|
ansible.builtin.ping:
|
2022-10-09 12:54:11 +00:00
|
|
|
|
|
2023-07-28 09:00:55 +00:00
|
|
|
|
- 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"
|
2023-07-04 09:34:31 +00:00
|
|
|
|
ansible.builtin.user:
|
|
|
|
|
name: "{{ account.username }}"
|
|
|
|
|
shell: "{{ params.shell }}"
|
|
|
|
|
home: "{{ params.home | default('/home/' + account.username, true) }}"
|
|
|
|
|
groups: "{{ params.groups }}"
|
|
|
|
|
expires: -1
|
|
|
|
|
state: present
|
2023-07-28 09:00:55 +00:00
|
|
|
|
when: user_info.failed
|
2023-07-04 09:34:31 +00:00
|
|
|
|
|
|
|
|
|
- name: "Add {{ account.username }} group"
|
|
|
|
|
ansible.builtin.group:
|
|
|
|
|
name: "{{ account.username }}"
|
|
|
|
|
state: present
|
2023-07-28 09:00:55 +00:00
|
|
|
|
when: user_info.failed
|
2023-07-04 09:34:31 +00:00
|
|
|
|
|
2023-07-28 09:00:55 +00:00
|
|
|
|
- name: "Add {{ account.username }} user to group"
|
2023-07-04 09:34:31 +00:00
|
|
|
|
ansible.builtin.user:
|
|
|
|
|
name: "{{ account.username }}"
|
|
|
|
|
groups: "{{ params.groups }}"
|
2023-07-28 09:00:55 +00:00
|
|
|
|
when:
|
|
|
|
|
- user_info.failed
|
|
|
|
|
- params.groups
|
2023-07-04 09:34:31 +00:00
|
|
|
|
|
2023-07-28 09:00:55 +00:00
|
|
|
|
- name: "Change {{ account.username }} password"
|
2022-10-14 08:33:24 +00:00
|
|
|
|
ansible.builtin.user:
|
2022-10-09 12:54:11 +00:00
|
|
|
|
name: "{{ account.username }}"
|
2022-10-13 09:47:29 +00:00
|
|
|
|
password: "{{ account.secret | password_hash('sha512') }}"
|
2022-10-09 12:54:11 +00:00
|
|
|
|
update_password: always
|
2023-05-19 08:09:32 +00:00
|
|
|
|
ignore_errors: true
|
2023-03-08 10:52:00 +00:00
|
|
|
|
when: account.secret_type == "password"
|
2022-10-09 12:54:11 +00:00
|
|
|
|
|
2022-10-20 12:34:15 +00:00
|
|
|
|
- name: remove jumpserver ssh key
|
|
|
|
|
ansible.builtin.lineinfile:
|
2023-04-13 11:02:04 +00:00
|
|
|
|
dest: "{{ ssh_params.dest }}"
|
|
|
|
|
regexp: "{{ ssh_params.regexp }}"
|
2022-10-20 12:34:15 +00:00
|
|
|
|
state: absent
|
2022-10-27 10:53:10 +00:00
|
|
|
|
when:
|
2023-03-08 10:52:00 +00:00
|
|
|
|
- account.secret_type == "ssh_key"
|
2023-04-13 11:02:04 +00:00
|
|
|
|
- ssh_params.strategy == "set_jms"
|
2022-10-20 12:34:15 +00:00
|
|
|
|
|
2023-07-28 09:00:55 +00:00
|
|
|
|
- name: "Change {{ account.username }} SSH key"
|
2022-10-14 08:33:24 +00:00
|
|
|
|
ansible.builtin.authorized_key:
|
2022-10-09 12:54:11 +00:00
|
|
|
|
user: "{{ account.username }}"
|
2022-10-20 12:34:15 +00:00
|
|
|
|
key: "{{ account.secret }}"
|
2023-04-13 11:02:04 +00:00
|
|
|
|
exclusive: "{{ ssh_params.exclusive }}"
|
2023-03-08 10:52:00 +00:00
|
|
|
|
when: account.secret_type == "ssh_key"
|
2022-10-14 08:33:24 +00:00
|
|
|
|
|
2023-07-28 09:00:55 +00:00
|
|
|
|
- name: "Set {{ account.username }} sudo setting"
|
2023-07-04 09:34:31 +00:00
|
|
|
|
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:
|
2023-07-28 09:00:55 +00:00
|
|
|
|
- user_info.failed
|
2023-07-04 09:34:31 +00:00
|
|
|
|
- params.sudo
|
|
|
|
|
|
2022-10-14 08:33:24 +00:00
|
|
|
|
- name: Refresh connection
|
|
|
|
|
ansible.builtin.meta: reset_connection
|
2022-10-09 12:54:11 +00:00
|
|
|
|
|
2023-07-28 09:00:55 +00:00
|
|
|
|
- name: "Verify {{ account.username }} password"
|
2022-10-14 08:33:24 +00:00
|
|
|
|
ansible.builtin.ping:
|
|
|
|
|
become: no
|
2022-10-09 12:54:11 +00:00
|
|
|
|
vars:
|
|
|
|
|
ansible_user: "{{ account.username }}"
|
2022-10-14 08:33:24 +00:00
|
|
|
|
ansible_password: "{{ account.secret }}"
|
|
|
|
|
ansible_become: no
|
2023-03-08 10:52:00 +00:00
|
|
|
|
when: account.secret_type == "password"
|
2022-10-20 12:34:15 +00:00
|
|
|
|
|
2023-07-28 09:00:55 +00:00
|
|
|
|
- name: "Verify {{ account.username }} SSH key"
|
2022-10-20 12:34:15 +00:00
|
|
|
|
ansible.builtin.ping:
|
|
|
|
|
become: no
|
|
|
|
|
vars:
|
|
|
|
|
ansible_user: "{{ account.username }}"
|
|
|
|
|
ansible_ssh_private_key_file: "{{ account.private_key_path }}"
|
|
|
|
|
ansible_become: no
|
2023-03-08 10:52:00 +00:00
|
|
|
|
when: account.secret_type == "ssh_key"
|