Ansible : Basic Usage2025/10/14 |
|
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, [debian] 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 debian@dlp:~$ ansible target_servers -k -m command -a "cat /etc/shadow" -b --ask-become-pass SSH password: BECOME password[defaults to SSH password]: 10.0.0.51 | CHANGED | rc=0 >> daemon:*:20310:0:99999:7::: bin:*:20310:0:99999:7::: sys:*:20310:0:99999:7::: ..... ..... 10.0.0.52 | CHANGED | rc=0 >> daemon:*:20310:0:99999:7::: bin:*:20310:0:99999:7::: sys:*:20310:0:99999:7::: ..... ..... |
| Sponsored Link |
|
|