Ansible - ad-hoc or basic

Page content

Create inventory(Ansible hosts) file

mbpr15:Homebrew cychong$ cat /etc/ansible/hosts
mini1 ansible_host=x.y.z.a ansible_ssh_user=cychong ansible_ssh_port=22
mini2 ansible_host=x.y.z.b ansible_ssh_user=cychong ansible_ssh_port=22
localhost     ansible_connection=local

ping

ping 명령도 ansible이 제공하는 ping module을 이용하므로 -m 옵션을 사용한다.

mbpr15:Homebrew cychong$ ansible all -m ping -k
SSH password: 
localhost | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
mini2 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
mini1 | UNREACHABLE! => {
    "changed": false,
    "msg": "timed out",
    "unreachable": true
}

ssh의 로그인 ID는 /etc/ansible/hosts에 기술하거나 명령어 옵션 —user=cychong으로 지정할 수 있다.

shell module

syntax

-m MODULE_NAME
-a MODULE_ARG
[-u/--user] USER_ID
[-sudo] 로그인 후 SUDO 권한으로 명령어 실행
[-sudo-user USER_ID] 로그인 후 특정 USER_ID 권한으로 명령어 실행 
[-b] -sudo 옵션 대체
[--become-user USER_ID] -sudo-user 옵션 대체

example

shell module의 명령어 df -k 실행 (df -h를 인자로 지정하면 -hansible 명령의 —help 옵션으로 간주된다. 그러므로 이 경우에는 ”df -h”와 같이 따옴표로 module argument를 묶어준다)

mbpr15:Homebrew cychong$ ansible all -m shell -a df -k
SSH password: 
localhost | SUCCESS | rc=0 >>
Filesystem                                                                                                  512-blocks       Used  Available Capacity   iused               ifree %iused  Mounted on
/dev/disk1s1                                                                                                 976490568  573048520  388412536    60%   1229764 9223372036853546043    0%   /
devfs                                                                                                              395        395          0   100%       684                   0  100%   /dev
/dev/disk1s4                                                                                                 976490568   12582968  388412536     4%         6 9223372036854775801    0%   /private/var/vm
map -hosts                                                                                                           0          0          0   100%         0                   0  100%   /net
map auto_home                                                                                                        0          0          0   100%         0                   0  100%   /home
//cychong@DISKSTATION._afpovertcp._tcp.local/Shelter                                                        7681886800 4548217416 3133669384    60% 568527175           391708673   59%   /Volumes/Shelter
//cychong@DISKSTATION._afpovertcp._tcp.local/media                                                          1220947280  966911944  254035336    80% 120863991            31754417   79%   /Volumes/media
//cychong@DISKSTATION._afpovertcp._tcp.local/video                                                          7681886800 4548217416 3133669384    60% 568527175           391708673   59%   /Volumes/video
//com.apple.idms.appleid.prd.4758655770614553526e3849724845577074615846773d3d@mini2._smb._tcp.local/cychong  249660000  199914656   49745344    81%  24989330             6218168   80%   /Volumes/cychong

mini2 | SUCCESS | rc=0 >>
Filesystem    512-blocks       Used Available Capacity iused               ifree %iused  Mounted on
/dev/disk2s1   249660000  198640784  49745232    80% 1226230 9223372036853549577    0%   /
devfs                393        393         0   100%     682                   0  100%   /dev
/dev/disk2s4   249660000         48  49745232     1%       0 9223372036854775807    0%   /private/var/vm
/dev/disk3s2   233769824  142965688  90804136    62%  757164          4294210115    0%   /Volumes/120G
/dev/disk1s2  1951845728 1515454904 436390824    78%  460710          4294506569    0%   /Volumes/data
map -hosts             0          0         0   100%       0                   0  100%   /net
map auto_home          0          0         0   100%       0                   0  100%   /home
map -fstab             0          0         0   100%       0                   0  100%   /Network/Servers
/dev/disk4s2   976101344  523330000 452771344    54%    1596          4294965683    0%   /Volumes/500G

copy module

Target host 들에게 파일을 보내는 경우(put)

mbpr15:Homebrew cychong$ ansible all -m copy -a "src=~/.vimrc dest=/tmp"
localhost | SUCCESS => {
    "changed": true,
    "checksum": "052ddf17cff54f1fb8231c7a5930be30756456d3",
    "dest": "/tmp/.vimrc",
    "gid": 20,
    "group": "staff",
    "md5sum": "f11f67ca155917ae34e4a459b2603562",
    "mode": "0644",
    "owner": "cychong",
    "size": 28,
    "src": "/Users/cychong/.ansible/tmp/ansible-tmp-1514382698.5150971-237795226033898/source",
    "state": "file",
    "uid": 501
}
mini2 | SUCCESS => {
    "changed": true,
    "checksum": "052ddf17cff54f1fb8231c7a5930be30756456d3",
    "dest": "/tmp/.vimrc",
    "gid": 20,
    "group": "staff",
    "md5sum": "f11f67ca155917ae34e4a459b2603562",
    "mode": "0644",
    "owner": "cychong",
    "size": 28,
    "src": "/Users/cychong/.ansible/tmp/ansible-tmp-1514382698.5150971-84669710159236/source",
    "state": "file",
    "uid": 501
}

제대로 복사 되었는지 확인

mbpr15:Homebrew cychong$ ansible all -m shell -a "ls -al /tmp/.vimrc"
localhost | SUCCESS | rc=0 >>
-rw-r--r--  1 cychong  staff  28 Dec 27 22:51 /tmp/.vimrc

mini2 | SUCCESS | rc=0 >>
-rw-r--r--  1 cychong  staff  28 Dec 27 22:51 /tmp/.vimrc

지원되는 모듈

Module Index — Ansible Documentation에서 참고