首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将不可见控制台输出打印到文件中

将不可见控制台输出打印到文件中
EN

Stack Overflow用户
提问于 2022-12-04 13:33:18
回答 2查看 22关注 0票数 0

我有一个可操作的剧本,打印目标machine.When的CPU利用率小于90%,我得到一条确定消息,如果超过90%,我应该在屏幕上得到不允许消息,并且当CPU利用率不确定时,我也会在Ansible主机上生成一个日志文件作为monitor.log。

我能够在控制台上生成输出,但无法将输出发送到日志文件。

我所创造的游戏手册是。

代码语言:javascript
复制
#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

产出:

代码语言:javascript
复制
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] => {}

请帮我把这个输出发送到一个文件中。

EN

回答 2

Stack Overflow用户

发布于 2022-12-04 13:36:05

要将输出发送到日志文件,可以使用"ansible-playbook“命令和"-l"选项指定日志文件,并使用"-v"选项来增加输出的详细程度。

例如:

代码语言:javascript
复制
ansible-playbook -l /var/log/monitor.log -v my_playbook.yml

这将在指定的日志文件中保存剧本的输出。您还可以通过增加"-v“选项的值来调整详细的级别,例如"-vvv”以获得更详细的输出。

您还可以使用游戏手册中的“调试”模块将消息打印到日志文件,使用"log_path“选项指定日志文件。例如:

代码语言:javascript
复制
    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

这将将消息打印到指定的日志文件,以及控制台输出。

希望这能有所帮助!

票数 1
EN

Stack Overflow用户

发布于 2022-12-04 19:43:43

要将输出发送到日志文件,可以在您的剧本中使用ansible.builtin.log模块。此模块允许您将消息记录到指定的文件中。

首先,您需要创建一个使用log模块将消息写入文件的任务。您可以通过将以下任务添加到您的游戏手册中来完成此任务:

代码语言:javascript
复制
- name: 'Write log message to file'
  log:
    msg: 'Memory utilization is not OK: {{ memhigusage.stdout }}%'
    path: /path/to/log/file/monitor.log

此任务将将消息Memory utilization is not OK: {{ memhigusage.stdout }}%写入指定路径中的文件monitor.log。您可以根据需要定制消息和日志文件的路径。

接下来,您需要指定何时执行此任务。在您的示例中,您希望在内存利用率不确定时写入日志消息,这意味着memsec.stdoutmem1sec.stdoutmem2sec.stdoutmem3sec.stdout变量大于或等于90。为此,可以将when条件添加到任务中,如下所示:

代码语言:javascript
复制
- 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

这将确保只有当内存利用率不确定时,日志消息才会写入文件。

希望这能帮上忙!谢谢

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74677085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档