英文:
Saving object name in a new variable ansible
问题
我想动态获取"/dev/sda2"的名称,以便将其用作ansible中用于扩展分区的变量。由于它是随机的,可以是sda或sdb,即:
有人知道我如何动态获取它吗?重点是获取包含"/"分区的设备名称。
我目前正在使用:
- name: 显示根文件系统的挂载设备
debug:
msg: "{{ ansible_lvm.pvs }}"
register: mount_devices_result
它会打印整个对象,但我很难只获取设备名称,即(sda或sdb)。感谢帮助!
英文:
I would like to dynamically get the name of "/dev/sda2" so i can use it as a variable for ansible to grow the partition. Since its random and can be either sda or sdb i.e.
Anyone know how i can dynamicly fetch it? The point is to get the device name that holds the "/" partition.
Im currently using:
- name: Display mount devices for root filesystem
debug:
msg: "{{ ansible_lvm.pvs }}"
register: mount_devices_result
which prints the whole object, but i have a hard time to just get the device name, i.e (sda or sdb). Help is appriciated!
答案1
得分: 1
基于您之前提供的样本数据集,一个最小示例的Playbook如下:
---
- hosts: localhost
become: false
gather_facts: false
vars:
ansible_lvm:
pvs: { "/dev/sda2": '' }
tasks:
- debug:
msg: "{{ ansible_lvm.pvs.keys() | first }}"
将从ansible_lvm.pvs
字典中获取所有键并仅过滤序列的第一项。
TASK [debug] *****
ok: [localhost] =>
msg: /dev/sda2
进一步阅读
英文:
Based on your former provided sample data set, a minimal example playbook
---
- hosts: localhost
become: false
gather_facts: false
vars:
ansible_lvm:
pvs: { "/dev/sda2": '' }
tasks:
- debug:
msg: "{{ ansible_lvm.pvs.keys() | first }}"
will get all keys from the ansible_lvm.pvs
dict as list and filter the first item of the sequence only.
TASK [debug] *****
ok: [localhost] =>
msg: /dev/sda2
Further Reading
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论