Home > Software engineering >  Printing Ansible Console output to a file
Printing Ansible Console output to a file

Time:12-05

I Have a Ansible playbook which prints the CPU utilisation of target machine.When the CPU utilization is less than 90% , I get a OK message and if more than 90% , I should get not okay message on screen and also generate a log file as monitor.log on the Ansible host machine when CPU utilization is not okay.

I am able to generate the output on the console but I am not able to send this output to a log file.

The Ansible playbook that I have created is.

#CPU callculation

- name: Setup Nginx server on myserver list
  hosts: myservers
  become: True
  tasks:

    - name: 'copy Get-Memory-Utilization.sh script to {{ inventory_hostname }}'
      copy:
        src:  /home/ec2-user/Memory-Utilization.sh

        dest: /tmp
        mode: '0775'

    - name: 'Preparing Memory utilization using script results'
      shell: |
        sh /tmp/Memory-Utilization.sh
      register: memsec


    - name: 'Preparing Memory utilization for 1st sec'
      shell: |
        sh /tmp/Memory-Utilization.sh
      register: mem1sec


    - name: 'Preparing Memory utilization for 2nd sec'
      shell: |
        sh /tmp/Memory-Utilization.sh
      register: mem2sec


    - name: 'Preparing Memory utilization for 3rd sec'
      shell: |
        sh /tmp/Memory-Utilization.sh
      register: mem3sec


    - name: 'Prepare Memory Used percentage if its abnormal'
      shell: |
        sh /tmp/Memory-Utilization.sh
      register: memhigusage
      when: memsec.stdout|int >= 90 or mem1sec.stdout|int >= 90 or mem2sec.stdout|int >= 90 or mem3sec.stdout|int >= 90

    - name: 'Print message if MEMORY utilization is  normal'
      debug:
        msg:
          - -------------------------------------------------------
          -  Memory Utilization = ( ( Total - Free ) / Total * 100 ) = {{ memsec.stdout }}%
          - -------------------------------------------------------
      when: memsec.stdout|int < 90 and mem1sec.stdout|int < 90 and mem2sec.stdout|int < 90 and mem3sec.stdout|int < 90

    - name: 'Print message if MEMORY utilization is  abnormal'
      debug:
        msg:
           - -------------------------------------------------------
           - Memory Utilization = ( ( Total - Free ) / Total * 100 ) = {{ memhigusage.stdout }}%
           - -------------------------------------------------------
      when: memsec.stdout|int >= 90 or mem1sec.stdout|int >= 90 or mem2sec.stdout|int >= 90 or mem3sec.stdout|int >= 90
         


output:


TASK [Print message if MEMORY utilization is  normal] *************************************************************************************************************************************************************
ok: [44.203.153.54] => {
    "msg": [
        "-------------------------------------------------------", 
        "Memory Utilization = ( ( Total - Free ) / Total * 100 ) = 13.87%", 
        "-------------------------------------------------------"
    ]
}

TASK [Print message if MEMORY utilization is  abnormal] ***********************************************************************************************************************************************************
skipping: [44.203.153.54] => {}


Please help me to send this output to a file.

CodePudding user response:

To send the output to a log file, you can use the "ansible-playbook" command with the "-l" option to specify the log file and the "-v" option to increase the verbosity of the output.

For example:

ansible-playbook -l /var/log/monitor.log -v my_playbook.yml

This will save the output of the playbook in the specified log file. You can also adjust the verbosity level by increasing the value of the "-v" option, for example "-vvv" for more detailed output.

You can also use the "debug" module in your playbook to print messages to the log file, using the "log_path" option to specify the log file. For example:

    name: 'Print message if MEMORY utilization is normal'
debug:
msg:
- -------------------------------------------------------
- Memory Utilization = ( ( Total - Free ) / Total * 100 ) = {{ memsec.stdout }}%
- -------------------------------------------------------
log_path: /var/log/monitor.log
when: memsec.stdout|int < 90 and mem1sec.stdout|int < 90 and mem2sec.stdout|int < 90 and mem3sec.stdout|int < 90

This will print the message to the specified log file, in addition to the console output.

Hope this helps!

CodePudding user response:

To send the output to a log file, you can use the ansible.builtin.log module in your playbook. This module allows you to log messages to a specified file.

First, you need to create a task that uses the log module to write a message to a file. You can do this by adding the following task to your playbook:

- name: 'Write log message to file'
  log:
    msg: 'Memory utilization is not OK: {{ memhigusage.stdout }}%'
    path: /path/to/log/file/monitor.log

This task will write the message Memory utilization is not OK: {{ memhigusage.stdout }}% to the file monitor.log in the specified path. You can customize the message and the path to the log file as needed.

Next, you need to specify when this task should be executed. In your case, you want to write a log message when the memory utilization is not OK, which means the memsec.stdout, mem1sec.stdout, mem2sec.stdout, or mem3sec.stdout variable is greater than or equal to 90. To do this, you can add the when condition to your task, as shown below:

- name: 'Write log message to file'
  log:
    msg: 'Memory utilization is not OK: {{ memhigusage.stdout }}%'
    path: /path/to/log/file/monitor.log
  when: memsec.stdout|int >= 90 or mem1sec.stdout|int >= 90 or mem2sec.stdout|int >= 90 or mem3sec.stdout|int >= 90

This will ensure that the log message is only written to the file when the memory utilization is not OK.

I hope this helps!! Thanks

  • Related