Ansible을 사용하여 새 사용자의 공개 키를 호스트 인벤토리에 푸시하고 싶습니다. 이를 위해 아래 예와 같이 플레이북이 생성됩니다.
---
- name: vms1 - Authorize hosts with pub key
hosts: vms1
tasks:
- name: Copy ssh pub key to remote host
ansible.posix.authorized_key:
user: user1
state: present
key: "{{ lookup('file', '/home/controluser/.ssh/id_rsa.pub') }}"
- name: vms2 - Authorize hosts with pub key
hosts: vms2
tasks:
- name: Copy ssh pub key to remote host
ansible.posix.authorized_key:
user: user2
state: present
key: "{{ lookup('file', '/home/controluser/.ssh/id_rsa.pub') }}"
- name: vms3 - Authorize hosts with pub key
hosts: vms3
tasks:
- name: Copy ssh pub key to remote host
ansible.posix.authorized_key:
user: user3
state: present
key: "{{ lookup('file', '/home/controluser/.ssh/id_rsa.pub') }}"
재고는 다음과 같습니다. (더 많은 호스트 사용 가능)
[vms1]
192.168.7.211
192.168.7.212
192.168.7.213
[vms2]
192.168.7.21
192.168.7.22
192.168.7.23
[vms3]
192.168.7.111
192.168.7.112
192.168.7.113
각 플레이에는 자체 호스트 그룹과 사용자가 있습니다. 플레이북은 다음 명령을 사용하여 시작할 수 있습니다.
ansible-playbook -k -i inventory playbook.yml
옵션을 사용하면 -k
SSH 비밀번호를 한 번만 묻습니다. 문제는 그룹마다 비밀번호가 다르다는 것입니다. 그래서 플레이할 때마다 비밀번호를 입력하고 싶어요.
플레이할 때마다 왜 비밀번호가 필요한가요?
답변1
묻다:"왜 플레이할 때마다 비밀번호를 입력해야 하나요?"
답: 변수를 사용하세요ansible_password. 예를 들어 플레이북에 변수를 넣는 것은변하기 쉬운
- hosts: vms1
vars:
ansible_password: connection passwd for vms1
tasks:
- name: Copy ssh pub key to remote host
...
- hosts: vms2
vars:
ansible_password: connection passwd for vms2
tasks:
- name: Copy ssh pub key to remote host
...
특정 정보 보기
shell> ansible-doc -t connection ssh
처음 플레이할 때 코드를 읽고 나중에 사용하세요. 예를 들어, 주어진 재고
shell> cat hosts
[vms1]
192.168.7.211
192.168.7.212
192.168.7.213
[vms2]
192.168.7.21
192.168.7.22
192.168.7.23
[vms3]
192.168.7.111
192.168.7.112
192.168.7.113
아래 스크립트
shell> cat pb.yml
- hosts: all
gather_facts: false
tasks:
- block:
- pause:
prompt: "Password for {{ item }}"
register: out
loop: "{{ groups|difference(['all', 'ungrouped']) }}"
- set_fact:
passwords: "{{ dict(out.results|json_query('[].[item, user_input]')) }}"
- debug:
var: passwords
run_once: true
- hosts: vms1
gather_facts: false
vars:
ansible_password: "{{ passwords.vms1 }}"
tasks:
- debug:
msg: "Copy ssh pub key to remote host by '{{ ansible_password }}'"
run_once: true
- hosts: vms2
gather_facts: false
vars:
ansible_password: "{{ passwords.vms2 }}"
tasks:
- debug:
msg: "Copy ssh pub key to remote host by '{{ ansible_password }}'"
run_once: true
- hosts: vms3
gather_facts: false
vars:
ansible_password: "{{ passwords.vms3 }}"
tasks:
- debug:
msg: "Copy ssh pub key to remote host by '{{ ansible_password }}'"
run_once: true
처음 플레이할 때 사용자에게 비밀번호를 묻는 메시지를 표시하고 나중에 사용합니다.
shell> ansible-playbook -i hosts pb.yml
PLAY [all] ***********************************************************************************
TASK [pause] *********************************************************************************
[pause]
Password for vms1:
passwd for vms1^Mok: [192.168.7.211] => (item=vms1)
[pause]
Password for vms2:
passwd for vms2^Mok: [192.168.7.211] => (item=vms2)
[pause]
Password for vms3:
passwd for vms3^Mok: [192.168.7.211] => (item=vms3)
TASK [set_fact] ******************************************************************************
ok: [192.168.7.211]
TASK [debug] *********************************************************************************
ok: [192.168.7.211] =>
passwords:
vms1: passwd for vms1
vms2: passwd for vms2
vms3: passwd for vms3
PLAY [vms1] **********************************************************************************
TASK [debug] *********************************************************************************
ok: [192.168.7.211] =>
msg: Copy ssh pub key to remote host by 'passwd for vms1'
PLAY [vms2] **********************************************************************************
TASK [debug] *********************************************************************************
ok: [192.168.7.21] =>
msg: Copy ssh pub key to remote host by 'passwd for vms2'
PLAY [vms3] **********************************************************************************
TASK [debug] *********************************************************************************
ok: [192.168.7.111] =>
msg: Copy ssh pub key to remote host by 'passwd for vms3'
PLAY RECAP ***********************************************************************************
192.168.7.111: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.7.21: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.7.211: ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0