带 && 的 awk 命令

带 && 的 awk 命令

我想做的是将 awk 与 Ansible 结合使用,这样我就可以获得大于 50% 的主机名和 vlume

然后我可以运行以下命令来获取超过 50% 的音量

ansible-playbook pb-servermgmt_runcommand_goce.yml --extra-vars "hostlist=test01 command='df -hlT -x tmpfs -x devtmpfs -x iso9660 --total'" | awk '0+$6 >= 50 {print}'
        "/dev/mapper/rhel-root xfs    50G   35G   16G  70% /",

我可以运行以下命令来显示服务器名称

ansible-playbook pb-servermgmt_runcommand_goce.yml --extra-vars "hostlist=test01 command='df -hlT -x tmpfs -x devtmpfs -x iso9660 --total'" | awk '/ok/ {print}'
ok: [test01] => {
test01                 : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

我想要以下输出

ok: [test01] => {
        "/dev/mapper/rhel-root xfs    50G   35G   16G  70% /",

我正在查看,&&但我只是没有得到写入语法。有任何想法吗 ?

答案1

问:“获取主机名和使用率超过 50% 的卷”

答:而不是awk使用过滤器社区.general.jc解析命令的输出。注册命令的输出

    - command: "{{ command }}"
      register: out

并声明变量

    my_df: "{{ out.stdout|community.general.jc('df') }}"

例如,下面的剧本

- hosts: "{{ hostlist }}"

  vars:

    my_df: "{{ out.stdout|community.general.jc('df') }}"

  tasks:

    - command: "{{ command }}"
      register: out

    - debug:
        var: my_df

给出(删节)

shell> ansible-playbook pb.yml -e "hostlist=test01 command='df -hlT --total'"

PLAY [test01] *************************************************************************************

TASK [command] ************************************************************************************
changed: [test01]

TASK [debug] **************************************************************************************
ok: [test01] => 
  my_df:
  - available: 1
    filesystem: devtmpfs
    mounted_on: /dev
    size: 1.9G
    type: devtmpfs
    use_percent: 0
    used: 0
  - available: 1
    filesystem: tmpfs
    mounted_on: /dev/shm
    size: 1.9G
    type: tmpfs
    use_percent: 0
    used: 0
  - available: 1
    filesystem: tmpfs
    mounted_on: /run
    size: 1.9G
    type: tmpfs
    use_percent: 11
    used: 193
  - available: 1
    filesystem: tmpfs
    mounted_on: /sys/fs/cgroup
    size: 1.9G
    type: tmpfs
    use_percent: 0
    used: 0
  - available: 2
    filesystem: /dev/mapper/cl-root
    mounted_on: /
    size: 18G
    type: xfs
    use_percent: 86
    used: 15
  - available: 820
    filesystem: /dev/sda2
    mounted_on: /boot
    size: 1014M
    type: xfs
    use_percent: 20
    used: 195
  - available: 592
    filesystem: /dev/sda1
    mounted_on: /boot/efi
    size: 599M
    type: vfat
    use_percent: 2
    used: 7
  - available: 374
    filesystem: tmpfs
    mounted_on: /run/user/1000
    size: 374M
    type: tmpfs
    use_percent: 0
    used: 0
  - available: 12
    filesystem: total
    mounted_on: '-'
    size: 27G
    type: '-'
    use_percent: 58
    used: 16
...

然后,您可以使用该列表我的_df选择或者拒绝你想要什么。看金贾测试。例如,选择所有类型的文件系统xfs其中使用率超过50%

    my_df_xfs_50: "{{ my_df|selectattr('type', '==', 'xfs')|
                            selectattr('use_percent', '>', 50)}}"

,或选择文件系统/dev/mapper/cl-root

    my_df_root: "{{ my_df|selectattr('filesystem', '==', '/dev/mapper/cl-root') }}"

在此示例中,两者给出相同的结果

  my_df_xfs_50:
  - available: 2
    filesystem: /dev/mapper/cl-root
    mounted_on: /
    size: 18G
    type: xfs
    use_percent: 86
    used: 15
  my_df_root:
  - available: 2
    filesystem: /dev/mapper/cl-root
    mounted_on: /
    size: 18G
    type: xfs
    use_percent: 86
    used: 15

写入主机名和使用率超过 50% 的卷

声明下面的字典

  report: "{{ dict(ansible_play_hosts|
                   zip(ansible_play_hosts|
                       map('extract', hostvars, 'my_df_xfs_50'))) }}"

调试

    - set_fact:
        my_df_xfs_50: "{{ my_df_xfs_50 }}"
    - debug:
        msg: |
          {{ report }}
      run_once: true

给出

    test01:
    - available: 2
      filesystem: /dev/mapper/cl-root
      mounted_on: /
      size: 18G
      type: xfs
      use_percent: 86
      used: 15

看:


问:“不是已经可以通过设置模块从 ansible 事实中获取该信息了吗?”

答:是的。而不是命令df,可以使用ansible_mounts收集者设置。此列表不提供使用百分比。使用此属性更新列表。然后,下面的剧本

shell> cat pb.yml
- hosts: "{{ hostlist }}"

  vars:

    ansible_mounts_update_str: |
      {% for i in ansible_mounts %}
      {% set use_percent = 100 - i.size_available / i.size_total * 100 %}
      - {{ i|combine({'use_percent': use_percent|round|int}) }}
      {% endfor %}
    ansible_mounts_update: "{{ ansible_mounts_update_str|from_yaml }}"

    my_df_xfs_50: "{{ ansible_mounts_update|
                      selectattr('fstype', '==', 'xfs')|
                      selectattr('use_percent', '>', 50) }}"
    my_df_root: "{{ ansible_mounts_update|
                    selectattr('device', '==', '/dev/mapper/cl-root') }}"

    report: "{{ dict(ansible_play_hosts|
                     zip(ansible_play_hosts|
                         map('extract', hostvars, 'my_df_xfs_50'))) }}"

  tasks:

    - setup:
        gather_subset: mounts
    - debug:
        var: ansible_mounts
    - debug:
        var: ansible_mounts_update

    - debug:
        var: my_df_xfs_50
    - debug:
        var: my_df_root

    - set_fact:
        my_df_xfs_50: "{{ my_df_xfs_50 }}"
    - debug:
        msg: |
          {{ report }}
      run_once: true

给出(删节的)相同的结果

shell> ansible-playbook pb.yml -e "hostlist=test01"

PLAY [test01] *************************************************************************************

TASK [setup] **************************************************************************************
ok: [test01]

...

TASK [debug] **************************************************************************************
ok: [test01] => 
  my_df_xfs_50:
  - block_available: 617954
    block_size: 4096
    block_total: 4561408
    block_used: 3943454
    device: /dev/mapper/cl-root
    fstype: xfs
    inode_available: 4944209
    inode_total: 5351696
    inode_used: 407487
    mount: /
    options: rw,seclabel,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota
    size_available: 2531139584
    size_total: 18683527168
    use_percent: 86
    uuid: 2cb4d983-108f-40f3-bc22-8afae8edc80a

TASK [debug] **************************************************************************************
ok: [test01] => 
  my_df_root:
  - block_available: 617954
    block_size: 4096
    block_total: 4561408
    block_used: 3943454
    device: /dev/mapper/cl-root
    fstype: xfs
    inode_available: 4944209
    inode_total: 5351696
    inode_used: 407487
    mount: /
    options: rw,seclabel,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota
    size_available: 2531139584
    size_total: 18683527168
    use_percent: 86
    uuid: 2cb4d983-108f-40f3-bc22-8afae8edc80a

TASK [set_fact] ***********************************************************************************
ok: [test01]

TASK [debug] **************************************************************************************
ok: [test01] => 
  msg:
    test01:
    - block_available: 617954
      block_size: 4096
      block_total: 4561408
      block_used: 3943454
      device: /dev/mapper/cl-root
      fstype: xfs
      inode_available: 4944209
      inode_total: 5351696
      inode_used: 407487
      mount: /
      options: rw,seclabel,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota
      size_available: 2531139584
      size_total: 18683527168
      use_percent: 86
      uuid: 2cb4d983-108f-40f3-bc22-8afae8edc80a

PLAY RECAP ****************************************************************************************
test01: ok=7    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

相关内容