한 번의 명령어로 다수의 시스템에 작업하기
uptime 확인
[root@Ansible-Server ~]# ansible all -m shell -a "uptime" -k
SSH password:
172.30.1.13 | CHANGED | rc=0 >>
04:07:56 up 2:57, 2 users, load average: 0.00, 0.01, 0.04
172.30.1.11 | CHANGED | rc=0 >>
04:07:56 up 2:57, 2 users, load average: 0.00, 0.01, 0.03
172.30.1.12 | CHANGED | rc=0 >>
04:07:57 up 2:57, 2 users, load average: 0.08, 0.03, 0.05
디스크 용량 확인
[root@Ansible-Server ~]# ansible all -m shell -a "df -h"
172.30.1.11 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
devtmpfs 484M 0 484M 0% /dev
tmpfs 496M 0 496M 0% /dev/shm
tmpfs 496M 6.8M 489M 2% /run
tmpfs 496M 0 496M 0% /sys/fs/cgroup
/dev/mapper/centos-root 50G 1.3G 49G 3% /
/dev/sda1 1014M 138M 877M 14% /boot
/dev/mapper/centos-home 2.0T 33M 2.0T 1% /home
tmpfs 100M 0 100M 0% /run/user/0
172.30.1.12 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
devtmpfs 484M 0 484M 0% /dev
tmpfs 496M 0 496M 0% /dev/shm
tmpfs 496M 6.8M 489M 2% /run
tmpfs 496M 0 496M 0% /sys/fs/cgroup
/dev/mapper/centos-root 50G 1.3G 49G 3% /
/dev/sda1 1014M 138M 877M 14% /boot
/dev/mapper/centos-home 2.0T 33M 2.0T 1% /home
tmpfs 100M 0 100M 0% /run/user/0
172.30.1.13 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
devtmpfs 484M 0 484M 0% /dev
tmpfs 496M 0 496M 0% /dev/shm
tmpfs 496M 6.8M 489M 2% /run
tmpfs 496M 0 496M 0% /sys/fs/cgroup
/dev/mapper/centos-root 50G 1.3G 49G 3% /
/dev/mapper/centos-home 2.0T 33M 2.0T 1% /home
/dev/sda1 1014M 138M 877M 14% /boot
tmpfs 100M 0 100M 0% /run/user/0
메모리 상태 확인
[root@Ansible-Server ~]# ansible all -m shell -a "free -h" -k
SSH password:
172.30.1.12 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 990M 165M 706M 6.8M 119M 693M
Swap: 2.0G 0B 2.0G
172.30.1.13 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 990M 163M 708M 6.8M 119M 694M
Swap: 2.0G 0B 2.0G
172.30.1.11 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 990M 164M 706M 6.8M 119M 693M
Swap: 2.0G 0B 2.0G
새로운 유저 생성
[root@Ansible-Server ~]# ansible all -m user -a "name=user01 password=1234" -k
SSH password:
[WARNING]: The input password appears not to have been hashed. The 'password' argument
must be encrypted for this module to work properly.
172.30.1.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1000,
"home": "/home/user01",
"name": "user01",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1000
}
172.30.1.13 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1000,
"home": "/home/user01",
"name": "user01",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1000
}
172.30.1.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1000,
"home": "/home/user01",
"name": "user01",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1000
}
ansible-node01에 접속하여 user01 사용자가 생성되었는지 확인
[root@Ansible-Server ~]# ssh root@172.30.1.11
[root@ansible-node01 ~]# cat /etc/passwd
...
user01:x:1000:1000::/home/user01:/bin/bash
파일 전송
파일을 하나 생성하여 관리되고 있는 노드들에 모두 전송
# 노드들에 전송할 파일 생성: jin.file
[root@Ansible-Server ~]# ls -rlt
total 8
-rw-------. 1 root root 1214 Jun 1 23:45 anaconda-ks.cfg
-rw-r--r--. 1 root root 24 Jun 2 03:43 test
[root@Ansible-Server ~]# touch jin.file
[root@Ansible-Server ~]# ls -rlt
total 8
-rw-------. 1 root root 1214 Jun 1 23:45 anaconda-ks.cfg
-rw-r--r--. 1 root root 24 Jun 2 03:43 test
-rw-r--r--. 1 root root 0 Jun 2 04:15 jin.file
# nginx에 속한 노드들의 /tmp 경로로 jin.file을 복사하겠다
[root@Ansible-Server ~]# ansible nginx -m copy -a "src=./jin.file dest=/tmp/" -k
SSH password:
172.30.1.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/tmp/jin.file",
"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-1654157890.28-10394-49655578753014/source",
"state": "file",
"uid": 0
}
172.30.1.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/tmp/jin.file",
"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-1654157890.29-10395-66125307720210/source",
"state": "file",
"uid": 0
}
172.30.1.13 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/tmp/jin.file",
"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-1654157890.32-10396-93284678817142/source",
"state": "file",
"uid": 0
}
전송된 파일 확인
[root@Ansible-Server ~]# ssh root@172.30.1.11
root@172.30.1.11's password:
Last login: Thu Jun 2 04:18:11 2022 from 172.30.1.10
[root@ansible-node01 ~]# ls -rlt /tmp
total 4
...
-rw-r--r--. 1 root root 0 Jun 2 04:18 jin.file
서비스 설치
노드들에 아파치를 설치하도록 명령 실행
에러 발생: 각 노드들에 DNS 설정이 되어있지 않기 때문
[root@Ansible-Server ~]# ansible nginx -m yum -a "name=httpd state=present" -k
SSH password:
172.30.1.11 | FAILED! => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"msg": "Failure talking to yum: Cannot find a valid baseurl for repo: base/7/x86_64"
}
172.30.1.13 | FAILED! => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"msg": "Failure talking to yum: Cannot find a valid baseurl for repo: base/7/x86_64"
}
172.30.1.12 | FAILED! => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"msg": "Failure talking to yum: Cannot find a valid baseurl for repo: base/7/x86_64"
}
에러 해결: ansible-server에 있는 DNS 설정 파일을 각 노드로 복사
[root@Ansible-Server ~]# ansible nginx -m copy -a "src=/etc/resolv.conf dest=/etc/resolv.conf" -k
다시 설치 명령 실행
[root@Ansible-Server ~]# ansible nginx -m yum -a "name=httpd state=present" -k
ansible-node01에 접속하여 설치 확인
[root@Ansible-Server ~]# ssh root@172.30.1.11
root@172.30.1.11's password:
Last login: Thu Jun 2 04:26:05 2022 from 172.30.1.10
[root@ansible-node01 ~]# yum list installed | grep httpd
httpd.x86_64 2.4.6-97.el7.centos.5 @updates
httpd-tools.x86_64 2.4.6-97.el7.centos.5 @updates
# 설치가 완료되었지만 실행은 되지 않음
[root@ansible-node01 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:httpd(8)
man:apachectl(8)