如何通过 ansible 从 ansible 变量中删除前导零?
来自How to remove leading zeros from a variable? 我想运行以下功能,但无法运行。
$ a=01234
$ b=00001234
$ c=1234
$
$ echo "${a#${a%%[1-9]*}}"
1234
$ echo "${b#${b%%[1-9]*}}"
1234
$ echo "${c#${c%%[1-9]*}}"
1234
我的尝试是:
- shell: "echo ${% raw -%}{{% endraw -%}{{mpp_number_with_leading_zeros}}#${% raw -%}{{% endraw -%}{{mpp_number_with_leading_zeros}}%%[1-9]*{% raw -%}}{% endraw -%}{% raw -%}}{% endraw -%}"
我使用的是 ansible 2.9
答案 0 :(得分:3)
您可以使用 int
插件转换变量,如下所示:
vars:
var1: "01234"
var2: "00001234"
var3: "1234"
tasks:
- debug:
msg: "{{ item }}"
with_items:
- "{{ var1 }}"
- "{{ var2 }}"
- "{{ var3 }}"
- "{{ var1 | int }}"
- "{{ var2 | int }}"
- "{{ var3 | int }}"
文档: https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#forcing-the-data-type