How to run basic ansible commands?
-
This is to show how you can run some basic adhoc ansible commands.
Determine the user(s) you want to use to run ansible, for this example one user is using ansible.
Prerequisites are
- You have your ansible control server installed and configured
- You know the user being used to run ansible commands, with or without sudo access
For this user(s), setup ssh authorization on all hosts you will be that are being accessed. You can use a user and password as well.
If there are no ssh keys generated you can create them using ssh-keygen command, and then copy the public ssh key to each remote user and host's authorized_keys file in the /home/<username>/.ssh directory.
$ ssh-keygen -t rsa -b 2028
Ensure you are able to run remote commands from the ansible control server.
$ ssh <username>@<remote host> who
Next create a host inventory file
If changing the default /etc/ansible/hosts location use the ANSIBLE_HOSTS variable
On the command line refer to it with -i (abbrev) or --inventory-file option
Create the file where you create all the other ansible files, under the ansible user for example, create a directory called ansible and create it there.
You can do some clever things with the host inventory file, that for another article. For now list all the remote server in the file each on a new line.
Try out some adhoc commands
$ ansible webservers -i $ANSIBLE_HOSTS -m ping ip-xxxx.us-west-2.compute.internal | SUCCESS => { "changed": false, "ping": "pong" }
Or for just everything leave the inventory out like this
$ ansible all -m ping ip-xxxx.us-west-2.compute.internal | SUCCESS => { "changed": false, "ping": "pong" }
Some remote shell commands using the -a option and the shell module
$ ansible all -m shell -a 'who' ip-xxxx.us-west-2.compute.internal | SUCCESS | rc=0 >> rhlab1 pts/0 2016-04-21 10:02 (x) rhlab1 pts/1 2016-04-21 08:02 (y) rhlab1 pts/2 2016-04-21 08:44 (y)
$ ansible all -m shell -a 'uptime' ip-xxx.us-west-2.compute.internal | SUCCESS | rc=0 >> 10:09:02 up 2:44, 3 users, load average: 0.00, 0.01, 0.05
© Lightnetics 2024