tech:notes_ansible_module_raw

Notes Ansible module raw

Le module raw permet de passer des commandes en SSH directement sans avoir besoin de Python installé sur la cible.

En général se module est justement utilisé pour installer Python. Il est aussi utile quand la cible contient une version de Python obsolète.

Exemple de déploiement d'un script shell avec Raw

Sur une vielle RedHat 5 (Python obsolète) les fichiers crées par raw sont systématiquement tronqués à 6258 bytes. De plus certains caractères spéciaux du script shell empêche son déploiement via un heredoc.

Pour contourner ces deux limitations nous allons :

  • Découper le fichier en paquets de 6258 bytes
  • Encoder le fichier en base64

play-deploy-shell-old-linux.yml

#!/usr/bin/ansible-playbook
 
---

- hosts: all
  gather_facts: false
  environment:
    PATH: /bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/cmcluster/bin/
  tasks:
    - name: "Gather facts"
      ignore_unreachable: true
      block:
        - name: "Gather facts for RHEL > 5"
          ansible.builtin.setup:

        - name: set_fact python_value
          ansible.builtin.set_fact:
            python_value: auto_legacy

      rescue:
        - name: set_fact with_raw_module
          ansible.builtin.set_fact:
            with_raw_module: true

    - name: block when with_raw_module
      when: with_raw_module is defined and with_raw_module
      block:
        - name: DEBUG
          ansible.builtin.debug:
            var: item
          with_items:
            - "{% for host in hostvars %}{{ host }}{% endfor %}"

        - name: Split file
          ansible.builtin.shell: |
            cat script.sh | gzip | openssl base64 | split -b 6258 --additional-suffix .asc - script-
          args:
            chdir: files
          delegate_to: localhost

        - name: Find
          ansible.builtin.find:
            file_type: file
            paths: files/
            patterns: 'script-*'
          register: f
          delegate_to: localhost

        - name: Slurp
          ansible.builtin.slurp:
            src: "{{ item }}"
          register: slurp_shell_code
          delegate_to: localhost
          with_items:
            - "{{ f.files | map(attribute='path') |list }}"

        - name: Copy shell script
          ansible.builtin.raw: |
            cat > script.sh.asc.{{ ansible_loop.index0 }} <<-EOF
            {{ item.content | b64decode }}
            EOF
          args:
            executable: /bin/bash
          loop_control:
            extended: true
          with_items:
            - "{{ slurp_shell_code.results }}"

        - name: Mkdir /usr/local/plop/
          ansible.builtin.raw: sudo install -d -m 750 /usr/local/plop/

        - name: Merge file
          ansible.builtin.raw: cat script.sh.asc.* | openssl base64 -d | gzip -d > script.sh

        - name: Clean temp files
          ansible.builtin.raw: rm -f script.sh.asc.*

        - name: Mv shell script
          ansible.builtin.raw: sudo mv script.sh /usr/local/plop/script.sh

        - name: Launch script
          ansible.builtin.raw: sudo bash /usr/local/plop/script.sh
tech/notes_ansible_module_raw.txt · Dernière modification : de Jean-Baptiste

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki