본문 바로가기

리눅스

[리눅스] ansible user 모듈(user module)

반응형

ansible user 모듈(user module)

불필요한 계정 삭제하기

userDelete.yaml 파일 작성

cat <<EOF > userDelete.yaml
---
- hosts: all
  become: true
  vars:
    user_names:
      - lp
      - news
      - uucp
      - games
      - mail
  tasks:
  - name: Remove the user
    user:
      name: "{{ item }}"
      state: absent
      remove: yes
      force: yes
    loop: "{{ user_names }}"
    register: remove_users_result
  - debug: msg="{{ remove_users_result }}"
EOF

인벤토리 확인

ansible-inventory --graph
$ ansible-inventory --graph
@all:
  |--@etcd:
  |  |--kube-control1
  |--@k8s_cluster:
  |  |--@calico_rr:
  |  |--@kube_control_plane:
  |  |  |--kube-control1
  |  |--@kube_node:
  |  |  |--kube-node1
  |  |  |--kube-node2
  |  |  |--kube-node3
  |  |  |--kube-node4
  |  |  |--kube-node5
  |  |  |--kube-node6
  |--@nfs_server:
  |  |--kube-nfs1
  |--@ungrouped:

ansible-playbook 실행

ansible-playbook -i ~/kubespray/inventory/mycluster/inventory.ini userDelete.yaml --limit "kube_node"
$ ansible-playbook -i ~/kubespray/inventory/mycluster/inventory.ini userDelete.yaml --limit "kube_node"

PLAY [all] *******************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************
ok: [kube-node3]
ok: [kube-node1]
ok: [kube-node2]
ok: [kube-node4]
ok: [kube-node5]
ok: [kube-node6]

TASK [Remove the user] *******************************************************************************************
changed: [kube-node2] => (item=lp)
ok: [kube-node1] => (item=lp)
changed: [kube-node3] => (item=lp)
changed: [kube-node5] => (item=lp)
ok: [kube-node1] => (item=news)
changed: [kube-node2] => (item=news)
changed: [kube-node4] => (item=lp)
changed: [kube-node3] => (item=news)
ok: [kube-node1] => (item=uucp)
changed: [kube-node2] => (item=uucp)
changed: [kube-node3] => (item=uucp)
changed: [kube-node5] => (item=news)
ok: [kube-node1] => (item=games)
changed: [kube-node2] => (item=games)
changed: [kube-node3] => (item=games)
changed: [kube-node4] => (item=news)
changed: [kube-node2] => (item=mail)
ok: [kube-node1] => (item=mail)
changed: [kube-node3] => (item=mail)
changed: [kube-node5] => (item=uucp)
changed: [kube-node4] => (item=uucp)
changed: [kube-node5] => (item=games)
changed: [kube-node6] => (item=lp)
changed: [kube-node4] => (item=games)
changed: [kube-node6] => (item=news)
changed: [kube-node4] => (item=mail)
changed: [kube-node5] => (item=mail)
changed: [kube-node6] => (item=uucp)
changed: [kube-node6] => (item=games)
changed: [kube-node6] => (item=mail)

PLAY RECAP *******************************************************************************************************
kube-node1                 : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
kube-node2                 : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
kube-node3                 : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
kube-node4                 : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
kube-node5                 : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
kube-node6                 : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

passwd(/etc/passwd) 파일 확인

ansible -i ~/kubespray/inventory/mycluster/inventory.ini kube-node1 -m shell -a 'cat /etc/passwd'

 

참고URL

- Ansible Docs : https://docs.ansible.com/ansible/latest/collections/ansible/builtin/user_module.html

 

728x90
반응형