examples.rst 9.92 KB
Newer Older
Michael DeHaan committed
1 2
Command Line Examples And Next Steps
====================================
Michael DeHaan committed
3

4 5 6 7
.. highlight:: bash

The following examples show how to use `/usr/bin/ansible` for running
ad hoc tasks.  Start here.
Tim Bielawa committed
8

9 10 11
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)
Michael DeHaan committed
12

13 14 15 16 17
.. contents::
   :depth: 2
   :backlinks: top


Michael DeHaan committed
18 19 20
Parallelism and Shell Commands
``````````````````````````````

21 22
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::
Michael DeHaan committed
23

24
    $ ssh-agent bash
25
    $ ssh-add ~/.ssh/id_rsa
Michael DeHaan committed
26

27 28 29
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.
Michael DeHaan committed
30

31 32
Now to run the command on all servers in a group, in this case,
*atlanta*, in 10 parallel forks::
33

34
    $ ansible atlanta -a "/sbin/reboot" -f 10
Michael DeHaan committed
35

Michael DeHaan committed
36 37 38
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).
39

Michael DeHaan committed
40 41 42
If you want to run commands as a different user, it looks like this::

    $ ansible atlanta -a "/usr/bin/foo" -u username
43

44
If you want to run commands through sudo::
Michael DeHaan committed
45

Michael DeHaan committed
46
    $ ansible atlanta -a "/usr/bin/foo" -u username --sudo [--ask-sudo-pass]
47 48 49 50 51

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.
Michael DeHaan committed
52

53 54
It is also possible to sudo to a user other than root using
``--sudo-user`` (``-U``)::
Michael DeHaan committed
55

Michael DeHaan committed
56
    $ ansible atlanta -a "/usr/bin/foo" -u username -U otheruser [--ask-sudo-pass]
57 58

Ok, so those are basics.  If you didn't read about patterns and groups yet, go back and read :doc:`patterns`.
Michael DeHaan committed
59

60 61
The ``-f 10`` in the above specifies the usage of 10 simultaneous
processes.  Normally commands also take a ``-m`` for module name, but
Michael DeHaan committed
62
the default module name is 'command', so we didn't need to
63 64
specify that all of the time.  We'll use ``-m`` in later examples to
run some other :doc:`modules`.
65

66 67 68 69 70 71
.. 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.
72

73
Using the :ref:`shell` module looks like this::
74

75
    $ ansible raleigh -m shell -a 'echo $TERM'
76

77 78 79 80 81 82 83 84 85 86
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.
87

Michael DeHaan committed
88

Michael DeHaan committed
89 90
File Transfer
`````````````
Michael DeHaan committed
91

Michael DeHaan committed
92
Here's another use case for the `/usr/bin/ansible` command line.  Ansible can SCP lots of files to multiple machines in parallel.
Michael DeHaan committed
93

94
To transfer a file directly to many different servers::
Michael DeHaan committed
95

96
    $ ansible atlanta -m copy -a "src=/etc/hosts dest=/tmp/hosts"
Michael DeHaan committed
97

Michael DeHaan committed
98 99
If you use playbooks, you can also take advantage of the ``template`` module,
which takes this another step further.  (See module and playbook documentation).
Michael DeHaan committed
100

101
The ``file`` module allows changing ownership and permissions on files.  These
Michael DeHaan committed
102
same options can be passed directly to the ``copy`` module as well::
103

104 105
    $ 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"
106

107 108 109
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"
110 111

As well as delete directories (recursively) and delete files::
112 113

    $ ansible webservers -m file -a "dest=/path/to/c state=absent"
114

Michael DeHaan committed
115

Michael DeHaan committed
116 117 118
Managing Packages
`````````````````

119
There are modules available for yum and apt.  Here are some examples
Michael DeHaan committed
120
with yum.
121

Michael DeHaan committed
122
Ensure a package is installed, but don't update it::
123

124
    $ ansible webservers -m yum -a "name=acme state=installed"
Michael DeHaan committed
125 126 127

Ensure a package is installed to a specific version::

128
    $ ansible webservers -m yum -a "name=acme-1.5 state=installed"
Michael DeHaan committed
129

130 131
Ensure a package is at the latest version::

132
    $ ansible webservers -m yum -a "name=acme state=latest"
133

Michael DeHaan committed
134
Ensure a package is not installed::
135

136
    $ ansible webservers -m yum -a "name=acme state=removed"
Michael DeHaan committed
137

138
Currently Ansible only has modules for managing packages with yum and apt.  You can install
139
for other packages for now using the command module or (better!) contribute a module
Michael DeHaan committed
140 141
for other package managers.  Stop by the mailing list for info/details.

142 143 144
Users and Groups
````````````````

Michael DeHaan committed
145
The 'user' module allows easy creation and manipulation of
146 147
existing user accounts, as well as removal of user accounts that may
exist::
148

149
    $ ansible all -m user -a "name=foo password=<crypted password here>"
150

151
    $ ansible all -m user -a "name=foo state=absent"
152

153 154
See the :doc:`modules` section for details on all of the available options, including
how to manipulate groups and group membership.
155

Michael DeHaan committed
156 157
Deploying From Source Control
`````````````````````````````
Michael DeHaan committed
158 159 160

Deploy your webapp straight from git::

161
    $ ansible webservers -m git -a "repo=git://foo.example.org/repo.git dest=/srv/myapp version=HEAD"
Michael DeHaan committed
162

163 164 165 166
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.
Michael DeHaan committed
167

Michael DeHaan committed
168 169 170 171 172
Managing Services
`````````````````

Ensure a service is started on all webservers::

173
    $ ansible webservers -m service -a "name=httpd state=started"
Michael DeHaan committed
174 175 176

Alternatively, restart a service on all webservers::

177
    $ ansible webservers -m service -a "name=httpd state=restarted"
Michael DeHaan committed
178 179 180

Ensure a service is stopped::

181
    $ ansible webservers -m service -a "name=httpd state=stopped"
Michael DeHaan committed
182

Michael DeHaan committed
183 184 185
Time Limited Background Operations
``````````````````````````````````

186 187
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
188 189
hosts, so you won't lose track.  If you kick hosts and don't want
to poll, it looks like this::
Michael DeHaan committed
190

191
    $ ansible all -B 3600 -a "/usr/bin/long_running_operation --do-stuff"
192 193 194

If you do decide you want to check on the job status later, you can::

195
    $ ansible all -m async_status -a "jid=123456789"
Michael DeHaan committed
196

197 198
Polling is built-in and looks like this::

199 200 201 202
    $ 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".
203

204
Poll mode is smart so all jobs will be started before polling will begin on any machine.
205
Be sure to use a high enough ``--forks`` value if you want to get all of your jobs started
Michael DeHaan committed
206 207
very quickly. After the time limit (in seconds) runs out (``-B``), the process on
the remote nodes will be terminated.
208

Michael DeHaan committed
209 210
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.
Michael DeHaan committed
211

212 213 214 215 216 217 218 219 220 221
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::

Michael DeHaan committed
222
    $ ansible webservers:dbservers -m command -a "/bin/foo xyz"
223 224 225 226

This is an "or" condition.  If you want to further constrain the selection, use --limit, which
also works with ``ansible-playbook``::

Michael DeHaan committed
227
    $ ansible webservers:dbservers -m command -a "/bin/foo xyz" --limit region
228

Michael DeHaan committed
229
Assuming version 0.9 or later, as with other host patterns, values to limit can be seperated with ";", ":", or ",".
Michael DeHaan committed
230

231 232 233 234 235 236 237 238
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).

Michael DeHaan committed
239 240 241 242 243 244
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
245

246 247 248 249 250 251 252 253 254
Ansible has an optional configuration file that can be used to tune settings and also eliminate the need to pass various command line flags. Ansible will look for the config file in the following order, using
the first config file it finds present:

1. File specified by the ``ANSIBLE_CONFIG`` environment variable
2. ``ansible.cfg`` in the current working directory. (version 0.8 and up)
3. ``~/.ansible.cfg``
4. ``/etc/ansible/ansible.cfg``

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.
255

256 257 258 259 260 261
.. seealso::

   :doc:`modules`
       A list of available modules
   :doc:`playbooks`
       Using ansible for configuration management & deployment
262
   `Mailing List <http://groups.google.com/group/ansible-project>`_
263 264 265
       Questions? Help? Ideas?  Stop by the list on Google Groups
   `irc.freenode.net <http://irc.freenode.net>`_
       #ansible IRC chat channel