Ansible - 替换ipv4地址的主机部分

时间:2014-08-24 10:25:58

标签: ansible ipv4 ansible-playbook

- hosts: localhost
  gather_facts: False

  vars_prompt:
    - name: ServerIP
      prompt: Enter the ServerIP to replace
      private: False
      default: "11.11.4.10"

  vars:
    Y: '126'

  pre_tasks:
    - name: Set some facts
      set_fact:
        ServerIP1: "{{ServerIP}}"

    - name: Save ServerIP in a register
      shell: echo {{ServerIP1}}
      register: resultip

注意:我知道我可以使用以下代码拆分Ip的主机部分:

- name: show the last octet of the ServerIP
  debug: msg={{resultip.stdout.split('.')[3]}}

我想要的是什么:

- name: Replace the last octet of the ServerIP with a variable var
  # How can I do this ?

我想存储/显示:

x.x.x。{{Y}}

1 个答案:

答案 0 :(得分:3)

如果要将ServerIP的最后一个八位字节替换为值 Y set_fact: NewIP: "{{ ServerIP.split('.')[0] }}.{{ ServerIP.split('.')[1] }}.{{ ServerIP.split('.')[2] }}.{{ Y }}" 以下内容适用于您:

{{1}}