Ansible: Authorized_keys "exclusive" ์˜ต์…˜์€ with_subelements์™€ ํ•จ๊ป˜ ์‹คํŒจํ•ฉ๋‹ˆ๋‹ค.

์— ๋งŒ๋“  2015๋…„ 06์›” 03์ผ  ยท  3์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: ansible/ansible

1.9.1์—์„œ ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ์‚ฌ์šฉ์ž๋ณ„ ํ‚ค์˜ _group_์ด ๋‹จ๋…์œผ๋กœ ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š๊ณ  ๊ฐ _individual_ ์‚ฌ์šฉ์ž๋ณ„ ํ‚ค๊ฐ€ ๋‹จ๋…์œผ๋กœ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋งˆ์ง€๋ง‰์œผ๋กœ ์ง€์ •๋œ ์‚ฌ์šฉ์ž๋ณ„ ํ‚ค๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

- name: sync all users keys
  authorized_key: user={{item.0.name}} key='{{item.1}}' state=present exclusive=yes
  with_subelements:
    - users
    - keys

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

๋‚˜๋Š” ์ด๊ฒƒ์„ ์ค‘๊ฐ„ ๋‹จ๊ณ„๋กœ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ

# Read each file and split by newline, allowing for multiple keys per file
- name: Assemble keys
  set_fact:
    key_item: "{{ lookup('file', role_path + '/files/' + item).split('\n') }}"
  with_items:
    - bob.pub
    - fred.pub
    - barney.pub
  register: keys

# Select each key, joining it again and join all keys. Internally the ansible
# module will then split the string by newline and work at each one. This is
# the only way exclusive works with a list of keys.
- name: Distribute operations ssh-keys to root
  authorized_key:
    key        : "{{ keys.results|selectattr('ansible_facts','defined')|map(attribute='ansible_facts.key_item')|map('join', '\n')|join('\n') }}"
    manage_dir : yes
    state      : present
    user       : root
    exclusive  : yes

๋ชจ๋“  3 ๋Œ“๊ธ€

์ฆ‰, ๋งค๋ฒˆ ๋ฐฐํƒ€์ (X == ํ•ญ๋ชฉ ์ˆ˜)์œผ๋กœ ๋ชจ๋“ˆ์„ X๋ฒˆ ์‹คํ–‰ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๊ฐ€๋Šฅํ•œ ์˜คํ•ด

์•ˆ๋…•ํ•˜์„ธ์š”!

Ansible์— ์ œ์ถœํ•ด ์ฃผ์…”์„œ ๋Œ€๋‹จํžˆ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๊ฒƒ์€ ์ง„์‹ฌ์œผ๋กœ ์šฐ๋ฆฌ์—๊ฒŒ ๋งŽ์€ ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

ํ•œ ๊ฐ€์ง€๊ฐ€ ๋ช…์‹œ๋œ ๊ฒƒ๊ณผ ์กฐ๊ธˆ ๋‹ค๋ฅด๊ฒŒ ์ž‘๋™ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ท€ํ•˜๊ฐ€ ์ œ์ถœํ•œ ํ‹ฐ์ผ“์ด ๋‹ค์†Œ ์˜คํ•ด๋˜๊ณ  ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

์•ž์œผ๋กœ ์ด๊ฒƒ์€ ์‚ฌ์šฉ์ž ๋ชฉ๋ก์— ๋” ์ ํ•ฉํ•œ ์ฃผ์ œ๊ฐ€ ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์œ„์˜ ๋‚ด์šฉ๊ณผ ๊ด€๋ จํ•˜์—ฌ ๋” ๋งŽ์€ ๋„์›€์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์—ฌ๊ธฐ์— ๊ฒŒ์‹œํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์‹œ ํ•œ๋ฒˆ Ansible์— ๋Œ€ํ•œ ๊ด€์‹ฌ๊ณผ ์„ฑ์›์— ๊ฐ์‚ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค!

๋‚˜๋Š” ์ด๊ฒƒ์„ ์ค‘๊ฐ„ ๋‹จ๊ณ„๋กœ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ

# Read each file and split by newline, allowing for multiple keys per file
- name: Assemble keys
  set_fact:
    key_item: "{{ lookup('file', role_path + '/files/' + item).split('\n') }}"
  with_items:
    - bob.pub
    - fred.pub
    - barney.pub
  register: keys

# Select each key, joining it again and join all keys. Internally the ansible
# module will then split the string by newline and work at each one. This is
# the only way exclusive works with a list of keys.
- name: Distribute operations ssh-keys to root
  authorized_key:
    key        : "{{ keys.results|selectattr('ansible_facts','defined')|map(attribute='ansible_facts.key_item')|map('join', '\n')|join('\n') }}"
    manage_dir : yes
    state      : present
    user       : root
    exclusive  : yes
์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰