Ansible Setup and Usage¶
Initially¶
add hosts to ~/.ssh/config
this allows you to specify everything you need to either ssh or use tools like Ansible. Then in /etc/ansible/hosts
specify the same shorthand name you used in the ssh config file.
After setup in both places, run a ping using the Ansible ping module: ansible -m ping all
{in place of all, you can specify the group used in the ansible hosts file.}
germany | FAILED! => {
"changed": false,
"module_stderr": "Shared connection to x.x.x.x closed.\r\n",
"module_stdout": "/bin/sh: 1: /usr/bin/python: not found\r\n",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 127
}
to fix, ssh -> install python2 / python2-minimal
$ ssh germany
user@germany ~ # apt update -y ; apt install python -y
{apt installs python2}
user@germany ~ # exit
$ ansible -m ping all
lax | SUCCESS => {
"changed": false,
"ping": "pong"
}
kc | SUCCESS => {
"changed": false,
"ping": "pong"
}
buf | SUCCESS => {
"changed": false,
"ping": "pong"
}
aws | SUCCESS => {
"changed": false,
"ping": "pong"
}
nor | SUCCESS => {
"changed": false,
"ping": "pong"
}
au | SUCCESS => {
"changed": false,
"ping": "pong"
}
germany | SUCCESS => {
"changed": false,
"ping": "pong"
}
Installing fail2ban and starting with os [ubuntu]¶
# pi @ pi4-1tb in ~/projects/ansible on git:master x [13:07:37]
$ cat apt-update.yml
--- # Fail2ban
- hosts: all
become: yes
tasks:
- name: Update Ubuntu cache
apt:
update_cache: yes
force_apt_get: True
- name: Update Ubuntu packages
apt:
name: "*"
state: latest
force_apt_get: True
- name: Apt autoremove
apt:
autoremove: yes
force_apt_get: True
- name: install essentials
apt:
name:
- htop
- fail2ban
- git
- curl
- sshfs
- unzip
- tar
- name: enable fail2ban at boot
service:
name: fail2ban
enabled: yes
Error with RHEL¶
$ ansible vm -m ping
git | FAILED! => {
"changed": false,
"module_stderr": "Shared connection to 192.168.11.15 closed.\r\n",
"module_stdout": "/bin/sh: /usr/bin/python2: No such file or directory\r\n",
"msg": "The module failed to execute correctly, you probably need to set the interpreter.\nSee stdout/stderr for the exact error",
"rc": 127
}
Waht the problem is? One of two things. Python is not installed - in that case, ssh@host
& sudo yum install python2 -y
OR add a vars to your /etc/ansible/hosts
file:
[vm:vars]
ansible_python_interpreter=/usr/bin/python2
Now it works:
$ ansible vm -m ping
dns | SUCCESS => {
"changed": false,
"ping": "pong"
}
factorio | SUCCESS => {
"changed": false,
"ping": "pong"
}
docker | SUCCESS => {
"changed": false,
"ping": "pong"
}
git | SUCCESS => {
"changed": false,
"ping": "pong"
}