Ad-hoc commands are one of the simplest ways of using Ansible. These are used when you want to issue some commands on a server or bunch of servers. The ad-hoc commands are not stored for future use, but it represents a fast way to interact with the desired servers.
The Ansible ad-hoc command uses the /usr/bin/ansible command-line tool to automate a single task on one or more managed nodes. The Ad-hoc commands are quick and easy, but they are not re-usable. The Ad-hoc commands demonstrate the simplicity and power of Ansible.
Syntax
Explanation
Hosts: It can be an entry in the inventory file. For specifying all hosts in the inventory, use all or "*".
module_name: It is an optional parameter. There are hundreds of modules available in the Ansible, such as shell, yum, apt, file, and copy. By default, it is the command.
Arguments: We should pass values that are required by the module. It can change according to the module used.
Username: It specifies the user account in which Ansible can execute commands.
Become: It's an optional parameter specified when we want to run operations that need sudo privilege. By default, it becomes false.
1. Parallelism and shell commands
You can reboot your company server in 12 parallel forks at the same time. For this, you need to set up the SSHagent for connection.
To run reboot for all your company servers in the group, 'abc', in 12 parallel forks:
By default, Ansible will run the above ad-hoc commands from the current user account. If you want to change then pass the username in ad-hoc command as follows:
2. File Transfer
You can use ad-hoc commands for doing SCP (secure copy protocol) which means lots of files in parallel on multiple machines or servers.
Transferring file on many machines or servers
Creating new directory
Deleting all directory and files
3. Managing Packages
Ad-hoc commands are available for apt and yum module. Here are the following ad-hoc commands using yum.
Below command checks, if the yum package is installed or not, but not update it.
Below command checks the package is not installed.
Alternatively, restart a service on all webservers:
Ensure a service is stopped:
6. Gathering Facts
Fact represents the discovered variables about a system. You can use the facts to implement conditional execution of tasks, and also used to get ad-hoc information about your systems. To see all the facts:
No comments:
Post a Comment