首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ansible:迭代捕获的命令输出

Ansible:迭代捕获的命令输出
EN

Stack Overflow用户
提问于 2020-04-09 01:05:48
回答 2查看 98关注 0票数 0

我正在尝试将现有的Perl脚本转换为Ansible角色。在迭代捕获的命令输出时,我遇到了麻烦。

下面是Perl脚本:

代码语言:javascript
复制
# Description: This script will adjust the oom score of all the important system processes to a negative value so that OOM killer does not touch these processes ############


chomp(my $OS = `uname`);

if($OS eq "Linux")
{
  my @file = `ps -ef|egrep 'sssd|wdmd|portreserve|autofs|automount|ypbind|rpcbind|rpc.statd|rpc.mountd|rpc.idampd|ntpd|lmgrd|Xvnc|vncconfig|irqblance|rpc.rquotad|metric|nscd|crond|snpslmd|getpwname.pl|mysqld|rsyslogd|xinetd|sendmail|lsf|tigervnc|tightvnc|cfadm' |egrep -ve 'ps|egrep' |awk '{print \$8,\$2}'`;
  chomp(@file);

  foreach my $element (@file)
  {
    chomp($element);
    (my $process, my $pid) = (split(/\s/,$element))[0,1];
    print "($process)($pid)\n";
    system("echo -17 > /proc/$pid/oom_adj");
    system("cat /proc/$pid/oom_adj");
  }
}

else
{
  print "The host is a $OS system, so no action taken\n";
}

以下是我到目前为止在Ansible中尝试过的内容:

代码语言:javascript
复制
---
  - name: Capture uname ouput
    shell: "uname"
    register: os_type

  - name: Adjust OOM to negative so that OOM killer does not kill below processes
    shell: 'ps -ef|egrep "sssd|wdmd|portreserve|autofs|automount|ypbind|rpcbind|rpc.statd|rpc.mountd|rpc.idampd|ntpd|lmgrd|Xvnc|vncconfig|irqblance|rpc.rquotad|metric|nscd|crond|snpslmd|getpwname.pl|mysqld|rsyslogd|xinetd|sendmail|lsf|tigervnc|tightvnc|cfadm" |egrep -ve "ps|egrep" |awk "{print \$8,\$2}"'
    register: oom
    when: os_type.stdout == 'Linux'

  - debug:  var=oom.stdout_lines

现在,我想在var上迭代并在Ansible中实现这个部分:

代码语言:javascript
复制
foreach my $element (@file)
  {
    chomp($element);
    (my $process, my $pid) = (split(/\s/,$element))[0,1];
    print "($process)($pid)\n";
    system("echo -17 > /proc/$pid/oom_adj");
    system("cat /proc/$pid/oom_adj");
  }

请帮帮忙。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-29 18:38:23

我想出了办法。下面是对我有用的解决方案。感谢所有想帮我的人。(赞赏:)

代码语言:javascript
复制
---
  - name: Capture uname ouput
    shell: "uname"
    register: os_type
  - name: Gather important processes
    shell: 'ps -ef|egrep "sssd|wdmd|portreserve|autofs|automount|ypbind|rpcbind|rpc.statd|rpc.mountd|rpc.idampd|ntpd|lmgrd|Xvnc|vncconfig|irqblance|rpc.rquotad|metric|nscd|crond|snpslmd|getpwname.pl|mysqld|rsyslogd|xinetd|sendmail|lsf|tigervnc|tightvnc|cfadm" |egrep -ve "ps|egrep" |awk "{print \$8,\$2}"'
    register: oom
    when: os_type.stdout == 'Linux'
  - name: Adjust OOM to negative so that OOM killer does not kill important processes
    shell: "echo -17 >> /proc/{{ item.split()[1] }}/oom_adj"
    loop: "{{ oom.stdout_lines }}"
    register: echo
  - set_fact:
      stdout_lines: []
  - set_fact:
      stdout_lines: "{{ stdout_lines + item.stdout_lines }}"
    with_items: "{{ echo.results }}"
  - debug:
      msg: "This is a stdout line: {{ item }}"
    with_items: "{{ stdout_lines }}"
票数 0
EN

Stack Overflow用户

发布于 2020-04-10 01:06:59

下面为我工作

代码语言:javascript
复制
- hosts: temp
  gather_facts: yes
  remote_user: root
  tasks:

   - name: Adjust OOM to negative so that OOM killer does not kill below processes
     shell: 'ps -ef|egrep "sssd|wdmd|portreserve|autofs|automount|ypbind|rpcbind|rpc.statd|rpc.mountd|rpc.idampd|ntpd|lmgrd|Xvnc|vncconfig|irqblance|rpc.rquotad|metric|nscd|crond|snpslmd|getpwname.pl|mysqld|rsyslogd|xinetd|sendmail|lsf|tigervnc|tightvnc|cfadm" |egrep -ve "ps|egrep" |awk "{print \$2}"'
     register: oom
     when: ansible_system == 'Linux'

   - debug:  var=oom.stdout
   - name: update the pid
     raw: echo -17 > /proc/{{ item }}/oom_adj
     loop: "{{ oom.stdout_lines }}"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61112398

复制
相关文章

相似问题

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