본문 바로가기

IaC Application/ansible

ansible ad-hoc

[root@ansible-server ~]# uptime
 16:18:24 up 10 min,  2 users,  load average: 0.05, 0.10, 0.08
[root@ansible-server ~]# ansible all -m shell -a "uptime" -k
SSH password: 
200.200.200.148 | CHANGED | rc=0 >>
 16:18:59 up 11 min,  3 users,  load average: 0.00, 0.03, 0.04
200.200.200.147 | CHANGED | rc=0 >>
 16:18:59 up 2 min,  3 users,  load average: 0.40, 0.47, 0.20
[root@ansible-server ~]# top

[root@ansible-server ~]# ansible all -m shell -a "last reboot" -k
SSH password: 
200.200.200.148 | CHANGED | rc=0 >>
reboot   system boot  5.14.0-362.18.1. Mon Mar 25 16:07   still running
reboot   system boot  5.14.0-362.18.1. Tue Mar 26 01:03 - 16:07  (-8:55)
reboot   system boot  5.14.0-362.18.1. Fri Feb  2 15:51 - 16:01  (00:09)
reboot   system boot  5.14.0-362.18.1. Fri Feb  2 15:47 - 15:51  (00:03)
reboot   system boot  5.14.0-362.18.1. Fri Feb  2 15:37 - 15:47  (00:09)
reboot   system boot  5.14.0-362.18.1. Fri Feb  2 15:35 - 15:37  (00:02)
reboot   system boot  5.14.0-362.18.1. Fri Feb  2 15:30 - 15:35  (00:04)
reboot   system boot  5.14.0-362.8.1.e Fri Feb  2 15:00 - 15:30  (00:29)

wtmp begins Fri Feb  2 15:00:51 2024
200.200.200.147 | CHANGED | rc=0 >>
reboot   system boot  5.14.0-362.18.1. Mon Mar 25 16:16   still running
reboot   system boot  5.14.0-362.18.1. Mon Mar 25 16:07 - 16:14  (00:06)
reboot   system boot  5.14.0-362.18.1. Mon Mar 25 16:03 - 16:07  (00:04)
reboot   system boot  5.14.0-362.18.1. Tue Mar 26 01:02 - 16:02  (-8:59)
reboot   system boot  5.14.0-362.18.1. Fri Feb  2 15:51 - 16:01  (00:09)
reboot   system boot  5.14.0-362.18.1. Fri Feb  2 15:47 - 15:51  (00:03)
reboot   system boot  5.14.0-362.18.1. Fri Feb  2 15:37 - 15:47  (00:09)
reboot   system boot  5.14.0-362.18.1. Fri Feb  2 15:35 - 15:37  (00:02)
reboot   system boot  5.14.0-362.18.1. Fri Feb  2 15:30 - 15:35  (00:04)
reboot   system boot  5.14.0-362.8.1.e Fri Feb  2 15:00 - 15:30  (00:29)

wtmp begins Fri Feb  2 15:00:51 2024

[root@ansible-server ~]# ansible all -m user -a "name=auser01" -k
SSH password: 
200.200.200.148 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1001,
    "home": "/home/auser01",
    "name": "auser01",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1001
}
200.200.200.147 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1001,
    "home": "/home/auser01",
    "name": "auser01",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1001
}
[root@ansible-server ~]# ansible all -m shell -a "tail -1 /etc/passwd" -k
SSH password: 
200.200.200.147 | CHANGED | rc=0 >>
auser01:x:1001:1001::/home/auser01:/bin/bash
200.200.200.148 | CHANGED | rc=0 >>
auser01:x:1001:1001::/home/auser01:/bin/bash

[root@ansible-server ~]# ansible all -m shell -a "userdel -r auser01" -k
SSH password: 
200.200.200.148 | CHANGED | rc=0 >>

200.200.200.147 | CHANGED | rc=0 >>
[root@ansible-server ~]# ansible all -m user -a "name=auser01" -k
SSH password: 
200.200.200.147 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1001,
    "home": "/home/auser01",
    "name": "auser01",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1001
}
200.200.200.148 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1001,
    "home": "/home/auser01",
    "name": "auser01",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1001
}
[root@ansible-server ~]# ansible all -m shell -a "tail -1 /etc/passwd" -k
SSH password: 
200.200.200.147 | CHANGED | rc=0 >>
auser01:x:1001:1001::/home/auser01:/bin/bash
200.200.200.148 | CHANGED | rc=0 >>
auser01:x:1001:1001::/home/auser01:/bin/bash
[root@ansible-server ~]# ansible all -m user -a "name=auser01 state=absent" -k
SSH password: 
200.200.200.147 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "force": false,
    "name": "auser01",
    "remove": false,
    "state": "absent"
}
200.200.200.148 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "force": false,
    "name": "auser01",
    "remove": false,
    "state": "absent"
}
[root@ansible-server ~]# ansible all -m shell -a "tail -1 /etc/passwd" -k
SSH password: 
200.200.200.148 | CHANGED | rc=0 >>
rocky:x:1000:1000:rocky:/home/rocky:/bin/bash
200.200.200.147 | CHANGED | rc=0 >>
rocky:x:1000:1000:rocky:/home/rocky:/bin/bash
[root@ansible-server ~]# ansible all -m dnf -a "name=httpd state=present" -k
SSH password: 
200.200.200.147 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: apr-util-bdb-1.6.1-23.el9.x86_64",
        "Installed: apr-util-1.6.1-23.el9.x86_64",
        "Installed: mod_lua-2.4.57-5.el9.x86_64",
        "Installed: httpd-tools-2.4.57-5.el9.x86_64",
        "Installed: httpd-2.4.57-5.el9.x86_64",
        "Installed: httpd-filesystem-2.4.57-5.el9.noarch",
        "Installed: rocky-logos-httpd-90.15-2.el9.noarch",
        "Installed: mod_http2-1.15.19-5.el9.x86_64",
        "Installed: apr-1.7.0-12.el9_3.x86_64",
        "Installed: httpd-core-2.4.57-5.el9.x86_64",
        "Installed: apr-util-openssl-1.6.1-23.el9.x86_64"
    ]
}
200.200.200.148 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: apr-util-bdb-1.6.1-23.el9.x86_64",
        "Installed: apr-util-1.6.1-23.el9.x86_64",
        "Installed: mod_lua-2.4.57-5.el9.x86_64",
        "Installed: httpd-tools-2.4.57-5.el9.x86_64",
        "Installed: httpd-2.4.57-5.el9.x86_64",
        "Installed: httpd-filesystem-2.4.57-5.el9.noarch",
        "Installed: rocky-logos-httpd-90.15-2.el9.noarch",
        "Installed: mod_http2-1.15.19-5.el9.x86_64",
        "Installed: apr-1.7.0-12.el9_3.x86_64",
        "Installed: httpd-core-2.4.57-5.el9.x86_64",
        "Installed: apr-util-openssl-1.6.1-23.el9.x86_64"
    ]
}
[root@ansible-server ~]# echo hi > index.html
[root@ansible-server ~]# ansible all -m copy -a "src=/root/index.html dest=/var/www/html/index.html" -k
SSH password: 
200.200.200.148 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "checksum": "55ca6286e3e4f4fba5d0448333fa99fc5a404a73",
    "dest": "/var/www/html/index.html",
    "gid": 0,
    "group": "root",
    "md5sum": "764efa883dda1e11db47671c4a3bbd9e",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:httpd_sys_content_t:s0",
    "size": 3,
    "src": "/root/.ansible/tmp/ansible-tmp-1711352078.4842117-3822-26346187174949/source",
    "state": "file",
    "uid": 0
}
200.200.200.147 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "checksum": "55ca6286e3e4f4fba5d0448333fa99fc5a404a73",
    "dest": "/var/www/html/index.html",
    "gid": 0,
    "group": "root",
    "md5sum": "764efa883dda1e11db47671c4a3bbd9e",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:httpd_sys_content_t:s0",
    "size": 3,
    "src": "/root/.ansible/tmp/ansible-tmp-1711352078.471072-3821-198695550769494/source",
    "state": "file",
    "uid": 0
}

[root@ansible-server ~]# ansible all -m systemd -a "name=httpd.service state=started" -k
[root@ansible-server ~]# curl http://200.200.200.147
hi
[root@ansible-server ~]# curl http://200.200.200.148
hi

[root@ansible-server ~]# ansible all -m systemd -a "name=httpd.service state=stopped" -k
[root@ansible-server ~]# curl http://200.200.200.147
curl: (7) Failed to connect to 200.200.200.147 port 80: 연결이 거부됨
[root@ansible-server ~]# curl http://200.200.200.148
curl: (7) Failed to connect to 200.200.200.148 port 80: 연결이 거부됨

'IaC Application > ansible' 카테고리의 다른 글

vyos  (0) 2024.03.27
ansible Windows  (0) 2024.03.26
ansible playbook -  (0) 2024.03.26