2018-03-14 20:44:21 +01:00
.. _intro_adhoc:
2019-10-08 19:46:38 +02:00
***** ***** ***** ***** ***** ***** *
Introduction to ad-hoc commands
***** ***** ***** ***** ***** ***** *
2012-03-09 13:42:53 +01:00
2020-02-21 11:56:25 +01:00
An Ansible ad-hoc command uses the `/usr/bin/ansible` command-line tool to automate a single task on one or more managed nodes. Ad-hoc commands are quick and easy, but they are not reusable. So why learn about ad-hoc commands first? Ad-hoc commands demonstrate the simplicity and power of Ansible. The concepts you learn here will port over directly to the playbook language. Before reading and executing these examples, please read :ref: `intro_inventory` .
2013-12-26 20:32:01 +01:00
2019-10-08 19:46:38 +02:00
.. contents ::
:local:
2013-10-03 03:41:46 +02:00
2019-10-08 19:46:38 +02:00
Why use ad-hoc commands?
========================
2013-10-03 03:41:46 +02:00
2019-10-08 19:46:38 +02:00
Ad-hoc commands are great for tasks you repeat rarely. For example, if you want to power off all the machines in your lab for Christmas vacation, you could execute a quick one-liner in Ansible without writing a playbook. An ad-hoc command looks like this:
2013-09-29 20:54:53 +02:00
2019-10-08 19:46:38 +02:00
.. code-block :: bash
2013-09-29 20:54:53 +02:00
2019-10-08 19:46:38 +02:00
$ ansible [pattern] -m [module] -a "[module options]"
2012-03-07 17:35:18 +01:00
2019-10-08 19:46:38 +02:00
You can learn more about :ref: `patterns<intro_patterns>` and :ref: `modules<working_with_modules>` on other pages.
2013-09-29 20:54:53 +02:00
2019-10-08 19:46:38 +02:00
Use cases for ad-hoc tasks
==========================
2013-10-03 03:41:46 +02:00
2019-10-08 19:46:38 +02:00
Ad-hoc tasks can be used to reboot servers, copy files, manage packages and users, and much more. You can use any Ansible module in an ad-hoc task. Ad-hoc tasks, like playbooks, use a declarative model,
calculating and executing the actions required to reach a specified final state. They
achieve a form of idempotence by checking the current state before they begin and doing nothing unless the current state is different from the specified final state.
2012-03-08 19:36:47 +01:00
2019-10-08 19:46:38 +02:00
Rebooting servers
-----------------
2013-10-03 03:41:46 +02:00
2020-09-24 19:16:15 +02:00
The default module for the `` ansible `` command-line utility is the :ref: `ansible.builtin.command module<command_module>` . You can use an ad-hoc task to call the command module and reboot all web servers in Atlanta, 10 at a time. Before Ansible can do this, you must have all servers in Atlanta listed in a group called [atlanta] in your inventory, and you must have working SSH credentials for each machine in that group. To reboot all the servers in the [atlanta] group:
2013-10-05 00:14:54 +02:00
2019-10-08 19:46:38 +02:00
.. code-block :: bash
2012-03-08 19:36:47 +01:00
2019-10-08 19:46:38 +02:00
$ ansible atlanta -a "/sbin/reboot"
2013-10-03 03:41:46 +02:00
2019-10-08 19:46:38 +02:00
By default Ansible uses only 5 simultaneous processes. If you have more hosts than the value set for the fork count, Ansible will talk to them, but it will take a little longer. To reboot the [atlanta] servers with 10 parallel forks:
2012-03-08 19:36:47 +01:00
2019-10-08 19:46:38 +02:00
.. code-block :: bash
2012-03-22 06:14:26 +01:00
2012-08-07 04:13:58 +02:00
$ ansible atlanta -a "/sbin/reboot" -f 10
2012-03-08 19:36:47 +01:00
2019-10-08 19:46:38 +02:00
/usr/bin/ansible will default to running from your user account. To connect as a different user:
2012-08-15 04:20:47 +02:00
2019-10-08 19:46:38 +02:00
.. code-block :: bash
2012-03-31 04:10:32 +02:00
2019-10-08 19:46:38 +02:00
$ ansible atlanta -a "/sbin/reboot" -f 10 -u username
2012-04-14 14:42:24 +02:00
2019-10-08 19:46:38 +02:00
Rebooting probably requires privilege escalation. You can connect to the server as `` username `` and run the command as the `` root `` user by using the :ref: `become <become>` keyword:
2012-08-07 04:13:58 +02:00
2019-10-08 19:46:38 +02:00
.. code-block :: bash
2012-05-10 07:04:26 +02:00
2019-10-08 19:46:38 +02:00
$ ansible atlanta -a "/sbin/reboot" -f 10 -u username --become [--ask-become-pass]
2012-05-10 07:04:26 +02:00
2019-10-08 19:46:38 +02:00
If you add `` --ask-become-pass `` or `` -K `` , Ansible prompts you for the password to use for privilege escalation (sudo/su/pfexec/doas/etc).
2012-03-09 20:39:29 +01:00
2012-08-07 04:13:58 +02:00
.. note ::
2018-04-18 18:14:26 +02:00
The :ref: `command module <command_module>` does not support extended shell syntax like piping and
2018-04-18 03:45:07 +02:00
redirects (although shell variables will always work). If your command requires shell-specific
syntax, use the `shell` module instead. Read more about the differences on the
:ref: `working_with_modules` page.
2012-03-31 16:50:47 +02:00
2020-09-24 19:16:15 +02:00
So far all our examples have used the default 'command' module. To use a different module, pass `` -m `` for module name. For example, to use the :ref: `ansible.builtin.shell module <shell_module>` :
2019-10-08 19:46:38 +02:00
.. code-block :: bash
2012-03-31 16:50:47 +02:00
2020-09-24 19:16:15 +02:00
$ ansible raleigh -m ansible.builtin.shell -a 'echo $TERM'
2012-03-22 06:14:26 +01:00
2013-10-07 05:22:09 +02:00
When running any command with the Ansible *ad hoc* CLI (as opposed to
2019-06-26 23:07:27 +02:00
:ref: `Playbooks <working_with_playbooks>` ), pay particular attention to shell quoting rules, so
2019-10-08 19:46:38 +02:00
the local shell retains the variable and passes it to Ansible.
2015-04-18 11:47:02 +02:00
For example, using double rather than single quotes in the above example would
2012-08-07 04:13:58 +02:00
evaluate the variable on the box you were on.
2013-10-05 00:14:54 +02:00
.. _file_transfer:
2012-03-08 19:36:47 +01:00
2019-10-08 19:46:38 +02:00
Managing files
--------------
2012-03-08 19:36:47 +01:00
2019-10-08 19:46:38 +02:00
An ad-hoc task can harness the power of Ansible and SCP to transfer many files to multiple machines in parallel. To transfer a file directly to all servers in the [atlanta] group:
2012-03-08 19:36:47 +01:00
2019-10-08 19:46:38 +02:00
.. code-block :: bash
2012-03-08 19:36:47 +01:00
2020-09-24 19:16:15 +02:00
$ ansible atlanta -m ansible.builtin.copy -a "src=/etc/hosts dest=/tmp/hosts"
2012-03-08 19:36:47 +01:00
2020-09-24 19:16:15 +02:00
If you plan to repeat a task like this, use the :ref: `ansible.builtin.template<template_module>` module in a playbook.
2019-10-08 19:46:38 +02:00
2020-09-24 19:16:15 +02:00
The :ref: `ansible.builtin.file<file_module>` module allows changing ownership and permissions on files. These
2019-10-08 19:46:38 +02:00
same options can be passed directly to the `` copy `` module as well:
2012-04-19 05:02:28 +02:00
2019-10-08 19:46:38 +02:00
.. code-block :: bash
2012-03-16 03:57:10 +01:00
2020-09-24 19:16:15 +02:00
$ ansible webservers -m ansible.builtin.file -a "dest=/srv/foo/a.txt mode=600"
$ ansible webservers -m ansible.builtin.file -a "dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"
2012-03-16 03:57:10 +01:00
2019-10-08 19:46:38 +02:00
The `` file `` module can also create directories, similar to `` mkdir -p `` :
.. code-block :: bash
2012-08-07 04:13:58 +02:00
2020-09-24 19:16:15 +02:00
$ ansible webservers -m ansible.builtin.file -a "dest=/path/to/c mode=755 owner=mdehaan group=mdehaan state=directory"
2012-03-16 03:57:10 +01:00
2019-10-08 19:46:38 +02:00
As well as delete directories (recursively) and delete files:
.. code-block :: bash
2012-08-07 04:13:58 +02:00
2020-09-24 19:16:15 +02:00
$ ansible webservers -m ansible.builtin.file -a "dest=/path/to/c state=absent"
2012-03-16 03:57:10 +01:00
2013-10-05 00:14:54 +02:00
.. _managing_packages:
2012-03-08 19:36:47 +01:00
2019-10-08 19:46:38 +02:00
Managing packages
-----------------
2012-03-10 17:35:36 +01:00
2019-10-08 19:46:38 +02:00
You might also use an ad-hoc task to install, update, or remove packages on managed nodes using a package management module like yum. To ensure a package is installed without updating it:
2012-03-27 03:41:50 +02:00
2019-10-08 19:46:38 +02:00
.. code-block :: bash
2012-08-07 04:13:58 +02:00
2020-09-24 19:16:15 +02:00
$ ansible webservers -m ansible.builtin.yum -a "name=acme state=present"
2012-03-10 17:35:36 +01:00
2019-10-08 19:46:38 +02:00
To ensure a specific version of a package is installed:
.. code-block :: bash
2012-03-10 17:35:36 +01:00
2020-09-24 19:16:15 +02:00
$ ansible webservers -m ansible.builtin.yum -a "name=acme-1.5 state=present"
2012-03-10 17:35:36 +01:00
2019-10-08 19:46:38 +02:00
To ensure a package is at the latest version:
.. code-block :: bash
2012-03-10 17:40:49 +01:00
2020-09-24 19:16:15 +02:00
$ ansible webservers -m ansible.builtin.yum -a "name=acme state=latest"
2012-03-10 17:40:49 +01:00
2019-10-08 19:46:38 +02:00
To ensure a package is not installed:
.. code-block :: bash
2012-08-07 04:13:58 +02:00
2020-09-24 19:16:15 +02:00
$ ansible webservers -m ansible.builtin.yum -a "name=acme state=absent"
2012-03-10 17:35:36 +01:00
2019-10-08 19:46:38 +02:00
Ansible has modules for managing packages under many platforms. If there is no module for your package manager, you can install packages using the command module or create a module for your package manager.
2012-03-10 17:35:36 +01:00
2013-10-05 00:14:54 +02:00
.. _users_and_groups:
2019-10-08 19:46:38 +02:00
Managing users and groups
-------------------------
2012-03-22 06:01:02 +01:00
2019-10-08 19:46:38 +02:00
You can create, manage, and remove user accounts on your managed nodes with ad-hoc tasks:
.. code-block :: bash
2012-03-22 06:01:02 +01:00
2020-09-24 19:16:15 +02:00
$ ansible all -m ansible.builtin.user -a "name=foo password=<crypted password here>"
2012-03-22 06:01:02 +01:00
2020-09-24 19:16:15 +02:00
$ ansible all -m ansible.builtin.user -a "name=foo state=absent"
2012-03-22 06:01:02 +01:00
2020-09-24 19:16:15 +02:00
See the :ref: `ansible.builtin.user <user_module>` module documentation for details on all of the available options, including
2012-04-05 02:33:00 +02:00
how to manipulate groups and group membership.
2012-03-22 06:01:02 +01:00
2013-10-05 00:14:54 +02:00
.. _managing_services:
2019-10-08 19:46:38 +02:00
Managing services
-----------------
2012-03-09 04:50:00 +01:00
2019-10-08 19:46:38 +02:00
Ensure a service is started on all webservers:
2012-03-10 17:35:36 +01:00
2019-10-08 19:46:38 +02:00
.. code-block :: bash
2012-03-10 17:35:36 +01:00
2020-09-24 19:16:15 +02:00
$ ansible webservers -m ansible.builtin.service -a "name=httpd state=started"
2012-03-12 02:09:21 +01:00
2019-10-08 19:46:38 +02:00
Alternatively, restart a service on all webservers:
2012-03-12 02:09:21 +01:00
2019-10-08 19:46:38 +02:00
.. code-block :: bash
2012-03-09 13:42:53 +01:00
2020-09-24 19:16:15 +02:00
$ ansible webservers -m ansible.builtin.service -a "name=httpd state=restarted"
2012-03-12 02:09:21 +01:00
2019-10-08 19:46:38 +02:00
Ensure a service is stopped:
2012-08-07 04:13:58 +02:00
2019-10-08 19:46:38 +02:00
.. code-block :: bash
2012-03-31 16:41:29 +02:00
2020-09-24 19:16:15 +02:00
$ ansible webservers -m ansible.builtin.service -a "name=httpd state=stopped"
2012-03-12 02:09:21 +01:00
2019-10-08 19:46:38 +02:00
.. _gathering_facts:
2012-03-09 13:42:53 +01:00
2019-10-08 19:46:38 +02:00
Gathering facts
---------------
2013-10-05 00:14:54 +02:00
2019-10-08 19:46:38 +02:00
Facts represent discovered variables about a system. You can use facts to implement conditional execution of tasks but also just to get ad-hoc information about your systems. To see all facts:
2013-05-28 18:23:12 +02:00
2019-10-08 19:46:38 +02:00
.. code-block :: bash
2013-05-28 18:23:12 +02:00
2020-09-24 19:16:15 +02:00
$ ansible all -m ansible.builtin.setup
2013-05-28 18:23:12 +02:00
2020-09-24 19:16:15 +02:00
You can also filter this output to display only certain facts, see the :ref: `ansible.builtin.setup <setup_module>` module documentation for details.
2013-05-28 18:23:12 +02:00
2019-10-08 19:46:38 +02:00
Now that you understand the basic elements of Ansible execution, you are ready to learn to automate repetitive tasks using :ref: `Ansible Playbooks <playbooks_intro>` .
2012-08-17 03:12:36 +02:00
2012-03-31 15:29:31 +02:00
.. seealso ::
2018-03-14 20:44:21 +01:00
:ref: `intro_configuration`
2013-10-07 05:22:09 +02:00
All about the Ansible config file
2020-09-09 18:43:40 +02:00
:ref: `list_of_collections`
Browse existing collections, modules, and plugins
2018-03-14 20:44:21 +01:00
:ref: `working_with_playbooks`
2013-10-07 05:22:09 +02:00
Using Ansible for configuration management & deployment
2018-07-21 15:48:47 +02:00
`Mailing List <https://groups.google.com/group/ansible-project> `_
2012-03-31 15:55:37 +02:00
Questions? Help? Ideas? Stop by the list on Google Groups
`irc.freenode.net <http://irc.freenode.net> `_
#ansible IRC chat channel