Outils pour utilisateurs

Outils du site


blog

Notes Ansible - test ports de connexion SSH

Voir aussi :

  • block / rescue

Context :

Le port par défaut pour SSH est le 22.

Nous changeons habituellement ce port en 2222.

Nous écrivons un playbook capable d’adresser les machines avec SSH configuré avec le port 2222 ainsi que celles configurées avec le port 22.

wrapper_ssh_ports.yml

---

- name: wrapper_both_ssh_ports
  hosts: all
  gather_facts: false
  vars:
    port_test_1: 2222
    port_test_2: 22

  tasks:
  
    - name: set_fact port_test_1
      set_fact:
        ansible_port: "{{ port_test_1 }}"

    - name: ping 1
      ignore_unreachable: true
      ping:
      register: ping_1

    - name: set_fact port_test_2
      when: not ( ping_1.ping is defined and ping_1.ping == 'pong' )
      set_fact:
        ansible_port: "{{ port_test_2 }}" 

    - name: ping 2
      when: not ( ping_1.ping is defined and ping_1.ping == 'pong' )
      ignore_unreachable: true
      ping:
      register: ping_2

    - name: fail when all ports NOK
      assert:
        that:
          - ( ping_1.ping is defined and ping_1.ping == 'pong' ) or ( ping_2.ping is defined and ping_2.ping == 'pong' )

        msg: "Fail unreachable target"
 

- import_playbook: play2.yml

play2.yml

- name: playbook to launch always - SSH port 2222 and 22
  hosts: all
  gather_facts: false

  tasks:

    - name: commande uptime
      command: uptime
      changed_when: false
      register: uptime

    - name: echo uptime
      debug:
        var: uptime.stdout
2025/03/24 15:06

Apt dpkg postinst debug

LANG=C apt-get -f install                                                                                                                                                          Err 100
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-headers-3.2.0-31-generic linux-headers-3.2.0-31 linux-headers-3.2.0-27 libclamav6 linux-headers-3.2.0-27-generic libtommath0
Use 'apt-get autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 6 not upgraded.
1 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Setting up grub-pc (1.99-21ubuntu3.20) ...
Killed
dpkg: error processing grub-pc (--configure):
 subprocess installed post-installation script returned error exit status 137
Errors were encountered while processing:
 grub-pc
E: Sub-process /usr/bin/dpkg returned an error code (1)
/var/lib/dpkg/info/grub-pc.postinst
/var/lib/dpkg/info/grub-pc.postrm
/var/lib/dpkg/info/grub-pc-bin.md5sums
/var/lib/dpkg/info/grub-pc-bin.list
/var/lib/dpkg/info/grub-pc.prerm
/var/lib/dpkg/info/grub-pc.config
/var/lib/dpkg/info/grub-pc.templates
/var/lib/dpkg/info/grub-pc.conffiles
/var/lib/dpkg/info/grub-pc.preinst
/var/lib/dpkg/info/grub-pc.md5sums
/var/lib/dpkg/info/grub-pc.list

/var/lib/dpkg/info/grub-pc.postinst

#!/bin/bash -x
2025/03/24 15:06

Notes Ansible - mount - prevent nested loops

Nous voulons interdire les montages imbriqués (montage dans un montage) \ Ou pour le dire autrement : Nous voulons interdire à un utilisateur de créer un point de montage dans un point de montage existant (autre que /).

playbook.yml

- name: is a nested mount ?
  become: true
  shell: |
    # will_be_mounted_on
    # Returns the mount point where the future file given in argument will be located
    set -euo pipefail
    TMP_PATH="{{ PLOP_ACCOUNT_HOME | quote }}"
    while [[ ! -e "$TMP_PATH" ]]
    do
        TMP_PATH="$(dirname $TMP_PATH)"
    done
    df -l --output=target "$TMP_PATH" | tail -1
  check_mode: false
  changed_when: false
  register: mounted_on
  when: PLOP_ACCOUNT_HOME is defined and PLOP_ACCOUNT_HOME != `

- name: assert fail on nested mount
  assert:
    that:
      - mounted_on.stdout_lines[-1] == '/' or mounted_on.stdout_lines[-1] == PLOP_ACCOUNT_HOME
2025/03/24 15:06

Notes Ansible - callback plugins

ansible/plugins/callback/

ansible.cfg

[defaults]
 
# change the default callback, you can only have one 'stdout' type  enabled at a time.
#stdout_callback = skippy
stdout_callback = kafka_logger
 
# Callback pluging activated for /usr/bin/ansible too
bin_ansible_callbacks = true
 
# enable callback plugins, they can output to stdout but cannot be 'stdout' type.
#callback_whitelist = timer, mail
callback_whitelist = timer, mail
 
# callback plugin path
# cp -p /usr/lib/python3.6/site-packages/ansible/plugins/callback/timer.py ansible/plugins/callback/
callback_plugins   = $PWD/ansible/plugins/callback
ansible-doc -t callback -l
2025/03/24 15:06
blog.txt · Dernière modification : de 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki