Ansible : Basic Usage2025/02/18 |
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 => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python3" }, "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, [cent] user runs Ansible. |
# run command to show [/etc/shadow] to [target_servers] group [cent@dlp ~]$ ansible target_servers -m command -a "cat /etc/shadow" -b --ask-become-pass BECOME password: 10.0.0.52 | CHANGED | rc=0 >> bin:*:18078:0:99999:7::: daemon:*:18078:0:99999:7::: adm:*:18078:0:99999:7::: ..... ..... 10.0.0.51 | CHANGED | rc=0 >> bin:*:18078:0:99999:7::: daemon:*:18078:0:99999:7::: adm:*:18078:0:99999:7::: ..... ..... |
Sponsored Link |
|