개요
- Ansible 실습 - Ansible Bootstrapping
root@98d42fda3b45:~/tutorial# a() { docker exec -it ansible bash -c "echo 'PS1='\''ansible# '\' >> /root/.bashrc; bash"; }
root@98d42fda3b45:~/tutorial# t() { docker exec -it target bash -c "echo 'PS1='\''target# '\' >> /root/.bashrc; bash"; }
root@98d42fda3b45:~/tutorial# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8aee4ce0c551 ubuntu:14.04 "/bin/bash -c 'apt-g…" 16 seconds ago Up 14 seconds target
c6a88fdee025 williamyeh/ansible:ubuntu14.04-onbuild "/bin/bash -c 'while…" 18 seconds ago Up 15 seconds ansible
root@98d42fda3b45:~/tutorial# a
ansible# ansible --version
ansible 2.7.2
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.6 (default, Nov 13 2018, 12:45:42) [GCC 4.8.4]
ansible# ansible all -i 'localhost,' -c local -m ping
localhost | SUCCESS => {
"changed": false,
"ping": "pong"
}
ansible# ansible all -i 'localhost,' -m ping
localhost | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host localhost port 22: Cannot assign requested address\r\n",
"unreachable": true
}
ansible# ansible all -i 'target,' -m ping
The authenticity of host 'target (172.19.0.3)' can't be established.
ECDSA key fingerprint is 65:1a:46:73:c3:3b:30:40:d6:1a:3b:29:2d:42:0b:f3.
Are you sure you want to continue connecting (yes/no)? yes
target | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Warning: Permanently added 'target,172.19.0.3' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,password).\r\n",
"unreachable": true
}
root@98d42fda3b45:~/tutorial# t
target# apt-get update; apt-get install -y openssh-server;service ssh start
Ign http://archive.ubuntu.com trusty InRelease
Hit http://archive.ubuntu.com trusty-updates InRelease
Hit http://archive.ubuntu.com trusty-backports InRelease
...
openssh-server is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.
* Starting OpenBSD Secure Shell server sshd [ OK ]
root@98d42fda3b45:~/tutorial# a
ansible# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
3f:90:3b:e0:84:ce:57:2e:e4:54:be:c8:93:8f:21:85 root@c6a88fdee025
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| . |
| o o . |
| E * S |
| o O * = |
| + @ = o |
| o * . . |
| . . |
+-----------------+
ansible# ls -ltr ~/.ssh
total 12
-rw-r--r-- 1 root root 444 Dec 20 08:12 known_hosts
-rw-r--r-- 1 root root 399 Dec 20 08:15 id_rsa.pub
-rw------- 1 root root 1675 Dec 20 08:15 id_rsa
ansible# cp ~/.ssh/id_rsa.pub /shared_volume/ansible_id_rsa.pub
ansible# exit
exit
root@98d42fda3b45:~/tutorial# t
target# [ ! -d ~/.ssh ] && mkdir ~/.ssh; cat /shared_volume/ansible_id_rsa.pub >> ~/.ssh/authorized_keys
target# cat ~/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCoe0g5acjJwHQHqbeh+Kf5fGBD3KgYcjVRvEtXyfaakIPmZXVwX8CQB3NSkUSxmILA5TsBewfdLStbiV8GSrolszQOWI9ucgHqg8qJSs3v+3iJ3/NVrZdPsgPTPR+2JOCKv3HlrWSnb098lXsdomweKm5n9ka1sPlBzd2pL6Jwo8UlcV5GILAAx7frVbA0TOEV5eJeSM2mXhALcge8AH9ibhBRgzg2dW4Imu05uPmqccXrcFOh4HrOF0dU0GLWfEbySer4Krzj+cudFA/GEF73aUr4JZ69p9Mk5b+WGY8sD4Li9mgeLrO5YbDFi34Tg1qjsdQHjVg4VrCelCKNLucv root@c6a88fdee025
root@98d42fda3b45:~/tutorial# a
ansible# ansible all -i 'target,' -m ping
target | SUCCESS => {
"changed": false,
"ping": "pong"
}
ansible# ansible all -i 'target,' -m shell -a "echo hello world\! > testfile"
target | CHANGED | rc=0 >>
root@98d42fda3b45:~/tutorial# t
target# cat ~/testfile
hello world!
같이 보기
참고