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.