1. 사용자 추가

1.1 사용자 1명 추가

그룹 생성 및 사용자 추가는 여러 개 있어도 대응할 수 있지만, Key 파일에 문제가 발생합니다.

OS 사용자 추가는 크게 4가지의 작업으로 구성합니다.

# site.yaml
---
- name: Add a User
  hosts: web
  vars_files:
    - "./VARS/vars.yaml"
  become: yes
  gather_facts: no
  tasks:
    - name: Ensure group exists
      ansible.builtin.group:
        name: "{{ item.group }}"
        state: present
      loop: "{{ users }}"

    - name: Create a user name
      ansible.builtin.user:
        name: "{{ item.1.name }}"
        # primary group
        group: "{{ item.0.group }}"
        # secondary group
        groups: "{{ item.0.group }}"
        append: yes
        state: present
        shell: /bin/bash
      with_subelements:
        - "{{ users }}"
        - member

    - name: Create a directory if it does not exist
      ansible.builtin.file:
        path: /home/web/.ssh
        state: directory

    - name: Creating a file with content
      copy:
        dest: "/home/web/.ssh/authorized_keys"
        content: "{{content}}"
# ./VARS/vars.yaml
---
# Add Users
users:
  - group: web
    member:
      - name: web01

content: |
      <public-key-1>
      <public-key-2>

# /etc/ansible/hosts
[web]
100.100.100.100
100.100.100.101

[web:vars]
ansible_user=ubuntu
ansible_port=30000
ansible_python_interpreter=/usr/bin/python3
다음에 참고할 코드!

./VARS/vars.yaml 파일에 public key를 넣는 것보다 public key 파일을 직접 변수로 활용하여 authorized_key 모듈을 사용하여 작성할 예정입니다.

- name: Set up multiple authorized keys
  ansible.posix.authorized_key:
    user: deploy
    state: present
    key: '{{ item }}'
  with_file:
    - public_keys/doe-jane
    - public_keys/doe-john
마지막으로,

끝까지 읽어주신 모든 분들께 감사드립니다.

다음 글 보기
이전 글 보기
[Ansible] 사용자 추가