Ansible 仅从预加载的 ansible 变量中提取 IP

问题描述 投票:0回答:1

我尝试从变量

"{{ ansible_eth0.ipv4.address }}"
中仅提取IP,但没有成功。这让我回归
['192.168.1.1']

我需要使用 ansible 变量删除

"[' ']"
,以便仅将 IP 添加到具有
lineinfile
模块的文件中。

module ansible ip
1个回答
0
投票

根据评论,他们是对的,我认为你输出 ansible 变量是错误的。使用内置

debug
模块,您将仅获取不在列表中的 IP 地址。示例手册:

---
- name: write out ansible_ens192.ipv4.address value
  hosts: localhost

  tasks:
  - name: Print the IP address
    ansible.builtin.debug:
      msg: "IP address is: {{ ansible_ens192.ipv4.address }}"

将产生以下输出:

PLAY [write out ansible_ens192.ipv4.address value] **************************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Print the IP address] *************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "IP address is: 10.0.1.7"
}

PLAY RECAP ******************************************************************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

看起来不错。

但是,如果我将你的变量“破解”到列表中,并尝试将其写出来:

---
- name: write out ansible_ens192.ipv4.address value
  vars:
    ipaddr:
      - "192.168.1.1"
  hosts: localhost

  tasks:
  - name: Print the IP address in a list
    ansible.builtin.debug:
      msg: "IP address is: {{ ipaddr }}"

  - name: Print the IP address sanitized
    ansible.builtin.debug:
      msg: "IP address is: {{ ipaddr | join }}"

然后用一个简单的

join
就能解决你的问题。输出将是:

PLAY [write out ansible_ens192.ipv4.address value] **************************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Print the IP address in a list] ***************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "IP address is: ['192.168.1.1']"
}

TASK [Print the IP address sanitized] ***************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "IP address is: 192.168.1.1"
}

PLAY RECAP ******************************************************************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
© www.soinside.com 2019 - 2024. All rights reserved.