1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
---|
2 | <html xmlns="http://www.w3.org/1999/xhtml"> |
---|
3 | <head> |
---|
4 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
---|
5 | <meta http-equiv="Content-Style-Type" content="text/css" /> |
---|
6 | <meta name="generator" content="pandoc" /> |
---|
7 | <title>Three steps to Ansible enlightenment</title> |
---|
8 | <style type="text/css">code{white-space: pre;}</style> |
---|
9 | <link rel="stylesheet" href="../style.css" type="text/css" /> |
---|
10 | </head> |
---|
11 | <body> |
---|
12 | <div id="header"> |
---|
13 | <h1 class="title">Three steps to Ansible enlightenment</h1> |
---|
14 | </div> |
---|
15 | <div id="TOC"> |
---|
16 | <ul> |
---|
17 | <li><a href="#use-ansible-to-run-shell-commands"><span class="toc-section-number">1</span> Use ansible to run shell commands</a></li> |
---|
18 | <li><a href="#using-ansible-modules"><span class="toc-section-number">2</span> Using ansible modules</a><ul> |
---|
19 | <li><a href="#install-a-package"><span class="toc-section-number">2.1</span> Install a package</a></li> |
---|
20 | <li><a href="#copy-a-file"><span class="toc-section-number">2.2</span> Copy a file</a></li> |
---|
21 | <li><a href="#create-a-directory"><span class="toc-section-number">2.3</span> Create a directory</a></li> |
---|
22 | </ul></li> |
---|
23 | <li><a href="#writing-playbooks"><span class="toc-section-number">3</span> Writing playbooks</a></li> |
---|
24 | <li><a href="#optional-guru-level-roles"><span class="toc-section-number">4</span> Optional Guru level: Roles</a></li> |
---|
25 | </ul> |
---|
26 | </div> |
---|
27 | <p>Can you use ansible to apply updates to all your VMs? Yes you can! You can start simple and grow as your experience develops.</p> |
---|
28 | <p>Edit the file <code>/etc/ansible/hosts</code> as root (remember <code>sudo</code>) and add all your VMs under a group heading, like this:</p> |
---|
29 | <pre><code>[vms] |
---|
30 | host1.ws.nsrc.org |
---|
31 | host2.ws.nsrc.org |
---|
32 | host3.ws.nsrc.org</code></pre> |
---|
33 | <p>or the short form:</p> |
---|
34 | <pre><code>[vms] |
---|
35 | host[1:3].ws.nsrc.org</code></pre> |
---|
36 | <h1 id="use-ansible-to-run-shell-commands"><a href="#use-ansible-to-run-shell-commands"><span class="header-section-number">1</span> Use ansible to run shell commands</a></h1> |
---|
37 | <pre><code># ansible vms [-u sysadm] -skK -m shell -a 'apt-get install -y bash'</code></pre> |
---|
38 | <p>Here is an explanation of the flags</p> |
---|
39 | <dl> |
---|
40 | <dt><code>-u sysadm</code></dt> |
---|
41 | <dd><p>login as user <code>sysadm</code> (only required if the login username is different from your local username)</p> |
---|
42 | </dd> |
---|
43 | <dt><code>-s</code></dt> |
---|
44 | <dd><p>use sudo to get root on the remote machine</p> |
---|
45 | </dd> |
---|
46 | <dt><code>-k</code></dt> |
---|
47 | <dd><p>prompt for login password</p> |
---|
48 | </dd> |
---|
49 | <dt><code>-K</code></dt> |
---|
50 | <dd><p>prompt for sudo password (just hit Enter to use the same password)</p> |
---|
51 | </dd> |
---|
52 | <dt><code>-m shell</code></dt> |
---|
53 | <dd><p>invoke the shell module</p> |
---|
54 | </dd> |
---|
55 | <dt><code>-a '...'</code></dt> |
---|
56 | <dd><p>arguments to the shell module (i.e. which shell command to run)</p> |
---|
57 | </dd> |
---|
58 | </dl> |
---|
59 | <p>This is very easy, but it will not work if the remote command prompts for input. (The flag <code>-y</code> to apt-get assumes "yes" to confirmation prompts)</p> |
---|
60 | <p>To specify a different inventory file use the <code>-i</code> option, e.g. <code>-i myhosts</code></p> |
---|
61 | <h1 id="using-ansible-modules"><a href="#using-ansible-modules"><span class="header-section-number">2</span> Using ansible modules</a></h1> |
---|
62 | <p>Your next step is to start learning the specific <a href="http://docs.ansible.com/modules_by_category.html">ansible modules</a> for each system administration task. Here are some common ones.</p> |
---|
63 | <h2 id="install-a-package"><a href="#install-a-package"><span class="header-section-number">2.1</span> Install a package</a></h2> |
---|
64 | <p>Use the <a href="http://docs.ansible.com/apt_module.html">apt module</a> to install packages on Ubuntu and Debian hosts.</p> |
---|
65 | <pre><code># ansible vms [-u sysadm] -skK -m apt -a 'pkg=bash state=present'</code></pre> |
---|
66 | <h2 id="copy-a-file"><a href="#copy-a-file"><span class="header-section-number">2.2</span> Copy a file</a></h2> |
---|
67 | <p>Use the <a href="http://docs.ansible.com/copy_module.html">copy module</a>. Example: to copy a file <code>foo</code> from the current directory to <code>/etc/foo</code> on the target hosts, and set the mode to make it world-readable:</p> |
---|
68 | <pre><code># ansible vms [-u sysadm] -skK -m copy -a 'src=foo dest=/etc/foo mode=644'</code></pre> |
---|
69 | <h2 id="create-a-directory"><a href="#create-a-directory"><span class="header-section-number">2.3</span> Create a directory</a></h2> |
---|
70 | <p>The <a href="http://docs.ansible.com/file_module.html">file module</a> can set the ownership and mode of files, and also create directories.</p> |
---|
71 | <pre><code># ansible vms [-u sysadm] -skK -m file -a 'path=/etc/bar state=directory'</code></pre> |
---|
72 | <p>Use <code>state=absent</code> to delete a file or a directory and its contents.</p> |
---|
73 | <h1 id="writing-playbooks"><a href="#writing-playbooks"><span class="header-section-number">3</span> Writing playbooks</a></h1> |
---|
74 | <p>Once you know the ansible module and arguments, you can put them into a playbook, e.g. <code>foo.yml</code></p> |
---|
75 | <pre><code>- hosts: |
---|
76 | - vms |
---|
77 | tasks: |
---|
78 | - apt: pkg=bash state=installed</code></pre> |
---|
79 | <p>and run it:</p> |
---|
80 | <pre><code># ansible-playbook [-u sysadm] -skK foo.yml</code></pre> |
---|
81 | <p>Now you can make a single playbook which performs multiple tasks.</p> |
---|
82 | <h1 id="optional-guru-level-roles"><a href="#optional-guru-level-roles"><span class="header-section-number">4</span> Optional Guru level: Roles</a></h1> |
---|
83 | <p>Roles make your tasks easier to re-use in multiple playbooks. Create a directory <code>roles</code>, inside that a subdirectory for the role name (say "bar"), and inside that a subdirectory <code>tasks</code></p> |
---|
84 | <p>Now create file <code>roles/bar/tasks/main.yml</code></p> |
---|
85 | <pre><code>- apt: pkg-bash state=installed</code></pre> |
---|
86 | <p>Then foo.yml becomes:</p> |
---|
87 | <pre><code>- hosts: |
---|
88 | - vms |
---|
89 | roles: |
---|
90 | - bar</code></pre> |
---|
91 | <p>Any files you want to copy can be stored in <code>roles/bar/files/...</code></p> |
---|
92 | </body> |
---|
93 | </html> |
---|