Skip to content

Instantly share code, notes, and snippets.

@msato0731
Last active June 2, 2021 13:24
Show Gist options
  • Save msato0731/4fa53792082ffb5eff1a91f1abeef648 to your computer and use it in GitHub Desktop.
Save msato0731/4fa53792082ffb5eff1a91f1abeef648 to your computer and use it in GitHub Desktop.
---
- hosts: all
become: yes
vars_files:
- var.yaml
tasks:
- name: add a admin group
group: name=admin state=present
- name: add a new user
user:
name: "{{ item.name }}"
uid: "{{ item.uid}}"
password: "{{ item.password | password_hash('sha512') }}"
group: admin
groups: wheel
state: present
with_items:
- "{{ users }}"
- name: mkdir .ssh
file:
path: /home/{{ item.name }}/.ssh/
state: directory
owner: "{{ item.name }}"
group: admin
mode: 0700
with_items:
- "{{ users }}"
- name: add authorized_key
authorized_key:
user: "{{ item.name }}"
key: "{{ lookup('file', '/home/vagrant/ansible/ansible_useradd/publickey/{{ item.name }}/id_rsa.pub') }}"
with_items:
- "{{ users }}"
users:
- { name: 'hoge1', uid: '1011', password: 'password' }
- { name: 'hoge2', uid: '1012', password: 'password' }
- { name: 'hoge3', uid: '1013', password: 'password'}
- { name: 'hoge4', uid: '1014', password: 'password' }
@arossouw
Copy link

arossouw commented Jun 2, 2021

I've added this to my playbook. It will remove users if its a revoked user and the file exists:

- name: Get stat about public key
  stat:
    path: "{{ lookup('file','public_key/{{ item.name }}/id_rsa.pub') }}"
  register: pubkey
  with_items:
   - "{{ users }}"

- name: remove revoked authorized_key
  authorized_key:
    user: "{{ item.name }}"
    key: "{{ lookup('file','public_key/{{ item.name }}/id_rsa.pub') }}"
  with_items:
   - "{{ revoked_users }}"
  when: pubkey.results[0].stat.exists 


- name: remove revoked users
  user:
    name: "{{ item.name }}"
    uid: "{{ item.uid }}"
    state: absent
  with_items:
   - "{{ revoked_users }}"
  when: pubkey.results[0].stat.exists 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment