-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnsibleCommands
More file actions
130 lines (76 loc) · 4.35 KB
/
Copy pathAnsibleCommands
File metadata and controls
130 lines (76 loc) · 4.35 KB
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
ansible virtualmachines -m ping -i inventory.yaml
ansible-inventory -i inventory.yaml --list
ansible-playbook -i inventory.yml playbook-01.yml -u kevin
root@asus:/home/kevin# ansible --version
ansible 2.10.8
config file = None
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0]
root@asus:/home/kevin#
ansible all -a "hostname"
ansible virtualmachines -a "hostname"
ansible example -m ping -u [username]
…where [username] is the user you use to log into the server. If everything worked, you should see a
message that shows www.example.com | success >>, then the result of your ping. If it didn’t work,
run the command again with -vvvv on the end to see verbose output. Chances are you don’t have
SSH keys configured properly—if you login with ssh username@www.example.com and that works,
the above Ansible command should work, too.
Ansible assumes you’re using passwordless (key-based) login for SSH (e.g. you login by
entering ssh username@example.com and don’t have to type a password). If you’re still
logging into your remote servers with a username and password, or if you need a primer
on Linux remote authentication and security best practices, please read Chapter 10 - Server
Security and Ansible. If you insist on using passwords, add the --ask-pass (-k) flag to
Ansible commands (you may also need to install the sshpass package for this to work).
Let’s run a more useful command:
$ ansible example -a "free -m" -u [username]
you have to define your hosts in the /etc/ansible/hosts file on your control server:
root@asus:/etc/ansible# cat hosts
[labhosts]
192.168.122.101
192.168.122.102
192.168.122.103
[vpnhosts]
10.147.18.1
10.147.18.14
10.147.18.72
root@asus:/etc/ansible#
then we can do:
kevin@asus:~/LOCAL/WORK/AnsibleLab/mylan$ ansible vpnhosts -a "hostname"
[DEPRECATION WARNING]: Distribution ubuntu 22.04 on host 10.147.18.14 should use /usr/bin/python3, but is using /usr/bin/python for backward compatibility
with prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.10/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in version 2.12.
Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
10.147.18.14 | CHANGED | rc=0 >>
asus
10.147.18.72 | CHANGED | rc=0 >>
ip-172-31-82-94
10.147.18.1 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host 10.147.18.1 port 22: No route to host",
"unreachable": true
}
kevin@asus:~/LOCAL/WORK/AnsibleLab/mylan$
The above is correct since 10.147.18.1 the lenvpn is switched off right now.
ansible has found two machines - the aws server and the asus laptop, via the vpn lan network.
another ad-hoc command:
kevin@asus:~/LOCAL/WORK/AnsibleLab/mylan$ ansible vpnhosts -a "free"
[DEPRECATION WARNING]: Distribution ubuntu 22.04 on host 10.147.18.14 should use /usr/bin/python3, but is using /usr/bin/python for backward compatibility
with prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.10/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in version 2.12.
Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
10.147.18.14 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 18368156 6803544 1201804 183504 10362808 11027232
Swap: 2097148 19712 2077436
10.147.18.1 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host 10.147.18.1 port 22: No route to host",
"unreachable": true
}
10.147.18.72 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 989388 442784 69928 69884 476676 291700
Swap: 0 0 0
kevin@asus:~/LOCAL/WORK/AnsibleLab/mylan$