Ansible : Basic Usage2024/09/11 |
This is the Basic Usage of Ansible. ⇒ ansible [Target Hosts] [Option] -m [Module] -a [Arguments]
* There are many modules provided by official site and you can refer them on the it.
It's necessary to authenticate with a user on using Ansible because it uses SSH access. |
|
[1] | For the case which SSH servers on client hosts allow direct root login, (except [PermitRootLogin no]) + key-pair authentication (non-passphrase), it's possible to use Ansible like follows. If passphrase is set in key-pair, it's possible to use it with SSH-Agent. |
# run [ping] module to [10.0.0.50] root@dlp:~ # ansible 10.0.0.50 -m ping 10.0.0.50 | SUCCESS => { "changed": false, "ping": "pong" } |
[2] |
For the case you connect to client hosts with a common user who can use privilege by [sudo].
For example, [freebsd] user runs Ansible. Also, [-k] option below means it uses SSH password authentication, not key-pair authentication. To use [-k] with password authentication, it needs to install [sshpass] package. |
# run command to show [/etc/shadow] to [target_servers] group freebsd@dlp:~ $ ansible target_servers -k -m command -a "ls -l /root" -b --ask-become-pass SSH password: BECOME password[defaults to SSH password]: 10.0.0.51 | CHANGED | rc=0 >> total 58 drwx------ 3 root wheel 3 Sep 11 09:59 .ansible -rw-r--r-- 2 root wheel 1011 Nov 10 2023 .cshrc -rw-r--r-- 1 root wheel 66 Nov 10 2023 .k5login -rw------- 1 root wheel 20 Jul 18 09:40 .lesshst -rw-r--r-- 1 root wheel 316 Nov 10 2023 .login -rw-r--r-- 2 root wheel 495 Nov 10 2023 .profile -rw------- 1 root wheel 1031 Sep 11 10:13 .sh_history -rw-r--r-- 1 root wheel 1174 Nov 10 2023 .shrc drwx------ 2 root wheel 3 Sep 11 09:57 .ssh -rw------- 1 root wheel 7183 Sep 11 09:54 .viminfo 10.0.0.52 | CHANGED | rc=0 >> total 57 -rw-r--r-- 2 root wheel 1011 Nov 10 2023 .cshrc -rw-r--r-- 1 root wheel 66 Nov 10 2023 .k5login -rw------- 1 root wheel 20 Jul 18 09:40 .lesshst -rw-r--r-- 1 root wheel 316 Nov 10 2023 .login -rw-r--r-- 2 root wheel 495 Nov 10 2023 .profile -rw------- 1 root wheel 882 Aug 23 08:50 .sh_history -rw-r--r-- 1 root wheel 1174 Nov 10 2023 .shrc drwx------ 2 root wheel 3 Sep 11 09:57 .ssh -rw------- 1 root wheel 7183 Sep 11 09:54 .viminfo |
Sponsored Link |
|