Using Ansible – Getting Started

You are here:
< All Topics

 

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 example -m ping -u [username]

 

…where [username] is the user  to log into the server. If everything worked, you will see a
message that shows www.example.com | success >>, followed by the result of your ping.

 

If that didn’t work, run the command again using -vvvv on the end to display more verbose output.

 

 

 

 

Ansible assumes you’re using passwordless (key-based) login for SSH connections (e.g. you login by
entering ssh username@example.com – without typing a password).

 

If you’re still logging into your machines with a username and password,  add the –ask-pass (-k) flag to
Ansible commands. You may need to install the sshpass package for this to work properly.

 

Define your hosts in in /etc/ansible/hosts  

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

 

 

 

Then, you need to place the hosts in your inventory file as well:

 

 

kevin@asus:~/LOCAL/WORK/AnsibleLab/mylan$ cat inventory.yml
vpnhosts:
hosts:
asusvpn:
ansible_host: 10.147.18.14
lenvpn:
ansible_host: 10.147.18.1
kevinvm1vpn:
ansible_host: 10.147.18.72
children:
webservers:
hosts:
kevinvm1vpn:
laptops:
hosts:
asusvpn:
lenvpn:
kevin@asus:~/LOCAL/WORK/AnsibleLab/mylan$

 

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$

 

 

 

Tags:
Table of Contents