본문 바로가기

Automation Tools/Ansible

1.1 ansible | [test][playbook] 사용자 계정 추가

반응형

1. playbook

[root@ANSIBLE ansible]# cat add_user.yml
---
- name: "user add"
  hosts: test01
  become: true # 권한 상승
  tasks:
    - name: "user add" # user 모듈을 사용하여 사용자 추가
      user:
        name: "{{ add_user_name }}"
        password: "{{ upassword | password_hash('sha512') }}"
        shell: "/bin/bash"
    - name: "Add user to sudoers" # lineinfile 모듈을 사용하여 권한상승 설정 추가
      lineinfile:
        dest: /etc/sudoers
        regexp: "{{ add_user_name }}   ALL"
        line: "{{ add_user_name }}   ALL=(ALL)   ALL"
        state: present
#    - name: Expiring password for user "{{ add_user_name }}" ## 계정의 만료시간 설정
#      shell: chage -d 0 "{{ add_user_name }}"
  vars: # 변수 정의
    add_user_name: test3
    upassword: test1212

 

2. 실행

## 결과
[root@ANSIBLE ansible]# ansible-playbook -i hosts add_user.yml
 
PLAY [user add] ******************************************************************************************************************************************************************
 
TASK [Gathering Facts] ***********************************************************************************************************************************************************
ok: [test02]
 
TASK [user add] ******************************************************************************************************************************************************************
changed: [test02]
 
TASK [Add user to sudoers] *******************************************************************************************************************************************************
changed: [test02]
 
PLAY RECAP ***********************************************************************************************************************************************************************
test02                     : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
반응형