English 中文(简体)
事实的最后性质
原标题:ansible get the last character of a fact

我最后想谈谈事实。 我的游戏画着这一点:

- name: sf
  set_fact:
    root_dev: "{{ ansible_mounts|json_query( [?mount == `/`].device ) }}"

- name: e
  debug:
    msg: "{{ root_dev[:-1] }}"

问题始终是:

"msg": []

or if I use the playbook without semicolon:

  debug:
    msg: "{{ root_dev[-1] }}"

那么,整个分部分将是产出:

"msg": "/dev/sda1"

我还可以引用整个<代码>root_dev/code>。 因为它是事实,我希望得到它价值的最后特性。 <代码>split 过滤器也不适用于此情形,因为该装置可以是/dev/sda/dev/mapper/root_part_0等。 本案的最佳选择是什么?

最佳回答

问题解释:

你们必须理解:slicing Works。 鉴于l1l2用于检测

  l1: [a]
  l2: [a, b, c]

指数[-1]给你最后一个项目(也是第一个项目)

  l1[-1]: a

斜体[:-1]除最后一条外,给你所有物品。 结果是一份空洞的清单,因为只有一个项目。

l1[:-1]: []

查阅l2

 l2[:-1]: [a, b]

解决办法:

root_dev是一份清单

  root_dev: "{{ ansible_mounts | json_query( [?mount == `/`].device ) }}"

阁下

  root_dev: [ /dev/sda1 ]

获取分部分数,将清单中的第一个项目和该项目的最后一个特性索引

  root_dev[0][-1]: 1

  • Example of a complete playbook for testing
- hosts: localhost

  vars:

    root_dev: "{{ ansible_mounts | json_query( [?mount == `/`].device ) }}"

  tasks:

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

    - debug:
        var: root_dev
    - debug:
        var: root_dev[0][-1]

  • The above solution works for a single-digit partitions only. Use regex_replace to cover also multiple-digit options
  part_no: "{{ root_dev.0 | regex_replace( ^(.+?)(\d*)$ ,  \2 ) }}"
问题回答

它更多地涉及沙捞取,而不是圣经:

- set_fact:
    root_dev: "/dev/sda1"

- debug:
    msg: "{{ root_dev[-1::] }}"
ok: [localhost] => {
    "msg": "1"
}

See https://docs.python.org/3/library/Functions.html#slice for details.

EDIT:实际上存在问题。

正如你在评论中提到的那样,你最初的询问提供了一份清单,并附有<编码>root_dev:“{......一个可操作的_mount json_query(?mount=== / device)>,代之以斜体。

但是,我的建议是使用金雅2过滤器,而不是json_query:requires jmespath,另行安装<:

- set_fact:
    root_dev: "{{ (ansible_mounts | selectattr( mount ,  equalto ,  / ) | first).device }}"




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签