将对象名称保存在一个新变量 ansible 中。

huangapple go评论72阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年7月20日 19:03:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定