0
# Command Line Interface
1
2
Ansible Core provides nine comprehensive command-line tools for different automation tasks. Each CLI tool follows consistent patterns for argument parsing, configuration, and output formatting while providing specialized functionality for specific automation scenarios.
3
4
## Capabilities
5
6
### Ad-hoc Command Execution
7
8
Execute single tasks across multiple hosts without requiring playbooks, supporting all Ansible modules with pattern-based host selection and parallel execution.
9
10
```python { .api }
11
def main():
12
"""
13
Entry point for ansible ad-hoc command execution.
14
15
Usage: ansible <host-pattern> -m <module> -a <arguments>
16
17
Returns:
18
int: Exit code (0 for success, non-zero for failure)
19
"""
20
```
21
22
Examples:
23
```bash
24
# Ping all hosts
25
ansible all -m ping
26
27
# Execute shell commands
28
ansible webservers -m shell -a "uptime"
29
30
# Copy files
31
ansible databases -m copy -a "src=/tmp/file dest=/tmp/file"
32
```
33
34
### Playbook Execution
35
36
Execute YAML playbooks with comprehensive options for inventory selection, variable override, limit patterns, tags, verbosity control, and execution strategies.
37
38
```python { .api }
39
def main():
40
"""
41
Entry point for ansible-playbook execution.
42
43
Usage: ansible-playbook [options] playbook.yml
44
45
Returns:
46
int: Exit code (0 for success, non-zero for failure)
47
"""
48
```
49
50
Examples:
51
```bash
52
# Basic playbook execution
53
ansible-playbook site.yml
54
55
# With inventory and variables
56
ansible-playbook -i inventory site.yml -e "version=1.2.3"
57
58
# Limit to specific hosts
59
ansible-playbook site.yml --limit webservers
60
61
# Run specific tags
62
ansible-playbook site.yml --tags deploy,config
63
```
64
65
### Configuration Management
66
67
View, edit, and manage Ansible configuration settings with support for different configuration sources and comprehensive configuration validation.
68
69
```python { .api }
70
def main():
71
"""
72
Entry point for ansible-config management.
73
74
Usage: ansible-config [view|edit|dump|list] [options]
75
76
Returns:
77
int: Exit code (0 for success, non-zero for failure)
78
"""
79
```
80
81
Examples:
82
```bash
83
# View current configuration
84
ansible-config view
85
86
# List all configuration options
87
ansible-config list
88
89
# Dump all settings with sources
90
ansible-config dump
91
```
92
93
### Interactive Console
94
95
Interactive REPL environment for Ansible with command completion, history, and direct access to inventory and modules for exploratory automation.
96
97
```python { .api }
98
def main():
99
"""
100
Entry point for ansible-console interactive environment.
101
102
Usage: ansible-console [host-pattern]
103
104
Returns:
105
int: Exit code (0 for success, non-zero for failure)
106
"""
107
```
108
109
Examples:
110
```bash
111
# Start console with all hosts
112
ansible-console
113
114
# Start console with specific pattern
115
ansible-console webservers
116
117
# Interactive commands within console
118
root@all (3)[f:5]$ ping
119
root@all (3)[f:5]$ setup
120
```
121
122
### Documentation Viewer
123
124
Built-in documentation system providing comprehensive module, plugin, and keyword documentation with search capabilities and multiple output formats.
125
126
```python { .api }
127
def main():
128
"""
129
Entry point for ansible-doc documentation viewer.
130
131
Usage: ansible-doc [options] [plugin]
132
133
Returns:
134
int: Exit code (0 for success, non-zero for failure)
135
"""
136
```
137
138
Examples:
139
```bash
140
# List all modules
141
ansible-doc -l
142
143
# View module documentation
144
ansible-doc copy
145
146
# View plugin documentation
147
ansible-doc -t connection ssh
148
149
# Search documentation
150
ansible-doc -F keyword copy
151
```
152
153
### Galaxy Collection Management
154
155
Ansible Galaxy integration for downloading, installing, and managing collections and roles with dependency resolution and version management.
156
157
```python { .api }
158
def main():
159
"""
160
Entry point for ansible-galaxy collection management.
161
162
Usage: ansible-galaxy [collection|role] [action] [options]
163
164
Returns:
165
int: Exit code (0 for success, non-zero for failure)
166
"""
167
```
168
169
Examples:
170
```bash
171
# Install collection
172
ansible-galaxy collection install community.general
173
174
# Install from requirements
175
ansible-galaxy collection install -r requirements.yml
176
177
# Create collection skeleton
178
ansible-galaxy collection init my_namespace.my_collection
179
180
# List installed collections
181
ansible-galaxy collection list
182
```
183
184
### Inventory Management
185
186
Inventory inspection and management tools supporting multiple inventory sources with host and group listing capabilities and graph visualization.
187
188
```python { .api }
189
def main():
190
"""
191
Entry point for ansible-inventory management.
192
193
Usage: ansible-inventory [options] [host|group]
194
195
Returns:
196
int: Exit code (0 for success, non-zero for failure)
197
"""
198
```
199
200
Examples:
201
```bash
202
# List all inventory
203
ansible-inventory --list
204
205
# Show inventory graph
206
ansible-inventory --graph
207
208
# Export to YAML
209
ansible-inventory --list --yaml
210
211
# Show specific host
212
ansible-inventory --host webserver1
213
```
214
215
### Pull-based Automation
216
217
Git-based automation for distributed deployments where managed nodes pull configurations from version control systems rather than being pushed from a control node.
218
219
```python { .api }
220
def main():
221
"""
222
Entry point for ansible-pull automation.
223
224
Usage: ansible-pull -U <repository> [options] [playbook]
225
226
Returns:
227
int: Exit code (0 for success, non-zero for failure)
228
"""
229
```
230
231
Examples:
232
```bash
233
# Pull and run playbook
234
ansible-pull -U https://github.com/example/playbooks.git site.yml
235
236
# With specific branch and interval
237
ansible-pull -U repo.git -C production --sleep 300 site.yml
238
239
# With vault password
240
ansible-pull -U repo.git --vault-password-file /path/to/vault site.yml
241
```
242
243
### Secrets Management
244
245
Ansible Vault integration for encrypting sensitive data including variables, files, and entire playbooks with multiple encryption methods and key management.
246
247
```python { .api }
248
def main():
249
"""
250
Entry point for ansible-vault secrets management.
251
252
Usage: ansible-vault [create|edit|encrypt|decrypt|view] [options] file
253
254
Returns:
255
int: Exit code (0 for success, non-zero for failure)
256
"""
257
```
258
259
Examples:
260
```bash
261
# Create encrypted file
262
ansible-vault create secrets.yml
263
264
# Encrypt existing file
265
ansible-vault encrypt vars.yml
266
267
# Edit encrypted file
268
ansible-vault edit secrets.yml
269
270
# View encrypted file
271
ansible-vault view secrets.yml
272
273
# Decrypt file
274
ansible-vault decrypt secrets.yml
275
```
276
277
## Base CLI Framework
278
279
### CLI Base Class
280
281
```python { .api }
282
class CLI:
283
"""
284
Base class for all Ansible CLI tools providing common functionality.
285
286
Attributes:
287
- args: Parsed command line arguments
288
- options: CLI options configuration
289
- parser: Argument parser instance
290
"""
291
292
def __init__(self, args=None):
293
"""Initialize CLI base class"""
294
295
def parse(self):
296
"""Parse command line arguments"""
297
298
def run(self):
299
"""Execute CLI command logic"""
300
```
301
302
### Helper Functions
303
304
```python { .api }
305
def opt_help(option):
306
"""
307
Generate help text for CLI options.
308
309
Parameters:
310
- option: CLI option configuration
311
312
Returns:
313
str: Formatted help text
314
"""
315
316
class SortingHelpFormatter:
317
"""Custom help formatter for consistent CLI output"""
318
```
319
320
## CLI Configuration
321
322
All CLI tools support common options:
323
324
- **Inventory**: `-i, --inventory` - Specify inventory sources
325
- **Verbosity**: `-v, -vv, -vvv, -vvvv` - Control output verbosity
326
- **Connection**: `-c, --connection` - Connection type (ssh, local, etc.)
327
- **User**: `-u, --user` - Remote user for connections
328
- **Become**: `-b, --become` - Privilege escalation
329
- **Variables**: `-e, --extra-vars` - Additional variables
330
- **Limits**: `-l, --limit` - Host pattern restrictions
331
- **Tags**: `-t, --tags` - Run specific tags
332
- **Skip Tags**: `--skip-tags` - Skip specific tags