examples.rst 6.48 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

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

Michael DeHaan committed
12
Let's use ansible's command line tool to reboot all web servers in Atlanta, 10 at a time::
Michael DeHaan committed
13 14 15 16 17 18 19 20

    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.

21
.. note::
22
   -m does not always have to be specified to /usr/bin/ansible because 'command' is the default ansible module
23

24
If we want to execute a module using the shell, we can avoid using absolute paths, and can also include
Michael DeHaan committed
25
pipe and redirection operators.   Read more about the differences on the :doc:`modules` page.  The shell
26 27 28 29 30 31 32 33
module looks like this::

    ansible raleigh -m shell -a "echo \\$TERM"

.. note::
   When using ansible to run commands, and in particular the shell module, be careful of shell quoting rules.

.. note::
Michael DeHaan committed
34
   Note that other than the command :doc:`modules`, ansible modules usually do
35 36
   not work like simple scripts. They make the remote system look like
   you state, and run the commands necessary to get it there.  This
37 38
   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 imporant, so Ansible easily supports both.
Michael DeHaan committed
39

Michael DeHaan committed
40 41
File Transfer & Templating
``````````````````````````
Michael DeHaan committed
42

43 44
Ansible can SCP lots of files to multiple machines in parallel, and
optionally use them as template sources.
Michael DeHaan committed
45 46 47

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

48
    ansible atlanta -m copy -a "src=/etc/hosts dest=/tmp/hosts"
Michael DeHaan committed
49

50 51
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
52 53 54
template module to write the files using those templates. 

Templates are written in Jinja2 format. Playbooks (covered elsewhere in the
55
documentation) will run the setup module for you, making this even
56
simpler::
Michael DeHaan committed
57 58 59 60 61

    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"

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
Ansible variables are used in templates by using the name surrounded by double
curly-braces.  If facter or ohai were installed on the remote machine, variables
from those programs can be accessed too, which the appropriate prefix::

    This is an Ansible variable: {{ favcolor }}
    This is a facter variable: {{ facter_hostname }}
    This is an ohai variable: {{ ohai_foo }}

The `file` module allows changing ownership and permissions on files.  These
same options can be passed directly to the `copy` or `template` modules as well::

    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"

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"

As well as delete directories (recursively) and delete files::
    
    ansible webservers -m file -a "dest=/path/to/c state=absent"

84
The mode, owner, and group arguments can also be used on the copy or template lines.
85

Michael DeHaan committed
86

Michael DeHaan committed
87 88 89 90 91 92 93 94 95 96 97
Managing Packages
`````````````````

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"

98 99 100 101
Ensure a package is at the latest version::

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

Michael DeHaan committed
102 103
Ensure a package is not installed::
 
Michael DeHaan committed
104 105 106 107 108 109
    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
110 111
Deploying From Source Control
`````````````````````````````
Michael DeHaan committed
112 113 114 115 116

Deploy your webapp straight from git::

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

117 118 119 120
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
121

Michael DeHaan committed
122 123 124 125 126
Managing Services
`````````````````

Ensure a service is started on all webservers::

Michael DeHaan committed
127
    ansible webservers -m service -a "name=httpd state=started"
Michael DeHaan committed
128 129 130

Alternatively, restart a service on all webservers::

Michael DeHaan committed
131 132 133 134 135
    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
136

Michael DeHaan committed
137 138 139
Time Limited Background Operations
``````````````````````````````````

140 141
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
142 143
hosts, so you won't lose track.  If you kick hosts and don't want
to poll, it looks like this::
Michael DeHaan committed
144 145

    ansible all -B 3600 -a "/usr/bin/long_running_operation --do-stuff"
146 147 148

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

149
    ansible all -m async_status -a "jid=123456789"
Michael DeHaan committed
150

151 152 153 154 155
Polling is built-in and looks like this::
    
    ansible all -B 3600 -P 60 -a "/usr/bin/long_running_operation --do-stuff"

The above example says "run for 60 minutes max (60*60=3600), poll for status every 60 seconds".
156

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

Michael DeHaan committed
162
Any module other than `copy` or `template` can be
163
backgrounded.  Typically you'll be backgrounding long-running 
Michael DeHaan committed
164
shell commands or software upgrades only.  :doc:`playbooks` also support polling, and have
165
a simplified syntax for this.
Michael DeHaan committed
166

167 168 169 170 171 172
.. seealso::

   :doc:`modules`
       A list of available modules
   :doc:`playbooks`
       Using ansible for configuration management & deployment
173 174 175 176 177 178
   `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


Michael DeHaan committed
179

Michael DeHaan committed
180