1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/rst/examples.rst

251 lines
9.6 KiB
ReStructuredText
Raw Normal View History

2012-08-17 03:12:36 +02:00
Command Line Examples And Next Steps
====================================
2012-03-09 13:42:53 +01:00
2012-08-07 04:13:58 +02:00
.. highlight:: bash
The following examples show how to use `/usr/bin/ansible` for running
ad hoc tasks. Start here.
2012-03-07 17:35:18 +01:00
For configuration management and deployments, you'll want to pick up on
using `/usr/bin/ansible-playbook` -- the concepts port over directly.
(See :doc:`playbooks` for more information about those)
2012-03-08 19:36:47 +01:00
Parallelism and Shell Commands
``````````````````````````````
Let's use ansible's command line tool to reboot all web servers in Atlanta, 10 at a time. First, let's
set up SSH-agent so it can remember our credentials::
2012-03-08 19:36:47 +01:00
2012-08-07 04:13:58 +02:00
$ ssh-agent bash
$ ssh-add ~/.ssh/id_rsa.pub
2012-03-08 19:36:47 +01:00
2012-08-07 04:13:58 +02: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 07:04:26 +02:00
2012-08-07 04:13:58 +02:00
Now to run the command on all servers in a group, in this case,
*atlanta*, in 10 parallel forks::
2012-08-07 04:13:58 +02:00
$ ansible atlanta -a "/sbin/reboot" -f 10
2012-03-08 19:36:47 +01:00
2012-08-15 04:20:47 +02:00
In 0.7 and later, this will default to running from your user account. If you do not like this
behavior, pass in "-u username". (In 0.6 and before, it defaulted to root. Most folks prefered
defaulting to the current user, so we changed it).
2012-08-15 04:20:47 +02:00
If you want to run commands as a different user, it looks like this::
$ ansible atlanta -a "/usr/bin/foo" -u username
If you want to run commands through sudo::
2012-04-14 14:42:24 +02:00
2012-08-15 04:20:47 +02:00
$ ansible atlanta -a "/usr/bin/foo" -u username --sudo [--ask-sudo-pass]
2012-08-07 04:13:58 +02:00
Use ``--ask-sudo-pass`` (``-K``) if you are not using passwordless
sudo. This will interactively prompt you for the password to use.
Use of passwordless sudo makes things easier to automate, but it's not
required.
2012-05-10 07:04:26 +02:00
2012-08-07 04:13:58 +02:00
It is also possible to sudo to a user other than root using
``--sudo-user`` (``-U``)::
2012-05-10 07:04:26 +02:00
2012-08-15 04:20:47 +02:00
$ ansible atlanta -a "/usr/bin/foo" -u username -U otheruser [--ask-sudo-pass]
Ok, so those are basics. If you didn't read about patterns and groups yet, go back and read :doc:`patterns`.
2012-03-08 19:36:47 +01:00
2012-08-07 04:13:58 +02:00
The ``-f 10`` in the above specifies the usage of 10 simultaneous
processes. Normally commands also take a ``-m`` for module name, but
the default module name is :ref:`command`, so we didn't need to
specify that all of the time. We'll use ``-m`` in later examples to
run some other :doc:`modules`.
2012-03-09 20:39:29 +01:00
2012-08-07 04:13:58 +02:00
.. note::
The :ref:`command` module requires absolute paths and does not
support shell variables. If we want to execute a module using a
shell, we can do those things, and also use pipe and redirection
operators. Read more about the differences on the :doc:`modules`
page.
2012-03-31 16:50:47 +02:00
2012-08-07 04:13:58 +02:00
Using the :ref:`shell` module looks like this::
2012-03-31 16:50:47 +02:00
2012-08-07 04:13:58 +02:00
$ ansible raleigh -m shell -a 'echo $TERM'
2012-08-07 04:13:58 +02:00
When running any command with the ansible *ad hoc* CLI (as opposed to
:doc:`playbooks`), pay particular attention to shell quoting rules, so
the shell doesn't eat a variable before it gets passed to Ansible.
For example, using double vs single quotes in the above example would
evaluate the variable on the box you were on.
So far we've been demoing simple command execution, but most Ansible modules usually do not work like
simple scripts. They make the remote system look like you state, and run the commands necessary to
get it there. This is commonly referred to as 'idempotence', and is a core design goal of ansible.
However, we also recognize that running *ad hoc* commands is equally important, so Ansible easily supports both.
2012-03-31 16:50:47 +02:00
2012-03-08 19:36:47 +01:00
2012-08-17 03:12:36 +02:00
File Transfer
`````````````
2012-03-08 19:36:47 +01:00
2012-08-17 03:12:36 +02: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 19:36:47 +01:00
To transfer a file directly to many different servers::
2012-03-08 19:36:47 +01:00
2012-08-07 04:13:58 +02:00
$ ansible atlanta -m copy -a "src=/etc/hosts dest=/tmp/hosts"
2012-03-08 19:36:47 +01:00
2012-08-17 03:12:36 +02: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 05:02:28 +02:00
2012-08-07 04:13:58 +02:00
The ``file`` module allows changing ownership and permissions on files. These
2012-08-17 03:12:36 +02:00
same options can be passed directly to the ``copy`` module as well::
2012-03-16 03:57:10 +01:00
2012-08-07 04:13:58 +02: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 03:57:10 +01:00
2012-08-07 04:13:58 +02:00
The ``file`` module can also create directories, similar to ``mkdir -p``::
$ ansible webservers -m file -a "dest=/path/to/c mode=644 owner=mdehaan group=mdehaan state=directory"
2012-03-16 03:57:10 +01:00
As well as delete directories (recursively) and delete files::
2012-08-07 04:13:58 +02:00
$ ansible webservers -m file -a "dest=/path/to/c state=absent"
2012-03-16 03:57:10 +01:00
2012-03-08 19:36:47 +01:00
2012-03-10 17:35:36 +01:00
Managing Packages
`````````````````
2012-08-07 04:13:58 +02:00
There are modules available for yum and apt. Here are some examples
with :ref:`yum`.
2012-03-27 03:41:50 +02:00
2012-03-10 17:35:36 +01:00
Ensure a package is installed, but don't update it::
2012-08-07 04:13:58 +02:00
$ ansible webservers -m yum -a "pkg=acme state=installed"
2012-03-10 17:35:36 +01:00
Ensure a package is installed to a specific version::
2012-08-07 04:13:58 +02:00
$ ansible webservers -m yum -a "pkg=acme-1.5 state=installed"
2012-03-10 17:35:36 +01:00
Ensure a package is at the latest version::
2012-08-07 04:13:58 +02:00
$ ansible webservers -m yum -a "pkg=acme state=latest"
2012-03-31 16:40:01 +02:00
Ensure a package is not installed::
2012-08-07 04:13:58 +02:00
$ ansible webservers -m yum -a "pkg=acme state=removed"
2012-03-10 17:35:36 +01:00
Currently Ansible only has modules for managing packages with yum and apt. You can install
for other packages for now using the command module or (better!) contribute a module
2012-03-10 17:35:36 +01:00
for other package managers. Stop by the mailing list for info/details.
2012-03-22 06:01:02 +01:00
Users and Groups
````````````````
2012-08-07 04:13:58 +02:00
The :ref:`user` module allows easy creation and manipulation of
existing user accounts, as well as removal of user accounts that may
exist::
2012-03-22 06:01:02 +01:00
2012-08-07 04:13:58 +02:00
$ ansible all -m user -a "name=foo password=<crypted password here>"
2012-03-22 06:01:02 +01:00
2012-08-07 04:13:58 +02:00
$ ansible all -m user -a "name=foo state=absent"
2012-03-22 06:01:02 +01: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 06:01:02 +01:00
2012-03-09 04:50:00 +01:00
Deploying From Source Control
`````````````````````````````
2012-03-08 19:36:47 +01:00
Deploy your webapp straight from git::
2012-08-07 04:13:58 +02:00
$ ansible webservers -m git -a "repo=git://foo.example.org/repo.git dest=/srv/myapp version=HEAD"
2012-03-08 19:36:47 +01:00
2012-08-07 04:13:58 +02: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
deploying Perl/Python/PHP/Ruby directly from git and then restarting
apache.
2012-03-08 19:36:47 +01:00
2012-03-09 04:50:00 +01:00
Managing Services
`````````````````
Ensure a service is started on all webservers::
2012-08-07 04:13:58 +02:00
$ ansible webservers -m service -a "name=httpd state=started"
2012-03-09 04:50:00 +01:00
Alternatively, restart a service on all webservers::
2012-08-07 04:13:58 +02:00
$ ansible webservers -m service -a "name=httpd state=restarted"
2012-03-10 17:35:36 +01:00
Ensure a service is stopped::
2012-08-07 04:13:58 +02:00
$ ansible webservers -m service -a "name=httpd state=stopped"
2012-03-09 04:50:00 +01:00
2012-03-09 13:42:53 +01:00
Time Limited Background Operations
``````````````````````````````````
2012-03-09 20:39:29 +01:00
Long running operations can be backgrounded, and their status can be
checked on later. The same job ID is given to the same task on all
2012-03-12 02:09:21 +01:00
hosts, so you won't lose track. If you kick hosts and don't want
to poll, it looks like this::
2012-03-09 13:42:53 +01:00
2012-08-07 04:13:58 +02:00
$ ansible all -B 3600 -a "/usr/bin/long_running_operation --do-stuff"
2012-03-12 02:09:21 +01:00
If you do decide you want to check on the job status later, you can::
2012-08-07 04:13:58 +02:00
$ ansible all -m async_status -a "jid=123456789"
2012-03-09 13:42:53 +01:00
2012-03-12 02:09:21 +01:00
Polling is built-in and looks like this::
2012-08-07 04:13:58 +02:00
$ ansible all -B 1800 -P 60 -a "/usr/bin/long_running_operation --do-stuff"
The above example says "run for 30 minutes max (``-B``: 30*60=1800),
poll for status (``-P``) every 60 seconds".
2012-03-12 02:09:21 +01:00
Poll mode is smart so all jobs will be started before polling will begin on any machine.
2012-08-07 04:13:58 +02:00
Be sure to use a high enough ``--forks`` value if you want to get all of your jobs started
2012-03-12 04:20:55 +01:00
very quickly. After the time limit (in seconds) runs out (``-B``), the process on
the remote nodes will be terminated.
2012-03-12 02:09:21 +01:00
2012-08-17 03:12:36 +02:00
Typically you'll be only be backgrounding long-running
shell commands or software upgrades only. Backgrounding the copy module does not do a background file transfer. :doc:`playbooks` also support polling, and have a simplified syntax for this.
2012-03-09 13:42:53 +01:00
2012-08-15 04:07:00 +02:00
Limiting Selected Hosts
```````````````````````
.. versionadded:: 0.7
What hosts you select to manage can be additionally constrained by using the '--limit' parameter or
by using 'batch' (or 'range') selectors.
As mentioned above, patterns can be strung together to select hosts in more than one group::
2012-08-17 03:12:36 +02:00
$ ansible webservers:dbservers -m command -a "/bin/foo xyz"
2012-08-15 04:07:00 +02:00
This is an "or" condition. If you want to further constrain the selection, use --limit, which
also works with ``ansible-playbook``::
$ ansible webservers:dbservers -m command -a "/bin/foo xyz" region
Now let's talk about range selection. Suppose you have 1000 servers in group 'datacenter', but only want to target one at a time. This is also easy::
$ ansible webservers[0-99] -m command -a "/bin/foo xyz"
$ ansible webservers[100-199] -m command -a "/bin/foo xyz"
This will select the first 100, then the second 100, host entries in the webservers group. (It does not matter
what their names or IP addresses are).
2012-08-17 03:12:36 +02:00
Both of these methods can be used at the same time, and ranges can also be passed to the --limit parameter.
Configuration & Defaults
````````````````````````
.. versionadded:: 0.7
2012-08-15 04:07:00 +02:00
2012-08-17 03:12:36 +02:00
Ansible has an optional configuration file that can be used to tune settings and also eliminate the need to pass various command line flags. The config file location is controlled by the ANSIBLE_CONFIG environment variable, if set, otherwise ~/ansible.cfg or /etc/ansible/ansible.cfg will be loaded, whichever comes first. For those running from source, a sample configuration file lives in the examples/ directory. The RPM will install configuration into /etc/ansible/ansible.cfg automatically.
2012-08-15 04:07:00 +02:00
.. seealso::
:doc:`modules`
A list of available modules
:doc:`playbooks`
Using ansible for configuration management & deployment
2012-08-07 04:13:58 +02:00
`Mailing List <http://groups.google.com/group/ansible-project>`_
Questions? Help? Ideas? Stop by the list on Google Groups
`irc.freenode.net <http://irc.freenode.net>`_
#ansible IRC chat channel