examples.rst 4.1 KB
Newer Older
Michael DeHaan committed
1 2 3 4 5 6
Command Line Examples
=====================

The following examples show how to use `/usr/bin/ansible` for running ad-hoc tasks.
Start here.  For configuration management and deployments, you'll want to pick up on
using `/usr/bin/ansible-playbook` -- the concepts port over directly.
Tim Bielawa committed
7

Michael DeHaan committed
8 9 10 11 12 13 14 15 16 17 18
.. seealso::

   :doc:`modules`
       A list of available modules
   :doc:`playbooks`
       Alternative ways to use ansible


Parallelism and Shell Commands
``````````````````````````````

Michael DeHaan committed
19
Let's use ansible's command line tool to reboot all web servers in Atlanta, 10 at a time::
Michael DeHaan committed
20 21 22 23 24 25 26 27

    ssh-agent bash
    ssh-add ~/.ssh/id_rsa.pub

    ansible atlanta -a "/sbin/reboot" -f 10

The -f 10 specifies the usage of 10 simultaneous processes.

28 29 30 31 32 33
.. note::

   Note that other than the :ref:`command` module, ansible modules 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 'idempotent'.
Michael DeHaan committed
34

Michael DeHaan committed
35 36
File Transfer & Templating
``````````````````````````
Michael DeHaan committed
37

38 39
Ansible can SCP lots of files to multiple machines in parallel, and
optionally use them as template sources.
Michael DeHaan committed
40 41 42 43 44

To just transfer a file directly to many different servers::

    ansible atlanta copy -a "/etc/hosts /tmp/hosts"

45 46 47 48 49 50
To use templating, first run the setup module to put the template
variables you would like to use on the remote host. Then use the
template module to write the files using the templates. Templates are
written in Jinja2 format. Playbooks (covered elsewhere in the
documentation) will run the setup module for you, making this even
simpler.::
Michael DeHaan committed
51 52 53 54 55

    ansible webservers -m setup    -a "favcolor=red ntp_server=192.168.1.1"
    ansible webservers -m template -a "src=/srv/motd.j2 dest=/etc/motd"
    ansible webservers -m template -a "src=/srv/ntp.j2 dest=/etc/ntp.conf"

56 57 58
Need something like the fqdn in a template? If facter or ohai are
installed, data from these projects will also be made available to the
template engine, using 'facter' and 'ohai' prefixes for each.
Michael DeHaan committed
59

Michael DeHaan committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
Managing Packages
`````````````````

Ensure a package is at the latest version::

    ansible webservers -m yum -a "pkg=acme state=latest"

Ensure a package is installed, but don't update it::
    
    ansible webservers -m yum -a "pkg=acme state=installed"

Ensure a package is installed to a specific version::

    ansible-webservers -m yum -a "pkg=acme-1.5 state=installed"

Ensure a package is not installed:
    
    ansible-webservers -m yum -a "pkg=acme state=removed"

Currently Ansible only has a module for managing packages with yum.  You can install
for other package manages using the command module or contribute a module
for other package managers.  Stop by the mailing list for info/details.

Michael DeHaan committed
83 84
Deploying From Source Control
`````````````````````````````
Michael DeHaan committed
85 86 87 88 89

Deploy your webapp straight from git::

    ansible webservers -m git -a "repo=git://foo dest=/srv/myapp version=HEAD"

90 91 92 93
Since ansible modules can notify change handlers (see
:doc:`playbooks`) 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
94

Michael DeHaan committed
95 96 97 98 99
Managing Services
`````````````````

Ensure a service is started on all webservers::

Michael DeHaan committed
100
    ansible webservers -m service -a "name=httpd state=started"
Michael DeHaan committed
101 102 103

Alternatively, restart a service on all webservers::

Michael DeHaan committed
104 105 106 107 108
    ansible webservers -m service -a "name=httpd state=restarted"

Ensure a service is stopped::

    ansible webservers -m service -a "name=httpd state=stopped"
Michael DeHaan committed
109

Michael DeHaan committed
110 111 112
Time Limited Background Operations
``````````````````````````````````

113 114 115 116
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
hosts, so you won't lose track. Polling support is pending in the
command line.::
Michael DeHaan committed
117 118 119 120

    ansible all -B 3600 -a "/usr/bin/long_running_operation --do-stuff"
    ansible all -n job_status -a jid=123456789

121 122 123
Any module other than :ref:`copy` or :ref:`template` can be
backgrounded.  Typically you'll be backgrounding shell commands or
software upgrades only.
Michael DeHaan committed
124

125 126
After the time limit (in seconds) runs out (``-B``), the process on
the remote nodes will be killed.
Michael DeHaan committed
127

Michael DeHaan committed
128