2018-03-14 19:44:21 +00:00
.. _intro_adhoc:
2013-09-29 23:10:28 +00:00
Introduction To Ad-Hoc Commands
===============================
2012-03-09 12:42:53 +00:00
2013-12-26 19:32:01 +00:00
.. contents :: Topics
2012-08-07 02:13:58 +00:00
The following examples show how to use `/usr/bin/ansible` for running
2013-10-03 01:41:46 +00:00
ad hoc tasks.
What's an ad-hoc command?
An ad-hoc command is something that you might type in to do something really
2016-01-29 22:46:18 +00:00
quick, but don't want to save for later.
2013-09-29 18:54:53 +00:00
2013-10-07 03:22:09 +00:00
This is a good place to start to understand the basics of what Ansible can do
2013-10-03 01:41:46 +00:00
prior to learning the playbooks language -- ad-hoc commands can also be used
2016-01-29 22:46:18 +00:00
to do quick things that you might not necessarily want to write a full playbook for.
2013-09-29 18:54:53 +00:00
2013-10-03 01:41:46 +00:00
Generally speaking, the true power of Ansible lies in playbooks.
2013-09-29 18:54:53 +00:00
Why would you use ad-hoc tasks versus playbooks?
2012-03-07 16:35:18 +00:00
2013-09-29 18:54:53 +00:00
For instance, if you wanted to power off all of your lab for Christmas vacation,
you could execute a quick one-liner in Ansible without writing a playbook.
2013-10-03 01:41:46 +00:00
For configuration management and deployments, though, you'll want to pick up on
2016-01-29 22:46:18 +00:00
using '/usr/bin/ansible-playbook' -- the concepts you will learn here will
2013-10-03 01:41:46 +00:00
port over directly to the playbook language.
2012-03-22 05:14:26 +00:00
(See :doc: `playbooks` for more information about those)
2012-03-08 18:36:47 +00:00
2013-10-03 01:49:30 +00:00
If you haven't read :doc: `intro_inventory` already, please look that over a bit first
2013-10-03 01:41:46 +00:00
and then we'll get going.
2013-10-04 22:14:54 +00:00
.. _parallelism_and_shell_commands:
2012-03-08 18:36:47 +00:00
Parallelism and Shell Commands
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2013-10-03 01:41:46 +00:00
Arbitrary example.
2013-10-07 03:22:09 +00:00
Let's use Ansible's command line tool to reboot all web servers in Atlanta, 10 at a time. First, let's
2012-03-22 05:14:26 +00:00
set up SSH-agent so it can remember our credentials::
2012-03-08 18:36:47 +00:00
2012-08-07 02:13:58 +00:00
$ ssh-agent bash
2013-01-12 14:54:21 +00:00
$ ssh-add ~/.ssh/id_rsa
2012-03-08 18:36:47 +00:00
2012-08-07 02:13:58 +00:00
If you don't want to use ssh-agent and want to instead SSH with a
password instead of keys, you can with `` --ask-pass `` (`` -k `` ), but
it's much better to just use ssh-agent.
2012-05-10 05:04:26 +00:00
2012-08-07 02:13:58 +00:00
Now to run the command on all servers in a group, in this case,
*atlanta* , in 10 parallel forks::
2012-03-22 05:14:26 +00:00
2012-08-07 02:13:58 +00:00
$ ansible atlanta -a "/sbin/reboot" -f 10
2012-03-08 18:36:47 +00:00
2013-09-29 18:54:53 +00:00
/usr/bin/ansible will default to running from your user account. If you do not like this
2013-10-03 01:41:46 +00:00
behavior, pass in "-u username". If you want to run commands as a different user, it looks like this::
2012-08-15 02:20:47 +00:00
$ ansible atlanta -a "/usr/bin/foo" -u username
2012-03-31 02:10:32 +00:00
2016-01-29 22:46:18 +00:00
Often you'll not want to just do things from your user account. If you want to run commands through privilege escalation::
2012-04-14 12:42:24 +00:00
2016-01-29 22:46:18 +00:00
$ ansible atlanta -a "/usr/bin/foo" -u username --become [--ask-become-pass]
2012-08-07 02:13:58 +00:00
2016-01-29 22:46:18 +00:00
Use `` --ask-become-pass `` (`` -K `` ) if you are not using a passwordless privilege escalation method (sudo/su/pfexec/doas/etc).
This will interactively prompt you for the password to use.
Use of a passwordless setup makes things easier to automate, but it's not required.
2012-05-10 05:04:26 +00:00
2016-01-29 22:46:18 +00:00
It is also possible to become a user other than root using
`` --become-user `` ::
2012-05-10 05:04:26 +00:00
2017-10-28 08:00:10 +00:00
$ ansible atlanta -a "/usr/bin/foo" -u username --become --become-user otheruser [--ask-become-pass]
2012-03-31 02:10:32 +00:00
2014-02-15 19:21:08 +00:00
.. note ::
2016-01-29 22:46:18 +00:00
Rarely, some users have security rules where they constrain their sudo/pbrun/doas environment to running specific command paths only.
2014-02-15 19:21:08 +00:00
This does not work with ansible's no-bootstrapping philosophy and hundreds of different modules.
2016-01-29 22:46:18 +00:00
If doing this, use Ansible from a special account that does not have this constraint.
2018-03-14 19:44:21 +00:00
One way of doing this without sharing access to unauthorized users would be gating Ansible with :ref: `ansible_tower` , which
2014-02-15 19:21:08 +00:00
can hold on to an SSH credential and let members of certain organizations use it on their behalf without having direct access.
2018-03-14 19:44:21 +00:00
Ok, so those are basics. If you didn't read about patterns and groups yet, go back and read :ref: `intro_patterns` .
2012-03-08 18:36:47 +00:00
2012-08-07 02:13:58 +00:00
The `` -f 10 `` in the above specifies the usage of 10 simultaneous
2018-03-14 19:44:21 +00:00
processes to use. You can also set this in :ref: `intro_configuration` to avoid setting it again. The default is actually 5, which
2013-10-03 01:41:46 +00:00
is really small and conservative. You are probably going to want to talk to a lot more simultaneous hosts so feel free
to crank this up. If you have more hosts than the value set for the fork count, Ansible will talk to them, but it will
2016-01-15 21:11:17 +00:00
take a little longer. Feel free to push this value as high as your system can handle!
2013-09-29 18:54:53 +00:00
2013-10-07 03:22:09 +00:00
You can also select what Ansible "module" you want to run. Normally commands also take a `` -m `` for module name, but
2012-10-04 03:31:19 +00:00
the default module name is 'command', so we didn't need to
2012-08-07 02:13:58 +00:00
specify that all of the time. We'll use `` -m `` in later examples to
run some other :doc: `modules` .
2012-03-09 19:39:29 +00:00
2012-08-07 02:13:58 +00:00
.. note ::
2018-04-18 16:14:26 +00:00
The :ref: `command module <command_module>` does not support extended shell syntax like piping and
2018-04-18 01:45:07 +00: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 14:50:47 +00:00
2018-04-18 16:14:26 +00:00
Using the :ref: `shell module <shell_module>` looks like this::
2012-03-31 14:50:47 +00:00
2012-08-07 02:13:58 +00:00
$ ansible raleigh -m shell -a 'echo $TERM'
2012-03-22 05:14:26 +00:00
2013-10-07 03:22:09 +00:00
When running any command with the Ansible *ad hoc* CLI (as opposed to
2013-10-20 22:54:42 +00:00
:doc: `Playbooks <playbooks>` ), pay particular attention to shell quoting rules, so
2013-09-29 18:54:53 +00:00
the local shell doesn't eat a variable before it gets passed to Ansible.
2015-04-18 09:47:02 +00:00
For example, using double rather than single quotes in the above example would
2012-08-07 02:13:58 +00:00
evaluate the variable on the box you were on.
2016-12-01 22:32:55 +00:00
So far we've been demoing simple command execution, but most Ansible modules are not simple imperative scripts. Instead, they use a declarative model,
calculating and executing the actions required to reach a specified final state.
Furthermore, they achieve a form of idempotence by checking the current state
before they begin, and if the current state matches the specified final state,
doing nothing.
However, we also recognize that running arbitrary commands can be valuable, so Ansible easily supports both.
2012-03-31 14:50:47 +00:00
2013-10-04 22:14:54 +00:00
.. _file_transfer:
2012-03-08 18:36:47 +00:00
2012-08-17 01:12:36 +00:00
File Transfer
`` ` ` ` ` ` ` ` ` ` ``
2012-03-08 18:36:47 +00:00
2012-08-17 01:12:36 +00:00
Here's another use case for the `/usr/bin/ansible` command line. Ansible can SCP lots of files to multiple machines in parallel.
2012-03-08 18:36:47 +00:00
2014-05-03 15:59:50 +00:00
To transfer a file directly to many servers::
2012-03-08 18:36:47 +00:00
2012-08-07 02:13:58 +00:00
$ ansible atlanta -m copy -a "src=/etc/hosts dest=/tmp/hosts"
2012-03-08 18:36:47 +00:00
2012-08-17 01:12:36 +00:00
If you use playbooks, you can also take advantage of the `` template `` module,
which takes this another step further. (See module and playbook documentation).
2012-04-19 03:02:28 +00:00
2012-08-07 02:13:58 +00:00
The `` file `` module allows changing ownership and permissions on files. These
2012-08-17 01:12:36 +00:00
same options can be passed directly to the `` copy `` module as well::
2012-03-16 02:57:10 +00:00
2012-08-07 02:13:58 +00:00
$ ansible webservers -m file -a "dest=/srv/foo/a.txt mode=600"
$ ansible webservers -m file -a "dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"
2012-03-16 02:57:10 +00:00
2012-08-07 02:13:58 +00:00
The `` file `` module can also create directories, similar to `` mkdir -p `` ::
2014-02-20 06:04:48 +00:00
$ ansible webservers -m file -a "dest=/path/to/c mode=755 owner=mdehaan group=mdehaan state=directory"
2012-03-16 02:57:10 +00:00
As well as delete directories (recursively) and delete files::
2012-08-07 02:13:58 +00:00
$ ansible webservers -m file -a "dest=/path/to/c state=absent"
2012-03-16 02:57:10 +00:00
2013-10-04 22:14:54 +00:00
.. _managing_packages:
2012-03-08 18:36:47 +00:00
2012-03-10 16:35:36 +00:00
Managing Packages
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2012-08-07 02:13:58 +00:00
There are modules available for yum and apt. Here are some examples
2012-10-04 03:31:19 +00:00
with yum.
2012-03-27 01:41:50 +00:00
2012-03-10 16:35:36 +00:00
Ensure a package is installed, but don't update it::
2012-08-07 02:13:58 +00:00
2014-10-29 04:59:46 +00:00
$ ansible webservers -m yum -a "name=acme state=present"
2012-03-10 16:35:36 +00:00
Ensure a package is installed to a specific version::
2014-10-29 04:59:46 +00:00
$ ansible webservers -m yum -a "name=acme-1.5 state=present"
2012-03-10 16:35:36 +00:00
2012-03-10 16:40:49 +00:00
Ensure a package is at the latest version::
2013-02-05 21:31:51 +00:00
$ ansible webservers -m yum -a "name=acme state=latest"
2012-03-10 16:40:49 +00:00
2012-03-31 14:40:01 +00:00
Ensure a package is not installed::
2012-08-07 02:13:58 +00:00
2014-10-29 04:59:46 +00:00
$ ansible webservers -m yum -a "name=acme state=absent"
2012-03-10 16:35:36 +00:00
2016-03-25 23:35:04 +00:00
Ansible has modules for managing packages under many platforms. If there isn't
a module for your package manager, you can install packages using the
command module or (better!) contribute a module for your package manager.
Stop by the mailing list for info/details.
2012-03-10 16:35:36 +00:00
2013-10-04 22:14:54 +00:00
.. _users_and_groups:
2012-03-22 05:01:02 +00:00
Users and Groups
`` ` ` ` ` ` ` ` ` ` ` ` ` ``
2012-10-04 03:31:19 +00:00
The 'user' module allows easy creation and manipulation of
2012-08-07 02:13:58 +00:00
existing user accounts, as well as removal of user accounts that may
exist::
2012-03-22 05:01:02 +00:00
2012-08-07 02:13:58 +00:00
$ ansible all -m user -a "name=foo password=<crypted password here>"
2012-03-22 05:01:02 +00:00
2012-08-07 02:13:58 +00:00
$ ansible all -m user -a "name=foo state=absent"
2012-03-22 05:01:02 +00:00
2012-04-05 00:33:00 +00:00
See the :doc: `modules` section for details on all of the available options, including
how to manipulate groups and group membership.
2012-03-22 05:01:02 +00:00
2013-10-04 22:14:54 +00:00
.. _from_source_control:
2012-03-09 03:50:00 +00:00
Deploying From Source Control
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2012-03-08 18:36:47 +00:00
Deploy your webapp straight from git::
2017-10-03 19:16:25 +00:00
$ ansible webservers -m git -a "repo=https://foo.example.org/repo.git dest=/srv/myapp version=HEAD"
2012-03-08 18:36:47 +00:00
2013-10-07 03:22:09 +00:00
Since Ansible modules can notify change handlers it is possible to
tell Ansible to run specific tasks when the code is updated, such as
2012-08-07 02:13:58 +00:00
deploying Perl/Python/PHP/Ruby directly from git and then restarting
apache.
2012-03-08 18:36:47 +00:00
2013-10-04 22:14:54 +00:00
.. _managing_services:
2012-03-09 03:50:00 +00:00
Managing Services
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
Ensure a service is started on all webservers::
2012-08-07 02:13:58 +00:00
$ ansible webservers -m service -a "name=httpd state=started"
2012-03-09 03:50:00 +00:00
Alternatively, restart a service on all webservers::
2012-08-07 02:13:58 +00:00
$ ansible webservers -m service -a "name=httpd state=restarted"
2012-03-10 16:35:36 +00:00
Ensure a service is stopped::
2012-08-07 02:13:58 +00:00
$ ansible webservers -m service -a "name=httpd state=stopped"
2012-03-09 03:50:00 +00:00
2013-10-04 22:14:54 +00:00
.. _time_limited_background_operations:
2012-03-09 12:42:53 +00:00
Time Limited Background Operations
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2016-03-25 23:35:04 +00:00
Long running operations can be run in the background, and it is possible to
2016-08-18 22:45:59 +00:00
check their status later. For example, to execute `` long_running_operation ``
2016-03-25 23:35:04 +00:00
asynchronously in the background, with a timeout of 3600 seconds (`` -B `` ),
2016-08-18 22:45:59 +00:00
and without polling (`` -P `` )::
2012-03-09 12:42:53 +00:00
2014-12-07 02:24:50 +00:00
$ ansible all -B 3600 -P 0 -a "/usr/bin/long_running_operation --do-stuff"
2012-03-12 01:09:21 +00:00
2014-12-09 02:52:03 +00:00
If you do decide you want to check on the job status later, you can use the
async_status module, passing it the job id that was returned when you ran
the original job in the background::
2012-03-12 01:09:21 +00:00
2014-12-09 02:52:03 +00:00
$ ansible web1.example.com -m async_status -a "jid=488359678239.2844"
2012-03-09 12:42:53 +00:00
2012-03-12 01:09:21 +00:00
Polling is built-in and looks like this::
2012-08-07 02:13:58 +00:00
$ ansible all -B 1800 -P 60 -a "/usr/bin/long_running_operation --do-stuff"
2016-03-25 23:35:04 +00:00
The above example says "run for 30 minutes max (`` -B `` 30*60=1800),
2012-08-07 02:13:58 +00:00
poll for status (`` -P `` ) every 60 seconds".
2012-03-31 14:41:29 +00:00
2012-03-12 01:09:21 +00:00
Poll mode is smart so all jobs will be started before polling will begin on any machine.
2012-08-07 02:13:58 +00:00
Be sure to use a high enough `` --forks `` value if you want to get all of your jobs started
2012-03-12 03:20:55 +00:00
very quickly. After the time limit (in seconds) runs out (`` -B `` ), the process on
the remote nodes will be terminated.
2012-03-12 01:09:21 +00:00
2014-03-07 09:34:44 +00:00
Typically you'll only be backgrounding long-running
2016-01-15 21:11:17 +00:00
shell commands or software upgrades. Backgrounding the copy module does not do a background file transfer. :doc: `Playbooks <playbooks>` also support polling, and have a simplified syntax for this.
2012-03-09 12:42:53 +00:00
2013-10-04 22:14:54 +00:00
.. _checking_facts:
2013-05-28 16:23:12 +00:00
Gathering Facts
`` ` ` ` ` ` ` ` ` ` ` ` ``
2013-05-29 22:27:16 +00:00
Facts are described in the playbooks section and represent discovered variables about a
system. These can be used to implement conditional execution of tasks but also just to get ad-hoc information about your system. You can see all facts via::
2013-05-28 16:23:12 +00:00
2013-05-28 16:32:14 +00:00
$ ansible all -m setup
2013-05-28 16:23:12 +00:00
2014-12-29 02:35:30 +00:00
It's also possible to filter this output to just export certain facts, see the "setup" module documentation for details.
2013-05-28 16:23:12 +00:00
2013-10-20 22:54:42 +00:00
Read more about facts at :doc: `playbooks_variables` once you're ready to read up on :doc: `Playbooks <playbooks>` .
2012-08-17 01:12:36 +00:00
2012-03-31 13:29:31 +00:00
.. seealso ::
2018-03-14 19:44:21 +00:00
:ref: `intro_configuration`
2013-10-07 03:22:09 +00:00
All about the Ansible config file
2018-03-14 19:44:21 +00:00
:ref: `all_modules`
2012-03-31 13:29:31 +00:00
A list of available modules
2018-03-14 19:44:21 +00:00
:ref: `working_with_playbooks`
2013-10-07 03:22:09 +00:00
Using Ansible for configuration management & deployment
2018-07-21 13:48:47 +00:00
`Mailing List <https://groups.google.com/group/ansible-project> `_
2012-03-31 13:55:37 +00:00
Questions? Help? Ideas? Stop by the list on Google Groups
`irc.freenode.net <http://irc.freenode.net> `_
#ansible IRC chat channel