En función de la necesidad, podemos crear un Playbook o podemos ejecutar directamente comandos.
Para la ejecución de comandos, detallo varios ejemplos:
ansible all -m ping -u root
ansible -m shell -a 'yum -y update' all
ansible -m shell -a 'package-cleanup --oldkernels --count=1' all
ansible -m shell -a 'yum -y install yum-utils mlocate sysstat' all
ansible -m shell -a 'systemctl start sysstat.service' all
ansible -m shell -a 'systemctl enable sysstat.service' all
ansible -m shell -a 'yum clean all' all
ansible -m shell -a 'free -g' all
ansible -m shell -a 'echo $HOSTNAME' LINUX
ansible -m shell -a 'shutdown -r +5 &' all
Por otra parte, podemos crear un Playbook. Vamos a ver diferentes ejemplos:
Mostrar versión Kernel en grupo RPI
cat > kernel_info.yml << EOF
- hosts: RPI
tasks:
- name: "View current kernel version"
command: uname -mrs
register: uname_result
- name: print
debug:
msg: "{{ uname_result }}"
EOF
ansible-playbook kernel_info.yml
Creación de carpetas y asignación de permisos en todos los equipos
cat > create_folders.yml << EOF
- hosts:
all
tasks:
- name: CREATE_FOLDER
file:
path: "{{ item }}"
state: directory
owner: root
group: root
mode: 0700
recurse: yes
loop:
- /scripts
- /backups
EOF
ansible-playbook create_folders.yml
Modificar valor SELinux a Permissive
cat > SELinux_Permissive.yml << EOF
- name: Modificar SELinux
lineinfile:
path: /etc/selinux/config
regexp: '^SELINUX='
line: SELINUX=permissive
EOF
ansible-playbook SELinux_Permissive.yml CENTOS
Modificar valor PermitRootLogin en SSH
cat > PermitRootLogin.yml << EOF
- hosts: all
gather_facts: no
tasks:
- name: Enable Root Login
lineinfile:
dest: /etc/ssh/sshd_config
regexp: '^PermitRootLogin'
line: "PermitRootLogin yes"
state: present
backup: yes
become: yes
notify:
- restart ssh
handlers:
- name: restart ssh
systemctl:
name: sshd
state: restarted
EOF
ansible-playbook PermitRootLogin.yml
Obtener espacio Filesystems de un nodo
cat > disk_size.yml << EOF
- name: Check size FileSystem
hosts: 10.0.1.71
tasks:
- name: Disk
shell: df -Ph | grep '^/dev/' | awk '{print $5" "$NF}'
register: space
- debug:
var: space.stdout_lines
EOF
ansible-playbook disk_size.yml