mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
281 lines
11 KiB
ReStructuredText
281 lines
11 KiB
ReStructuredText
11 years ago
|
Introduction To Ad-Hoc Commands
|
||
|
===============================
|
||
13 years ago
|
|
||
11 years ago
|
.. contents:: Topics
|
||
|
|
||
13 years ago
|
.. highlight:: bash
|
||
|
|
||
|
The following examples show how to use `/usr/bin/ansible` for running
|
||
11 years ago
|
ad hoc tasks.
|
||
|
|
||
|
What's an ad-hoc command?
|
||
|
|
||
|
An ad-hoc command is something that you might type in to do something really
|
||
9 years ago
|
quick, but don't want to save for later.
|
||
11 years ago
|
|
||
11 years ago
|
This is a good place to start to understand the basics of what Ansible can do
|
||
11 years ago
|
prior to learning the playbooks language -- ad-hoc commands can also be used
|
||
9 years ago
|
to do quick things that you might not necessarily want to write a full playbook for.
|
||
11 years ago
|
|
||
11 years ago
|
Generally speaking, the true power of Ansible lies in playbooks.
|
||
11 years ago
|
Why would you use ad-hoc tasks versus playbooks?
|
||
13 years ago
|
|
||
11 years ago
|
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.
|
||
|
|
||
11 years ago
|
For configuration management and deployments, though, you'll want to pick up on
|
||
9 years ago
|
using '/usr/bin/ansible-playbook' -- the concepts you will learn here will
|
||
11 years ago
|
port over directly to the playbook language.
|
||
|
|
||
13 years ago
|
(See :doc:`playbooks` for more information about those)
|
||
13 years ago
|
|
||
11 years ago
|
If you haven't read :doc:`intro_inventory` already, please look that over a bit first
|
||
11 years ago
|
and then we'll get going.
|
||
|
|
||
11 years ago
|
.. _parallelism_and_shell_commands:
|
||
|
|
||
13 years ago
|
Parallelism and Shell Commands
|
||
|
``````````````````````````````
|
||
|
|
||
11 years ago
|
Arbitrary example.
|
||
|
|
||
11 years ago
|
Let's use Ansible's command line tool to reboot all web servers in Atlanta, 10 at a time. First, let's
|
||
13 years ago
|
set up SSH-agent so it can remember our credentials::
|
||
13 years ago
|
|
||
13 years ago
|
$ ssh-agent bash
|
||
12 years ago
|
$ ssh-add ~/.ssh/id_rsa
|
||
13 years ago
|
|
||
13 years ago
|
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.
|
||
13 years ago
|
|
||
13 years ago
|
Now to run the command on all servers in a group, in this case,
|
||
|
*atlanta*, in 10 parallel forks::
|
||
13 years ago
|
|
||
13 years ago
|
$ ansible atlanta -a "/sbin/reboot" -f 10
|
||
13 years ago
|
|
||
11 years ago
|
/usr/bin/ansible will default to running from your user account. If you do not like this
|
||
11 years ago
|
behavior, pass in "-u username". If you want to run commands as a different user, it looks like this::
|
||
12 years ago
|
|
||
|
$ ansible atlanta -a "/usr/bin/foo" -u username
|
||
13 years ago
|
|
||
9 years ago
|
Often you'll not want to just do things from your user account. If you want to run commands through privilege escalation::
|
||
13 years ago
|
|
||
9 years ago
|
$ ansible atlanta -a "/usr/bin/foo" -u username --become [--ask-become-pass]
|
||
13 years ago
|
|
||
9 years ago
|
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.
|
||
13 years ago
|
|
||
9 years ago
|
It is also possible to become a user other than root using
|
||
|
``--become-user``::
|
||
13 years ago
|
|
||
9 years ago
|
$ ansible atlanta -a "/usr/bin/foo" -u username --become-user otheruser [--ask-become-pass]
|
||
13 years ago
|
|
||
11 years ago
|
.. note::
|
||
9 years ago
|
|
||
|
Rarely, some users have security rules where they constrain their sudo/pbrun/doas environment to running specific command paths only.
|
||
11 years ago
|
This does not work with ansible's no-bootstrapping philosophy and hundreds of different modules.
|
||
9 years ago
|
If doing this, use Ansible from a special account that does not have this constraint.
|
||
11 years ago
|
One way of doing this without sharing access to unauthorized users would be gating Ansible with :doc:`tower`, which
|
||
|
can hold on to an SSH credential and let members of certain organizations use it on their behalf without having direct access.
|
||
|
|
||
11 years ago
|
Ok, so those are basics. If you didn't read about patterns and groups yet, go back and read :doc:`intro_patterns`.
|
||
13 years ago
|
|
||
13 years ago
|
The ``-f 10`` in the above specifies the usage of 10 simultaneous
|
||
11 years ago
|
processes to use. You can also set this in :doc:`intro_configuration` to avoid setting it again. The default is actually 5, which
|
||
11 years ago
|
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
|
||
9 years ago
|
take a little longer. Feel free to push this value as high as your system can handle!
|
||
11 years ago
|
|
||
11 years ago
|
You can also select what Ansible "module" you want to run. Normally commands also take a ``-m`` for module name, but
|
||
12 years ago
|
the default module name is 'command', so we didn't need to
|
||
13 years ago
|
specify that all of the time. We'll use ``-m`` in later examples to
|
||
|
run some other :doc:`modules`.
|
||
13 years ago
|
|
||
13 years ago
|
.. note::
|
||
8 years ago
|
The :ref:`command` module does not support extended shell syntax like piping and 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 :doc:`modules` page.
|
||
13 years ago
|
|
||
13 years ago
|
Using the :ref:`shell` module looks like this::
|
||
13 years ago
|
|
||
13 years ago
|
$ ansible raleigh -m shell -a 'echo $TERM'
|
||
13 years ago
|
|
||
11 years ago
|
When running any command with the Ansible *ad hoc* CLI (as opposed to
|
||
11 years ago
|
:doc:`Playbooks <playbooks>`), pay particular attention to shell quoting rules, so
|
||
11 years ago
|
the local shell doesn't eat a variable before it gets passed to Ansible.
|
||
10 years ago
|
For example, using double rather than single quotes in the above example would
|
||
13 years ago
|
evaluate the variable on the box you were on.
|
||
|
|
||
8 years ago
|
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.
|
||
13 years ago
|
|
||
11 years ago
|
.. _file_transfer:
|
||
13 years ago
|
|
||
12 years ago
|
File Transfer
|
||
|
`````````````
|
||
13 years ago
|
|
||
12 years ago
|
Here's another use case for the `/usr/bin/ansible` command line. Ansible can SCP lots of files to multiple machines in parallel.
|
||
13 years ago
|
|
||
11 years ago
|
To transfer a file directly to many servers::
|
||
13 years ago
|
|
||
13 years ago
|
$ ansible atlanta -m copy -a "src=/etc/hosts dest=/tmp/hosts"
|
||
13 years ago
|
|
||
12 years ago
|
If you use playbooks, you can also take advantage of the ``template`` module,
|
||
|
which takes this another step further. (See module and playbook documentation).
|
||
13 years ago
|
|
||
13 years ago
|
The ``file`` module allows changing ownership and permissions on files. These
|
||
12 years ago
|
same options can be passed directly to the ``copy`` module as well::
|
||
13 years ago
|
|
||
13 years ago
|
$ 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"
|
||
13 years ago
|
|
||
13 years ago
|
The ``file`` module can also create directories, similar to ``mkdir -p``::
|
||
|
|
||
11 years ago
|
$ ansible webservers -m file -a "dest=/path/to/c mode=755 owner=mdehaan group=mdehaan state=directory"
|
||
13 years ago
|
|
||
|
As well as delete directories (recursively) and delete files::
|
||
13 years ago
|
|
||
|
$ ansible webservers -m file -a "dest=/path/to/c state=absent"
|
||
13 years ago
|
|
||
11 years ago
|
.. _managing_packages:
|
||
13 years ago
|
|
||
13 years ago
|
Managing Packages
|
||
|
`````````````````
|
||
|
|
||
13 years ago
|
There are modules available for yum and apt. Here are some examples
|
||
12 years ago
|
with yum.
|
||
13 years ago
|
|
||
13 years ago
|
Ensure a package is installed, but don't update it::
|
||
13 years ago
|
|
||
10 years ago
|
$ ansible webservers -m yum -a "name=acme state=present"
|
||
13 years ago
|
|
||
|
Ensure a package is installed to a specific version::
|
||
|
|
||
10 years ago
|
$ ansible webservers -m yum -a "name=acme-1.5 state=present"
|
||
13 years ago
|
|
||
13 years ago
|
Ensure a package is at the latest version::
|
||
|
|
||
12 years ago
|
$ ansible webservers -m yum -a "name=acme state=latest"
|
||
13 years ago
|
|
||
13 years ago
|
Ensure a package is not installed::
|
||
13 years ago
|
|
||
10 years ago
|
$ ansible webservers -m yum -a "name=acme state=absent"
|
||
13 years ago
|
|
||
9 years ago
|
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.
|
||
13 years ago
|
|
||
11 years ago
|
.. _users_and_groups:
|
||
|
|
||
13 years ago
|
Users and Groups
|
||
|
````````````````
|
||
|
|
||
12 years ago
|
The 'user' module allows easy creation and manipulation of
|
||
13 years ago
|
existing user accounts, as well as removal of user accounts that may
|
||
|
exist::
|
||
13 years ago
|
|
||
13 years ago
|
$ ansible all -m user -a "name=foo password=<crypted password here>"
|
||
13 years ago
|
|
||
13 years ago
|
$ ansible all -m user -a "name=foo state=absent"
|
||
13 years ago
|
|
||
13 years ago
|
See the :doc:`modules` section for details on all of the available options, including
|
||
|
how to manipulate groups and group membership.
|
||
13 years ago
|
|
||
11 years ago
|
.. _from_source_control:
|
||
|
|
||
13 years ago
|
Deploying From Source Control
|
||
|
`````````````````````````````
|
||
13 years ago
|
|
||
|
Deploy your webapp straight from git::
|
||
|
|
||
13 years ago
|
$ ansible webservers -m git -a "repo=git://foo.example.org/repo.git dest=/srv/myapp version=HEAD"
|
||
13 years ago
|
|
||
11 years ago
|
Since Ansible modules can notify change handlers it is possible to
|
||
|
tell Ansible to run specific tasks when the code is updated, such as
|
||
13 years ago
|
deploying Perl/Python/PHP/Ruby directly from git and then restarting
|
||
|
apache.
|
||
13 years ago
|
|
||
11 years ago
|
.. _managing_services:
|
||
|
|
||
13 years ago
|
Managing Services
|
||
|
`````````````````
|
||
|
|
||
|
Ensure a service is started on all webservers::
|
||
|
|
||
13 years ago
|
$ ansible webservers -m service -a "name=httpd state=started"
|
||
13 years ago
|
|
||
|
Alternatively, restart a service on all webservers::
|
||
|
|
||
13 years ago
|
$ ansible webservers -m service -a "name=httpd state=restarted"
|
||
13 years ago
|
|
||
|
Ensure a service is stopped::
|
||
|
|
||
13 years ago
|
$ ansible webservers -m service -a "name=httpd state=stopped"
|
||
13 years ago
|
|
||
11 years ago
|
.. _time_limited_background_operations:
|
||
|
|
||
13 years ago
|
Time Limited Background Operations
|
||
|
``````````````````````````````````
|
||
|
|
||
9 years ago
|
Long running operations can be run in the background, and it is possible to
|
||
8 years ago
|
check their status later. For example, to execute ``long_running_operation``
|
||
9 years ago
|
asynchronously in the background, with a timeout of 3600 seconds (``-B``),
|
||
8 years ago
|
and without polling (``-P``)::
|
||
13 years ago
|
|
||
10 years ago
|
$ ansible all -B 3600 -P 0 -a "/usr/bin/long_running_operation --do-stuff"
|
||
13 years ago
|
|
||
10 years ago
|
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::
|
||
13 years ago
|
|
||
10 years ago
|
$ ansible web1.example.com -m async_status -a "jid=488359678239.2844"
|
||
13 years ago
|
|
||
13 years ago
|
Polling is built-in and looks like this::
|
||
|
|
||
13 years ago
|
$ ansible all -B 1800 -P 60 -a "/usr/bin/long_running_operation --do-stuff"
|
||
|
|
||
9 years ago
|
The above example says "run for 30 minutes max (``-B`` 30*60=1800),
|
||
13 years ago
|
poll for status (``-P``) every 60 seconds".
|
||
13 years ago
|
|
||
13 years ago
|
Poll mode is smart so all jobs will be started before polling will begin on any machine.
|
||
13 years ago
|
Be sure to use a high enough ``--forks`` value if you want to get all of your jobs started
|
||
13 years ago
|
very quickly. After the time limit (in seconds) runs out (``-B``), the process on
|
||
|
the remote nodes will be terminated.
|
||
13 years ago
|
|
||
11 years ago
|
Typically you'll only be backgrounding long-running
|
||
9 years ago
|
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.
|
||
13 years ago
|
|
||
11 years ago
|
.. _checking_facts:
|
||
|
|
||
12 years ago
|
Gathering Facts
|
||
|
```````````````
|
||
|
|
||
12 years ago
|
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::
|
||
12 years ago
|
|
||
12 years ago
|
$ ansible all -m setup
|
||
12 years ago
|
|
||
10 years ago
|
It's also possible to filter this output to just export certain facts, see the "setup" module documentation for details.
|
||
12 years ago
|
|
||
11 years ago
|
Read more about facts at :doc:`playbooks_variables` once you're ready to read up on :doc:`Playbooks <playbooks>`.
|
||
12 years ago
|
|
||
13 years ago
|
.. seealso::
|
||
|
|
||
11 years ago
|
:doc:`intro_configuration`
|
||
11 years ago
|
All about the Ansible config file
|
||
13 years ago
|
:doc:`modules`
|
||
|
A list of available modules
|
||
|
:doc:`playbooks`
|
||
11 years ago
|
Using Ansible for configuration management & deployment
|
||
13 years ago
|
`Mailing List <http://groups.google.com/group/ansible-project>`_
|
||
13 years ago
|
Questions? Help? Ideas? Stop by the list on Google Groups
|
||
|
`irc.freenode.net <http://irc.freenode.net>`_
|
||
|
#ansible IRC chat channel
|