You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The drudge work of administering Lindoes: setting up a server, and especially multiple servers, keeping them all updated, pushing changes out to them, copying files, etc. Things can get complicated and time consuming very quickly, but it doesn't have to be that way. _[Ansible](http://www.ansible.com/home)_ is a very helpful tool that allows you to create groups of machines, describe how those machines should be configured or what actions should be taken on them, and issue all of these commands from a central location. It uses simple SSH, and so nothing is required to be installed on the machines you are targeting, needing only Ansible's installed files on your main control machine (can even be your laptop!). It is a simple solution to a complicated problem.
24
+
The drudge work of administering Linodes: setting up a server, and especially multiple servers, keeping them all updated, pushing changes out to them, copying files, etc. Things can get complicated and time consuming very quickly, but it doesn't have to be that way. *[Ansible](http://www.ansible.com/home)* is a very helpful tool that allows you to create groups of machines, describe how those machines should be configured or what actions should be taken on them, and issue all of these commands from a central location. It uses simple SSH, and so nothing is required to be installed on the machines you are targeting, needing only Ansible's installed files on your main control machine (can even be your laptop!). It is a simple solution to a complicated problem.
25
25
26
-
In this guide you will be introduced to the basic of Ansible, and will get it installed and up and running. By the end of this guide, you'll have the tools needed to turn a brand new Linode VPS into a simple web server (Apache, MySQL, PHP), easily replicatable and adjustable. (And why not go ahead and try it? With Linode's hourly billing you can experiment with this guide for just a few cents.)
26
+
In this guide you will be introduced to the basics of Ansible, and will get it installed and up and running. By the end of this guide, you'll have the tools needed to turn a brand new Linode VPS into a simple web server (Apache, MySQL, PHP), easily replicatable and adjustable. (And why not go ahead and try it? With Linode's hourly billing you can experiment with this guide for just a few cents.)
27
27
28
28
29
29
## Installation
30
30
31
-
Ansible only needs to be installed on the _control machine_, or the machine from which you will be running commands. This will likely be your laptop or other computer from which you frequently access your server, or it may be a centralized server in more complicated setups.
31
+
Ansible only needs to be installed on the *control machine*, or the machine from which you will be running commands. This will likely be your laptop or other computer from which you frequently access your server, or it may be a centralized server in more complicated setups.
32
32
33
33
Make sure that you have Python 2.x available on the control machine (Ansible is not compatible with Python 3, nor can you use Windows as the control machine). You can easily [build Ansible from source](https://github.com/ansible/ansible) as is frequently done, or you can install the latest stable packages using the proper command below.
34
34
@@ -37,9 +37,13 @@ Make sure that you have Python 2.x available on the control machine (Ansible is
37
37
sudo easy_install pip
38
38
sudo pip install ansible
39
39
40
-
*CentOS/Fedora: install the epel-release RPM if needed on CentOS, RHEL, or Scientific Linux*
40
+
*CentOS/Fedora:*
41
41
42
42
sudo yum install ansible
43
+
44
+
{: .note}
45
+
>
46
+
>The EPEL-Release repository may need to be added on certain versions of CentOS, RHEL, and Scientific Linux
43
47
44
48
*Ubuntu:*
45
49
@@ -74,16 +78,16 @@ If you are successful you should see output similar to the following:
74
78
"ping": "pong"
75
79
}
76
80
77
-
Hooray! You were just able to get a valid connection to your server via Ansible! Now let's talk briefly about your _Inventory_ file and then move on to configuring the server via Ansible _Playbooks_.
81
+
Hooray! You were just able to get a valid connection to your server via Ansible! Now let's talk briefly about your *Inventory* file and then move on to configuring the server via Ansible *Playbooks*.
78
82
79
83
80
84
## Inventory File
81
85
82
86
You executed an Ansible command against your server, but it would be a pain to have to type the host's address every single time, and what if you had several servers you wanted to apply the same configuration to? This is where Ansible's [Inventory file](http://docs.ansible.com/ansible/intro_inventory.html) comes into play. By default, this file is expected to be located at `/etc/ansible/hosts`. Create that path and file if it does not already exist. If you are running OS X, you may want to create your own Ansible directory elsewhere and then set the path in an Ansible configuration file, as in the following:
83
87
84
-
# The ~/Code/ path below is up to you
85
-
mkdir ~/Code/ansible
86
-
touch ~/Code/ansible/hosts
88
+
# The ~/Path/To/ path below is up to you
89
+
mkdir ~/Path/To/ansible
90
+
touch ~/Path/To/ansible/hosts
87
91
touch ~/.ansible.cfg
88
92
89
93
Open that new `~/.ansible.cfg` file and add the following lines:
@@ -92,21 +96,21 @@ Open that new `~/.ansible.cfg` file and add the following lines:
92
96
~/.ansible.cfg
93
97
: ~~~ ini
94
98
[default]
95
-
inventory = ~/Code/ansible/hosts
96
-
~~~
99
+
inventory = ~/Path/To/ansible/hosts
100
+
~~~
97
101
98
102
Add an entry to your new hosts file, pointing to a server that you connected to in the previous section. You can include multiple servers in this file, using either domains or IP addresses, and can even group them. Example:
99
103
100
104
{: .file-excerpt}
101
-
~/Code/ansible/hosts
105
+
~/Path/To/ansible/hosts
102
106
: ~~~ ini
103
107
mainserver.com
104
108
myserver.net:2222
105
109
106
110
[mailservers]
107
111
mail1.mainserver.com
108
112
mail2.mainserver.com
109
-
~~~
113
+
~~~
110
114
111
115
For now, just add one entry, save, and run the following command, very similar to before, to try and ping your server via Ansible.
112
116
@@ -117,7 +121,7 @@ You should receive the same output as previously. Note that this time you used `
117
121
118
122
## Configuration via Playbooks
119
123
120
-
_Playbooks_ in Ansible define a series of actions to run, and address particular sets of servers. It's important to note that, unlike some other configuration tools, a playbook does not describe a state of the machine, with Ansible determining all the changes that need to be made on its own. However, playbooks should be designed to be idempotent, meaning that they can be run more than once without negative effects. For example, a playbook might have a task that sets up a configuration file for a server and injects a few variables. The playbook should be written such that Ansible can take the template config file, compare it to the actual file, and create/update it only if necessary. Luckily, many Ansible modules take care of the heavy lifting for that.
124
+
*Playbooks* in Ansible define a series of actions to run, and address particular sets of servers. It's important to note that, unlike some other configuration tools, a playbook does not describe a state of the machine, with Ansible determining all the changes that need to be made on its own. However, playbooks should be designed to be idempotent, meaning that they can be run more than once without negative effects. For example, a playbook might have a task that sets up a configuration file for a server and injects a few variables. The playbook should be written such that Ansible can take the template configuration file, compare it to the actual file, and create/update it only if necessary. Luckily, many Ansible modules take care of the heavy lifting for that.
121
125
122
126
You can write playbooks to perform initial server configurations, add users and directories, ensure certain software packages are installed or uninstalled, move files, etc. A playbook can also run a few commands on one set of machines, switch to a different set to run some different commands, and then even switch back to the original or different set of machines. It is procedural, and tasks are run in order, top to bottom.
123
127
@@ -132,7 +136,7 @@ Sample Playbook YAML file
132
136
tasks:
133
137
- [task 1]
134
138
- [task 2]
135
-
~~~
139
+
~~~
136
140
137
141
For example, the following playbook would log in to all web servers and ensure Apache was started.
138
142
@@ -147,15 +151,19 @@ Sample service check playbook
147
151
service: name=httpd state=started
148
152
become: yes
149
153
become_method: sudo
150
-
~~~
154
+
~~~
151
155
152
156
You see an example of a task in that playbook:
153
157
154
-
tasks:
158
+
{: .file-excerpt}
159
+
Playbook task
160
+
: ~~~ yaml
161
+
tasks:
155
162
- name: Ensure the Apache daemon has started
156
163
service: name=httpd state=started
157
164
become: yes
158
165
become_method: sudo
166
+
~~~
159
167
160
168
Every task should have a name which gets logged and can help you track progress. Following the name line is the module that will be run (in this case, the [service module](http://docs.ansible.com/ansible/service_module.html)), and the other attributes provide more options, in this case instructing Ansible to become the root user.
161
169
@@ -165,13 +173,13 @@ Executing a playbook is even easier than running ad-hoc commands like we did ear
165
173
166
174
ansible-playbook myplaybook.yml
167
175
168
-
Done! If you want to see what hosts this playbook will affect without having to open up the YAML file, you can run
176
+
Done! If you want to see what hosts this playbook will affect without having to open up the YAML file, you can run:
169
177
170
178
ansible-playbook myplaybook.yml --list-hosts
171
179
172
180
### Types of Tasks You Can Run
173
181
174
-
Ansible ships with a large collection of modules that you can run as tasks or via ad-hoc commands. To see a listing of all available modules, run
182
+
Ansible ships with a large collection of modules that you can run as tasks or via ad-hoc commands. To see a listing of all available modules, run:
175
183
176
184
ansible-docs -l
177
185
@@ -211,9 +219,9 @@ First, add your new server's IP to your Ansible `hosts` file so that we can addr
211
219
: ~~~ ini
212
220
[linode]
213
221
123.123.123.123
214
-
~~~
222
+
~~~
215
223
216
-
Let's write a playbook that creates a new normal user, adds in our public key, and adds the new user to the sudoers file so that we can use such a connection in the future. We're introducing a new aspect of Ansible here: _variables_. See the `vars:` entry and the `NORMAL_USER_NAME` line? You'll notice that it is reused twice in the file so that we only have to change it once.
224
+
Let's write a playbook that creates a new normal user, adds in our public key, and adds the new user to the sudoers file so that we can use such a connection in the future. We're introducing a new aspect of Ansible here: *variables*. See the `vars:` entry and the `NORMAL_USER_NAME` line? You'll notice that it is reused twice in the file so that we only have to change it once. Replace `username` with your choosen username.
217
225
218
226
{: .file}
219
227
initialize_basic_user.yml
@@ -222,7 +230,7 @@ initialize_basic_user.yml
222
230
- hosts: linode
223
231
remote_user: root
224
232
vars:
225
-
NORMAL_USER_NAME: 'josh'
233
+
NORMAL_USER_NAME: 'username'
226
234
tasks:
227
235
- name: "Create a secondary, non-root user"
228
236
user: name={{ NORMAL_USER_NAME }}
@@ -235,7 +243,7 @@ initialize_basic_user.yml
235
243
regexp="{{ NORMAL_USER_NAME }} ALL"
236
244
line="{{ NORMAL_USER_NAME }} ALL=(ALL) ALL"
237
245
state=present
238
-
~~~
246
+
~~~
239
247
240
248
Save that file as `initialize_basic_user.yml` and run the playbook with the following command. Note how we specify the use of a particular user (`-u root`) and force Ansible to prompt us for the password (`-ask-pass`) since we don't have key authentication set up yet.
241
249
@@ -250,7 +258,7 @@ common_server_setup.yml
250
258
: ~~~ yaml
251
259
---
252
260
- hosts: linode
253
-
remote_user: josh
261
+
remote_user: username
254
262
become: yes
255
263
become_method: sudo
256
264
vars:
@@ -270,7 +278,7 @@ common_server_setup.yml
270
278
with_items: groups['linode']
271
279
- name: Update packages
272
280
apt: update_cache=yes upgrade=dist
273
-
~~~
281
+
~~~
274
282
275
283
Run the command with a few less arguments:
276
284
@@ -283,11 +291,11 @@ At any time now you can SSH into the server directly and verify that things are
283
291
Finally, let's get a very basic server set up with Apache and PHP, and a test MySQL databases to use. The following playbook downloads the appropriate packages, turns on the Apache and MySQL services, and creates a basic database and user.
284
292
285
293
{: .file}
286
-
common_server_setup.yml
294
+
setup_webserver.yml
287
295
: ~~~ yaml
288
296
---
289
297
- hosts: linode
290
-
remote_user: josh
298
+
remote_user: username
291
299
become: yes
292
300
become_method: sudo
293
301
tasks:
@@ -315,7 +323,7 @@ common_server_setup.yml
315
323
mysql_user: name=webapp
316
324
password=mypassword
317
325
priv=*.*:ALL state=present
318
-
~~~
326
+
~~~
319
327
320
328
And as before, run the playbook from your control machine with the following command.
0 commit comments