[英]Extract specific data from an output of shell command in Ansible
I would like to create a playbook that extract the used
space for /boot
through the shell command df -B MB
.我想创建一个剧本,通过 shell 命令
df -B MB
为/boot
提取已used
空间。
For this example, the playbook will show 358MB
for the controller node.对于此示例,playbook 将为控制器节点显示
358MB
。
I would like to know how to filter the server_info.stdout_lines
in order to get the desired result.我想知道如何过滤
server_info.stdout_lines
以获得所需的结果。
I believe that we can do it with the regex_search
filter.我相信我们可以使用
regex_search
过滤器来做到这一点。
- hosts: all
gather_facts: True
become: True
tasks:
- name: show used space on /boot
shell: "df -B MB"
register: server_info
- debug:
msg: "{{ server_info.stdout_lines }}"
TASK [debug] *****************************************************************************************************************
ok: [control] => {
"msg": [
"Filesystem 1MB-blocks Used Available Use% Mounted on",
"devtmpfs 1458MB 0MB 1458MB 0% /dev",
"tmpfs 1488MB 1MB 1488MB 1% /dev/shm",
"tmpfs 1488MB 10MB 1478MB 1% /run",
"tmpfs 1488MB 0MB 1488MB 0% /sys/fs/cgroup",
"/dev/mapper/cs-root 19267MB 6268MB 13000MB 33% /",
"/dev/sda1 1064MB 358MB 706MB 34% /boot",
"tmpfs 298MB 1MB 298MB 1% /run/user/42",
"tmpfs 298MB 1MB 298MB 1% /run/user/1000"
]
}
ok: [ansible2] => {
"msg": [
"Filesystem 1MB-blocks Used Available Use% Mounted on",
"devtmpfs 1458MB 0MB 1458MB 0% /dev",
"tmpfs 1488MB 0MB 1488MB 0% /dev/shm",
"tmpfs 1488MB 10MB 1478MB 1% /run",
"tmpfs 1488MB 0MB 1488MB 0% /sys/fs/cgroup",
"/dev/mapper/cs-root 19267MB 6264MB 13003MB 33% /",
"/dev/sda1 1064MB 360MB 704MB 34% /boot",
"tmpfs 298MB 1MB 298MB 1% /run/user/42",
"tmpfs 298MB 1MB 298MB 1% /run/user/1000"
]
}
This is a typical x/y problem (although it is easy to answer since you have given your root requirement ie get used size on a mount point).这是一个典型的x/y 问题(尽管它很容易回答,因为您已经给出了根本要求,即在安装点上获得使用的大小)。
You don't have to use shell
to get that information.您不必使用
shell
来获取该信息。 It's all available in the target machine facts (just make sure you did not disable fact gathering with gather_fact: no
on your play).这一切都在目标机器事实中可用(只需确保您没有使用
gather_fact: no
)。
The example below is for /boot/efi
on my system.下面的示例适用于我系统上的
/boot/efi
。 Just put it back on the actual mount point you want to target in the selectattr
filter.只需将其放回您要在
selectattr
过滤器中定位的实际安装点。
---
- hosts: localhost
vars:
mount_point: /boot/efi
tasks:
- vars:
device_info: "{{ ansible_mounts | selectattr('mount', '==', mount_point) | first }}"
debug:
msg: "Used size on {{ mount_point }} is {{ (device_info.size_total - device_info.size_available) | human_readable }}"
gives:给出:
PLAY [localhost] **************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [localhost]
TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "Used size on /boot/efi is 29.98 MB"
}
PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.