티스토리 뷰

[Ansible] ad-hoc 명령어 - CentOS 7.x



Ansible 설치


Ansible 설치를 진행하지 않았다면 " Ansible(앤서블) Install - CentOS 7.x " 포스팅을 참조하여 Ansible을 설치하시면 됩니다.


Ansible에는 두가지의 작업방법이 있습니다. 

첫번째로는 명령어를 통해 작업하는 Ad-hoc 방법이 있으며, 두번째로는 사전에 작업할 내용을 명시하여 playbook을 통한 작업이 있습니다.


이번 포스팅은 Ansible 명령어를 통해 작업을 하는 Ad-hoc 방법을 알아보도록 하겠습니다.




Ansible ad-hoc을 통한 시스템 정보 확인



[ Hosts의 Uptime 확인 ]


ansible all -m shell -a "uptime" -k


[root@Ansible-Master ~]# ansible all -m shell -a "uptime" -k
SSH password:
192.168.0.208 | SUCCESS | rc=0 >>
 17:25:04 up 26 min,  1 user,  load average: 0.000.010.02
 
192.168.0.206 | SUCCESS | rc=0 >>
 17:25:04 up 28 min,  1 user,  load average: 0.000.010.02
 
192.168.0.207 | SUCCESS | rc=0 >>
 17:25:04 up 27 min,  1 user,  load average: 0.000.010.04




[ Hosts의 메모리 확인 ]


ansible all -m shell -a "free -h" -k


[root@Ansible-Master ~]# ansible all -m shell -a "free -h" -k
SSH password:
192.168.0.207 | SUCCESS | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:           487M         73M        277M        4.5M        136M        375M
Swap:          1.5G          0B        1.5G
 
192.168.0.208 | SUCCESS | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:           487M         73M        276M        4.5M        137M        375M
Swap:          1.5G          0B        1.5G
 
192.168.0.206 | SUCCESS | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:           487M         73M        276M        4.5M        137M        375M
Swap:          1.5G          0B        1.5G




[ Hosts의 디스크 용량 확인 ]


ansible all -m shell -a "df -h" -k


[root@Ansible-Master ~]# ansible all -m shell -a "df -h" -k
SSH password:
192.168.0.207 | SUCCESS | rc=0 >>
Filesystem                       Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00   38G  675M   37G   2/
devtmpfs                         235M     0  235M   0/dev
tmpfs                            244M     0  244M   0/dev/shm
tmpfs                            244M  4.6M  240M   2/run
tmpfs                            244M     0  244M   0/sys/fs/cgroup
/dev/sda2                       1014M   63M  952M   7/boot
tmpfs                             49M     0   49M   0/run/user/0
 
192.168.0.206 | SUCCESS | rc=0 >>
Filesystem                       Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00   38G  675M   37G   2/
devtmpfs                         235M     0  235M   0/dev
tmpfs                            244M     0  244M   0/dev/shm
tmpfs                            244M  4.6M  240M   2/run
tmpfs                            244M     0  244M   0/sys/fs/cgroup
/dev/sda2                       1014M   63M  952M   7/boot
tmpfs                             49M     0   49M   0/run/user/0
 
192.168.0.208 | SUCCESS | rc=0 >>
Filesystem                       Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00   38G  675M   37G   2/
devtmpfs                         235M     0  235M   0/dev
tmpfs                            244M     0  244M   0/dev/shm
tmpfs                            244M  4.6M  240M   2/run
tmpfs                            244M     0  244M   0/sys/fs/cgroup
/dev/sda2                       1014M   63M  952M   7/boot
tmpfs                             49M     0   49M   0/run/user/0





Ansible ad-hoc을 통한 파일 전송하기



[ Hosts에 파일 전송하기 ]


ansible all -m copy -a "src=./test.txt dest=./test.txt" -k


[root@Ansible-Master /]# ansible all -m copy -a "src=./test.txt dest=./test.txt" -k
SSH password:
192.168.0.206 | SUCCESS => {
    "changed"true,
    "checksum""da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "dest""./test.txt",
    "gid"0,
    "group""root",
    "md5sum""d41d8cd98f00b204e9800998ecf8427e",
    "mode""0644",
    "owner""root",
    "secontext""unconfined_u:object_r:admin_home_t:s0",
    "size"0,
    "src""/root/.ansible/tmp/ansible-tmp-1533576752.86-97284736609756/source",                                     
    "state""file",
    "uid"0
}
192.168.0.207 | SUCCESS => {
    "changed"true,
    "checksum""da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "dest""./test.txt",
    "gid"0,
    "group""root",
    "md5sum""d41d8cd98f00b204e9800998ecf8427e",
    "mode""0644",
    "owner""root",
    "secontext""unconfined_u:object_r:admin_home_t:s0",
    "size"0,
    "src""/root/.ansible/tmp/ansible-tmp-1533576752.87-259136038018515/source",
    "state""file",
    "uid"0
}
192.168.0.208 | SUCCESS => {
    "changed"true,
    "checksum""da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "dest""./test.txt",
    "gid"0,
    "group""root",
    "md5sum""d41d8cd98f00b204e9800998ecf8427e",
    "mode""0644",
    "owner""root",
    "secontext""unconfined_u:object_r:admin_home_t:s0",
    "size"0,
    "src""/root/.ansible/tmp/ansible-tmp-1533576752.88-164961029053171/source",
    "state""file",
    "uid"0
}





Ansible ad-hoc을 통한 서비스 설치하기



[ Hosts에 서비스 설치하기 ]


ansible all -m yum -a "name=nginx state=present" -k


[root@Ansible-Master /]# ansible all -m yum -a "name=httpd state=present" -k
SSH password:
192.168.0.206 | SUCCESS => {
    "changed"true,
    "msg""warning: /var/cache/yum/x86_64/7/base/packages/apr-1.4.8-3.el7...",
    "rc"0,
    "results": [
        "Loaded plugins: fastestmirror\nLoading mirror speeds from cached ..."
    ]
}
192.168.0.208 | SUCCESS => {
    "changed"true,
    "msg""warning: /var/cache/yum/x86_64/7/base/packages/apr-1.4.8-3.el7...",
    "rc"0,
    "results": [
        "Loaded plugins: fastestmirror\nLoading mirror speeds from cached ..."
    ]
}
192.168.0.207 | SUCCESS => {
    "changed"true,
    "msg""warning: /var/cache/yum/x86_64/7/base/packages/apr-1.4.8-3.el7...",
    "rc"0,
    "results": [
        "Loaded plugins: fastestmirror\nLoading mirror speeds from cached ..."
    ]
}




[ Hosts에 서비스 설치된 서비스 확인하기 ]


ansible all -m rpm -a "rpm -qa | grep httpd" -k


[root@Ansible-Master /]# ansible all -m shell -a "rpm -qa | grep httpd" -k
SSH password:
 
192.168.0.208 | SUCCESS | rc=0 >>
httpd-tools-2.4.6-80.el7.centos.1.x86_64
httpd-2.4.6-80.el7.centos.1.x86_64
 
192.168.0.206 | SUCCESS | rc=0 >>
httpd-tools-2.4.6-80.el7.centos.1.x86_64
httpd-2.4.6-80.el7.centos.1.x86_64
 
192.168.0.207 | SUCCESS | rc=0 >>
httpd-tools-2.4.6-80.el7.centos.1.x86_64
httpd-2.4.6-80.el7.centos.1.x86_64



Comments
최근에 올라온 글