0
votes

Using Ansible here, I'm gathering facts about a container:

   - name: start my container 
      lxd_container:
        name: vm_srv1
        state: started
      register: st
    - debug: msg="{{ st.addresses }}"

Running the playbook produces the following:

TASK [manager : debug] *********************************************************
ok: [lxc.myvmhost ] => {
    "msg": {
        "eth0": [
            "10.0.3.103"
        ]
    }
}

I would like to store eth0 value into a file

I have added

- debug: msg="{{ st.addresses['eth0'] }}"

output:

TASK [manager : debug] *********************************************************
ok: [lxc.myvmhost ] => {
    "msg": [
        "10.0.3.103"
    ]
}

When storing the output to a file

- lineinfile: dest=/tmp/file line="{{ st.addresses.eth0 }}"

I get the following:

Hello world
['10.0.3.103']

How can I store the IP address without any funny bagged that Ansible adds?

1

1 Answers

1
votes

In your example eth0 is a list of ip addresses, so to fetch the first element of the given list, use:

st.addresses.eth0[0]