Ansible - Alter or Modify Registered Variable

Viewed 34

How can I modify the register variable? For Example, azure_network_interface_info.networkinterfaces[0].ip_configurations has public_ip_allocation_method which I'd like to change it to Static Or Dynamic. Then use the ip_configurations array somewhere else after it's been modified.

      - name: Get facts for one network interface
        azure_rm_networkinterface_info:
          resource_group: "{{ resource_group }}"
          name: "{{ azure_vm_network_interface }}"
        register: azure_network_interface_info  

Output of registered variable:

ip_configurations:
  - application_gateway_backend_address_pools: null
    application_security_groups: null
    load_balancer_backend_address_pools: null
    name: Ubuntu915
    primary: true
    private_ip_address: 10.0.0.5
    private_ip_address_version: IPv4
    private_ip_allocation_method: Dynamic
    public_ip_address: /subscriptions/123456789/resourceGroups/test-resource-group/providers/Microsoft.Network/publicIPAddresses/Ubuntu-915-test
    public_ip_address_name: /subscriptions/123456789/resourceGroups/test-resource-group/providers/Microsoft.Network/publicIPAddresses/Ubuntu-915-test
    public_ip_allocation_method: null
1 Answers

Given the simplified data for testing

  conf:
    - {ip: 10.0.0.5, method: Dynamic}
    - {ip: 10.0.0.6, method: Dynamic}
    - {ip: 10.0.0.7, method: Dynamic}

Q: "Use the conf array somewhere else after it's been modified."

A: Update the first item in the list

  conf0: "{{ conf.0|combine({'method': 'Static'}) }}"

gives

  conf0: {ip: 10.0.0.5, method: Static}

Concatenate the list

  conf_update: "{{ [conf0] + conf[1:] }}"

gives

  conf_update:
    - {ip: 10.0.0.5, method: Static}
    - {ip: 10.0.0.6, method: Dynamic}
    - {ip: 10.0.0.7, method: Dynamic}

Example of a complete playbook

- hosts: localhost
  vars:
    conf:
      - {ip: 10.0.0.5, method: Dynamic}
      - {ip: 10.0.0.6, method: Dynamic}
      - {ip: 10.0.0.7, method: Dynamic}
    conf0: "{{ conf.0|combine({'method': 'Static'}) }}"
    conf_update: "{{ [conf0] + conf[1:] }}"
  tasks:
    - debug:
        var: conf_update
Related