-
Notifications
You must be signed in to change notification settings - Fork 209
/
2-step-02.nutsh
169 lines (123 loc) · 5.34 KB
/
2-step-02.nutsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
lesson_name("First modules and facts")
make_and_go_ws
clear_ws
run(`cp /tutorials/files/step-1-2/hosts hosts`)
"`Talking with nodes`"
"Now we're good to go. "
"Let's play with the command we saw in the previous chapter: `ansible`. This command is the first one of three that ansible provides which interact with nodes."
"Please press the \"`Enter`\" key to continue!"
prompt {
if command == "" {
expect (" ")
break
}
}
"`Doing something useful`"
"In the previous command, `-m ping` means `use module ping`. This module is one of many available with ansible. "
"`ping module` is really simple, it doesn't need any arguments. Modules that take arguments pass them via `-a` switch. Let's see a few other modules."
"Please press the \"`Enter`\" key to continue!"
prompt {
if command == "" {
expect (" ")
break
}
}
"`Shell module`"
"This module lets you execute a shell command on the remote host"
"Please run *ansible -i hosts -m shell -a 'uname -a' host0.example.org*"
prompt {
if success && command == "ansible -i hosts -m shell -a 'uname -a' host0.example.org" {
expect ("ansible -i hosts -m shell -a 'uname -a' host0.example.org")
"Easy!"
"Next one is the `copy module`"
break
}
}
"Please press the \"`Enter`\" key to continue!"
prompt {
if command == "" {
expect (" ")
break
}
}
"`Copy module`"
"No surprise, with this module you can copy a file from the controlling machine to the node. Lets say we want to copy our `/etc/motd` to `/tmp` of our target node. Please run the following command:"
"*ansible -i hosts -m copy -a 'src=/etc/motd dest=/tmp/' host0.example.org*"
prompt {
if success && command == "ansible -i hosts -m copy -a 'src=/etc/motd dest=/tmp/' host0.example.org" {
expect ("ansible -i hosts -m copy -a 'src=/etc/motd dest=/tmp/' host0.example.org")
"Please check the output above."
break
}
}
"Ansible (more accurately `copy module` executed on the node) replied back a bunch of useful information in JSON format. We'll see how that can be used later."
"We'll see other useful modules below. Ansible has a huge module list that covers almost anything you can do on a system."
"Please see the following page: `http://docs.ansible.com/ansible/latest/list_of_all_modules.html`"
"If you can't find the right module, writing one is pretty easy (it doesn't even have to be Python, it just needs to speak JSON)."
"Please press the \"`Enter`\" key to continue!"
prompt {
if command == "" {
expect (" ")
break
}
}
"`Many hosts, same command`"
"Ok, the above stuff is fun, but we have many nodes to manage. Let's try that on other hosts too."
"Lets say we want to get some facts about the node, and, for instance, know which Ubuntu version we have deployed on nodes, it's pretty easy. Please run the following:"
"*ansible -i hosts -m shell -a 'grep DISTRIB_RELEASE /etc/lsb-release' all*"
prompt {
if success && command == "ansible -i hosts -m shell -a 'grep DISTRIB_RELEASE /etc/lsb-release' all" {
expect ("ansible -i hosts -m shell -a 'grep DISTRIB_RELEASE /etc/lsb-release' all")
"`all` is a shortcut meaning `all hosts found in inventory file`"
break
}
}
"Please press the \"`Enter`\" key to continue!"
prompt {
if command == "" {
expect (" ")
break
}
}
"`Many more facts`"
"That was easy. However, It would quickly become cumbersome if we wanted more information (ip addresses, RAM size, etc...). The solution comes from another really handy module (weirdly) called `setup`: it specializes in node's `facts` gathering."
"Try it out:"
"*ansible -i hosts -m setup host0.example.org*"
prompt {
if success && command == "ansible -i hosts -m setup host0.example.org" {
expect ("ansible -i hosts -m setup host0.example.org")
"Replied with lots of information"
break
}
}
"You may also filter returned keys, in case you're looking for something specific."
"For instance, let's say you want to know how much memory you have on all your hosts, easy. Please run the following:"
"*ansible -i hosts -m setup -a 'filter=ansible_memtotal_mb' all*"
prompt {
if success && command == "ansible -i hosts -m setup -a 'filter=ansible_memtotal_mb' all" {
expect ("ansible -i hosts -m setup -a 'filter=ansible_memtotal_mb' all")
"Notice that hosts may reply in different order compared to the previous output. This is because ansible parallelizes communications with hosts!"
break
}
}
"BTW, when using the setup module, you can use `*` in the `filter=` expression. It will act like a shell glob"
"Please press the \"`Enter`\" key to continue!"
prompt {
if command == "" {
expect (" ")
break
}
}
"`Selecting hosts`"
"We saw that `all` means 'all hosts', but ansible provides a lot of other ways to select hosts: `http://docs.ansible.com/ansible/latest/intro_patterns.html`"
"- `host0.example.org:host1.example.org` would run on host0.example.org and host1.example.org"
"- `host*.example.org` would run on all hosts starting with 'host' and ending with '.example.org' (just like a shell glob too)"
"There are other ways that involve groups, we'll see that in the next lesson."
"This is the end of this lesson!"
"Please press the \"`Enter`\" key to continue!"
prompt {
if command == "" {
expect (" ")
break
}
}