Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
joohanhong
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
JooHan Hong
joohanhong
Commits
a288cec7
Commit
a288cec7
authored
4 years ago
by
JooHan Hong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ansible, 2021-03-08, update4
parent
6ec918fc
Pipeline
#5113
passed with stages
in 2 seconds
Changes
4
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
201 additions
and
189 deletions
+201
-189
README.md
ANSIBLE/README.md
+1
-2
README.md
ANSIBLE/USER/PASSWORD_MODIFY/README.md
+0
-0
README.md
ANSIBLE/USER/README.md
+7
-187
README.md
ANSIBLE/USER/USER_CONTROL/README.md
+193
-0
No files found.
ANSIBLE/README.md
View file @
a288cec7
...
...
@@ -11,8 +11,7 @@
| 1 | OS Maintenance ITEMs |
[
OS
](
./OS/
)
| |
| 2 | INFRA Maintenance ITEMs |
[
GO
](
./INFRA/
)
| |
| 3 | WEB Server Maintenance ITEMs |
[
GO
](
./WEB/
)
| |
| 4 | Users Management(추가/제거) ITEMs |
[
GO
](
./USER/
)
| |
| 5 | Users Management(변경) ITEMs |
[
GO
](
./USER2/
)
| |
| 4 | Users Management ITEMs |
[
GO
](
./USER/
)
| |
| 6 | Virtualization Maintenance ITEMs |
[
GO
](
./VM/
)
| |
| 7 | Secure Vulnerability ITEMs |
[
GO
](
./SECURE/
)
| |
| 8 | MISC ITEMs |
[
GO
](
./MISC/
)
| |
...
...
This diff is collapsed.
Click to expand it.
ANSIBLE/USER
2
/README.md
→
ANSIBLE/USER
/PASSWORD_MODIFY
/README.md
View file @
a288cec7
File moved
This diff is collapsed.
Click to expand it.
ANSIBLE/USER/README.md
View file @
a288cec7
[

](https://www.hongsnet.net)
# Ansible
을 이용한 사용자계정 관리
# Ansible
활용
> 사용자
의 추가/제거을 수행하고, 추가적으로 ssh의 Allow 설정을 적용한 후 txt 파일로 수행하는 시스템(PC 또는 서버)에 증적을 남긴
다.
> 사용자
관리를 수행한
다.
#
# 주요 기능
#
Table of Contents
-
리눅스 사용자에 대한 추가를 수행한다.
-
ssh 설정에
**AllowUsers**
또는
**AllowGroup**
설정을 수행할 수 있다.
-
수행 후 증적을 위해 현재 로컬에
*IP주소*
/
*날짜*
(YYYY-MM-DD)/
*사용자아이디*
/
*사용자아이디_허용*
IP.txt 파일을 기록한다.
| NO | ITEM | Content | 비고 |
| ------ | ------ | ------ | ------ |
| 1 | OS User Additional / Remove |
[
OS
](
./USER_CONTROL/
)
| |
| 2 | OS User Password Modify |
[
GO
](
./PASSWORD_MODIFY/
)
| |
## Inventory 설정
```
bash
# cat hosts
[
USERS]
172.16.0.100
managed_ip
=
172.16.0.100
des
=
"test user add, 2020-12-03"
[
USERS_OK]
```
대상 호스트는 172.16.0.100이며, 추가는 2020-12-03에 수행한다. 참고적으로
**USERS_OK**
호스트 그룹은 작업이 완료된 호스트의 history를 위한 그룹이다.
## Host Variables 설정
```
bash
# cat host_vars/172.16.0.100
ansible_ssh_host: 172.16.0.100
ansible_ssh_port: SSH_포트번호
ansible_ssh_user: 사용자 아이디
ansible_ssh_pass:
"사용자 패스워드"
ansible_become:
yes
ansible_become_method: su
ansible_become_user: root
ansible_become_pass:
"!root_패스워드"
ansible_python_interpreter: python2.7
```
## Playbook 설정
-
`사용자 추가`
```python
---
-
name: System User Additional
hosts: USERS
vars:
add_user_name: '사용자 아이디'
user_password: '사용자 패스워드'
allow_ip: '사용자의 PC IP주소'
time: "{{lookup('pipe','date
\"
+%Y-%m-%d
\"
')}}"
allow_user_check: '{{ add_user_name }}@{{ allow_ip }}'
tasks:
-
name: Local Directory Create
local_action: command mkdir -p USER_ADD/{{ managed_ip }}/{{ time }}/{{ user_name }}
-
name: New System User Add
user:
name: "{{ add_user_name }}"
password: "{{ user_password | password_hash('sha512') }}"
shell: /bin/bash
-
name: wheel Group Add
group:
name: wheel
state: present
-
name: User Add Wheel Group
shell: usermod -G wheel {{ add_user_name }}
-
name: AllowUsers checking
shell: cat /etc/ssh/sshd_config |grep "AllowUsers {{ allow_user_check }}" |wc -l
register: allowuser_exist
-
debug: var=allowuser_exist.stdout
-
name: /etc/ssh/sshd_config Add AllowUsers
shell: echo "AllowUsers {{ allow_user_check }}" >> /etc/ssh/sshd_config
when: allowuser_exist.stdout == "0"
-
name: sshd Daemon Restart
service:
name: ssh.service
state: reloaded
enabled: yes
when: allowuser_exist.stdout == "0"
-
name: System User AllowIP Result
local_action: copy content={{ allow_ip }} dest=USER_DEL/{{ managed_ip }}/{{ time }}/{{ user_name }}/{{ user_name }}_allowip.txt
when: allowuser_exist.stdout == "0"
```
`사용자 추가`에 대한 Playbook 분석
* [ ] **추가하려는 사용자**에 대한 아이디(add_user_name)/패스워드(user_password)/IP주소(allow_ip)는 변수로 설정한다.
* [ ] **root 유저로의 전환**은 wheel 그룹에 속한 사용자만 가능하므로, wheel 그룹에 추가한다.
* [ ] 만약 기존 사용자의 정보가 존재한다면, AllowUsers 설정이 불필요하므로, 체크한다(**allowuser_exist 변수**).
* [ ] 사용자가 존재하지않는다면(**allowuser_exist.stdout == "0"**), /etc/ssh/sshd_config에 AllowUsers 설정을 추가한 sshd 서비스데몬을 reload 한다.
- `사용자 제거`
```
python
---
-
name: System User Additional
hosts: USERS
vars:
del_user_name: '사용자 아이디'
allow_ip: '사용자의 PC IP주소'
time: "{{lookup('pipe','date
\"
+%Y-%m-%d
\"
')}}"
tasks:
-
name: Local Directory Create
local_action: command mkdir -p USER_DEL/{{ managed_ip }}/{{ time }}/{{ user_name }}
-
name: System User Exist Check
shell: id -u "{{ user_name }}"
register: user_exists
ignore_errors: True
-
name: System User Remove
user: name={{ user_name }}
state=absent
force=yes
remove=yes #사용자 홈디렉토리를 포함한 모든 설정을 지운다.
when: user_exists.rc == 0
-
name: User Remove Whell Group
shell: usermod -G "" {{ user_name }}
when: user_exists.rc == 0
-
name: User Remove sshd_config AllowUsers
lineinfile:
dest: /etc/ssh/sshd_config
regexp: "^AllowUsers {{ user_name }}@{{ allow_ip }}"
state: absent
ignore_errors: True
when: user_exists.rc == 0
-
name: sshd Daemon Restart
service:
name: ssh.service
state: reloaded
enabled: yes
when: user_exists.rc == 0
-
name: System User Password Modify Result
local_action: copy content={{ user_exists }} dest=USER_DEL/{{ managed_ip }}/{{ time }}/{{ user_name }}/{{ user_name }}_result.txt
when: user_exists.rc == 0
-
name: System User AllowIP Result
local_action: copy content={{ allow_ip }} dest=USER_DEL/{{ managed_ip }}/{{ time }}/{{ user_name }}/{{ user_name }}_allowip.txt
when: user_exists.rc == 0
```
`사용자 제거`에 대한 Playbook 분석
* [ ] **제거하려는 사용자**에 대한 아이디(del_user_name)/IP주소(allow_ip)는 변수로 설정한다.
* [ ] 사용자의 모든 그룹을 제거한다. 물론 이 과정은 wheel에 추가된 내역을 지우는데 목적이 있다.
* [ ] 사용자를 제거한다. 여기서 주의할 사항은 `remove=yes`로 인해 홈 디렉토리의 데이터까지 모두 제거된다.
* [ ] /etc/ssh/sshd_config 파일에 AllowUsers 설정을 지우는데, 해당 유저정보가 Match하면 지우게 된다.
## playbook 실행
- `사용자 추가`
```
bash
# ansible-playbook -i hosts user_add.yml
```
- `사용자 제거`
```
bash
# ansible-playbook -i hosts user_del.yml
```
## 수행 결과
```
bash
# ls USER_DEL/172.16.0.100/2020-12-03/test/test_allowip.txt
USER_DEL/10.130.10.79/2021-03-08/test/test_allowip.txt
# cat USER_DEL/172.16.0.100/2020-12-03/test/test_allowip.txt
172.
16.0.100
```
This diff is collapsed.
Click to expand it.
ANSIBLE/USER/USER_CONTROL/README.md
0 → 100644
View file @
a288cec7
[

](https://www.hongsnet.net)
# Ansible을 이용한 사용자계정 관리
> 사용자의 추가/제거을 수행하고, 추가적으로 ssh의 Allow 설정을 적용한 후 txt 파일로 수행하는 시스템(PC 또는 서버)에 증적을 남긴다.
## 주요 기능
-
리눅스 사용자에 대한 추가를 수행한다.
-
ssh 설정에
**AllowUsers**
또는
**AllowGroup**
설정을 수행할 수 있다.
-
수행 후 증적을 위해 현재 로컬에
*IP주소*
/
*날짜*
(YYYY-MM-DD)/
*사용자아이디*
/
*사용자아이디_허용*
IP.txt 파일을 기록한다.
## Inventory 설정
```
bash
# cat hosts
[
USERS]
172.16.0.100
managed_ip
=
172.16.0.100
des
=
"test user add, 2020-12-03"
[
USERS_OK]
```
대상 호스트는 172.16.0.100이며, 추가는 2020-12-03에 수행한다. 참고적으로
**USERS_OK**
호스트 그룹은 작업이 완료된 호스트의 history를 위한 그룹이다.
## Host Variables 설정
```
bash
# cat host_vars/172.16.0.100
ansible_ssh_host: 172.16.0.100
ansible_ssh_port: SSH_포트번호
ansible_ssh_user: 사용자 아이디
ansible_ssh_pass:
"사용자 패스워드"
ansible_become:
yes
ansible_become_method: su
ansible_become_user: root
ansible_become_pass:
"!root_패스워드"
ansible_python_interpreter: python2.7
```
## Playbook 설정
-
`사용자 추가`
```python
---
-
name: System User Additional
hosts: USERS
vars:
add_user_name: '사용자 아이디'
user_password: '사용자 패스워드'
allow_ip: '사용자의 PC IP주소'
time: "{{lookup('pipe','date
\"
+%Y-%m-%d
\"
')}}"
allow_user_check: '{{ add_user_name }}@{{ allow_ip }}'
tasks:
-
name: Local Directory Create
local_action: command mkdir -p USER_ADD/{{ managed_ip }}/{{ time }}/{{ user_name }}
-
name: New System User Add
user:
name: "{{ add_user_name }}"
password: "{{ user_password | password_hash('sha512') }}"
shell: /bin/bash
-
name: wheel Group Add
group:
name: wheel
state: present
-
name: User Add Wheel Group
shell: usermod -G wheel {{ add_user_name }}
-
name: AllowUsers checking
shell: cat /etc/ssh/sshd_config |grep "AllowUsers {{ allow_user_check }}" |wc -l
register: allowuser_exist
-
debug: var=allowuser_exist.stdout
-
name: /etc/ssh/sshd_config Add AllowUsers
shell: echo "AllowUsers {{ allow_user_check }}" >> /etc/ssh/sshd_config
when: allowuser_exist.stdout == "0"
-
name: sshd Daemon Restart
service:
name: ssh.service
state: reloaded
enabled: yes
when: allowuser_exist.stdout == "0"
-
name: System User AllowIP Result
local_action: copy content={{ allow_ip }} dest=USER_DEL/{{ managed_ip }}/{{ time }}/{{ user_name }}/{{ user_name }}_allowip.txt
when: allowuser_exist.stdout == "0"
```
`사용자 추가`에 대한 Playbook 분석
* [ ] **추가하려는 사용자**에 대한 아이디(add_user_name)/패스워드(user_password)/IP주소(allow_ip)는 변수로 설정한다.
* [ ] **root 유저로의 전환**은 wheel 그룹에 속한 사용자만 가능하므로, wheel 그룹에 추가한다.
* [ ] 만약 기존 사용자의 정보가 존재한다면, AllowUsers 설정이 불필요하므로, 체크한다(**allowuser_exist 변수**).
* [ ] 사용자가 존재하지않는다면(**allowuser_exist.stdout == "0"**), /etc/ssh/sshd_config에 AllowUsers 설정을 추가한 sshd 서비스데몬을 reload 한다.
- `사용자 제거`
```
python
---
-
name: System User Additional
hosts: USERS
vars:
del_user_name: '사용자 아이디'
allow_ip: '사용자의 PC IP주소'
time: "{{lookup('pipe','date
\"
+%Y-%m-%d
\"
')}}"
tasks:
-
name: Local Directory Create
local_action: command mkdir -p USER_DEL/{{ managed_ip }}/{{ time }}/{{ user_name }}
-
name: System User Exist Check
shell: id -u "{{ user_name }}"
register: user_exists
ignore_errors: True
-
name: System User Remove
user: name={{ user_name }}
state=absent
force=yes
remove=yes #사용자 홈디렉토리를 포함한 모든 설정을 지운다.
when: user_exists.rc == 0
-
name: User Remove Whell Group
shell: usermod -G "" {{ user_name }}
when: user_exists.rc == 0
-
name: User Remove sshd_config AllowUsers
lineinfile:
dest: /etc/ssh/sshd_config
regexp: "^AllowUsers {{ user_name }}@{{ allow_ip }}"
state: absent
ignore_errors: True
when: user_exists.rc == 0
-
name: sshd Daemon Restart
service:
name: ssh.service
state: reloaded
enabled: yes
when: user_exists.rc == 0
-
name: System User Password Modify Result
local_action: copy content={{ user_exists }} dest=USER_DEL/{{ managed_ip }}/{{ time }}/{{ user_name }}/{{ user_name }}_result.txt
when: user_exists.rc == 0
-
name: System User AllowIP Result
local_action: copy content={{ allow_ip }} dest=USER_DEL/{{ managed_ip }}/{{ time }}/{{ user_name }}/{{ user_name }}_allowip.txt
when: user_exists.rc == 0
```
`사용자 제거`에 대한 Playbook 분석
* [ ] **제거하려는 사용자**에 대한 아이디(del_user_name)/IP주소(allow_ip)는 변수로 설정한다.
* [ ] 사용자의 모든 그룹을 제거한다. 물론 이 과정은 wheel에 추가된 내역을 지우는데 목적이 있다.
* [ ] 사용자를 제거한다. 여기서 주의할 사항은 `remove=yes`로 인해 홈 디렉토리의 데이터까지 모두 제거된다.
* [ ] /etc/ssh/sshd_config 파일에 AllowUsers 설정을 지우는데, 해당 유저정보가 Match하면 지우게 된다.
## playbook 실행
- `사용자 추가`
```
bash
# ansible-playbook -i hosts user_add.yml
```
- `사용자 제거`
```
bash
# ansible-playbook -i hosts user_del.yml
```
## 수행 결과
```
bash
# ls USER_DEL/172.16.0.100/2020-12-03/test/test_allowip.txt
USER_DEL/10.130.10.79/2021-03-08/test/test_allowip.txt
# cat USER_DEL/172.16.0.100/2020-12-03/test/test_allowip.txt
172.
16.0.100
```
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment