automationansiblerhel

Malformed Block in Ansible


I'm trying to run a playbook that gathers yum history logs, and consolidates them on a single server. I ran this playbook

---
- name: Gather yum history from all hosts
  hosts: all
  become: true
  tasks:
    - name: Run 'yum history' command
      shell: yum history
      register: yum_history_output

    - name: Save yum history to local file
      copy:
        content: |
          Host: {{ inventory_hostname }}
          ========================================
          {{ yum_history_output.stdout }}
          ========================================
        dest: "/tmp/yum_history_{{ inventory_hostname }}.log"

- name: Fetch all yum history logs to control node
  hosts: all
  gather_facts: false
  tasks:
    - name: Fetch log files to control node
      fetch:
        src: "/tmp/yum_history_{{ inventory_hostname }}.log"
        dest: "./yum_history_logs/"
        flat: yes

and, after some formatting, got a malformed block error which just seemed to put the whole playbook into a single block. What am I doing wrong?

I was given a bunch of formatting issues that I fixed, mostly deleting a lot of spaces because it didn't like the spaces in vi, but now I get a malformed block, and have no idea what to do next.


Solution

  • Write the files at the controller. Delegate the copy task to localhost. For example, given the registered data for testing

        - command: echo "yum history"
          register: yum_history_output
    

    Create the files

        - copy:
            dest: "/tmp/yum_history_{{ inventory_hostname }}.log"
            content: |
              Host: {{ inventory_hostname }}
              ========================================
              {{ yum_history_output.stdout }}
              ========================================
          delegate_to: localhost
    

    gives

    shell> ls -1 /tmp | grep yum
    yum_history_test_01.log
    yum_history_test_02.log
    yum_history_test_03.log
    
    shell> cat /tmp/yum_history_test_01.log 
    Host: test_01
    ========================================
    yum history
    ========================================
    
    shell> cat /tmp/yum_history_test_02.log 
    Host: test_02
    ========================================
    yum history
    ========================================
    
    shell> cat /tmp/yum_history_test_03.log 
    Host: test_03
    ========================================
    yum history
    ========================================
    

    Example of a complete playbook for testing

    shell> cat pb.yml
    - hosts: all
    
      tasks:
    
        - command: echo "yum history"
          register: yum_history_output
    
        - copy:
            dest: "/tmp/yum_history_{{ inventory_hostname }}.log"
            content: |
              Host: {{ inventory_hostname }}
              ========================================
              {{ yum_history_output.stdout }}
              ========================================
          delegate_to: localhost
    
    shell> ansible-playbook pb.yml 
    
    PLAY [all] **********************************************************************************************************
    
    TASK [command] ******************************************************************************************************
    changed: [test_01]
    changed: [test_03]
    changed: [test_02]
    
    TASK [copy] *********************************************************************************************************
    changed: [test_01 -> localhost]
    changed: [test_02 -> localhost]
    changed: [test_03 -> localhost]
    
    PLAY RECAP **********************************************************************************************************
    test_01                    : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    test_02                    : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    test_03                    : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0