Monday, July 11, 2022

Ansible ad hoc commands

 Without a playbook you can do some simple actions from the ad hoc commands like the below examples:


ansible all -i ansible/hosts/hosts.ini -m shell -a "/sbin/reboot"


ansible all -i ansible/hosts/hosts.ini -m shell -a "/sbin/reboot" -f 2    

# this will make the reboot in parallel and we can increase the number for more servers in the hosts file


ansible all -i ansible/hosts/hosts.ini -m shell -a "cat /etc/hosts /etc/hostname"


ansible all -i ansible/hosts/hosts.ini  -m shell -a "uptime" 


ansible client01 -i ansible/hosts/hosts.ini  -m shell -a "free -m"


ansible all -i ansible/hosts/hosts.ini -m service -a "name=ssh"


root@dcex1510ctrl001:~# ansible client01 -i ansible/hosts/hosts.ini -m shell -a "hostnamectl set-hostname dcex1510clnt001"

10.X.Y.Z | CHANGED | rc=0 >>


root@dcex1510ctrl001:~# ansible client02 -i ansible/hosts/hosts.ini -m shell -a "hostnamectl set-hostname dcex1510clnt002"

10.A.B.C | CHANGED | rc=0 >>



Ensure a service is started on client01 host:

ansible client01 -i ansible/hosts/hosts.ini -m ansible.builtin.service -a "name=ssh state=started"


Alternatively, restart a service on client01 host:

ansible client01 -i ansible/hosts/hosts.ini -m ansible.builtin.service -a "name=ssh state=restarted"


Ensure a service is stopped:

ansible client01 -i ansible/hosts/hosts.ini -m ansible.builtin.service -a "name=ssh state=stopped"



 in the next blog we will start to create some simple playbooks to do the following:

1. transfer a new hosts file to the clients 

2. change the client hostname

3. install some packages on the clients

No comments:

Post a Comment

Install Vault on ubuntu 20

 Today I will install vault on ubuntu 20 to use it as a part of my Ansible learning journey  Add GPG key curl -fsSL https://apt.releases.has...