Extract data from task in a variable

Hi,
This is a role i wrote which will get the IP addresses of the Hosts and then copy a file from the ansible server to the host server.

  • name: Seeking the host IP address
    shell: |
    ifconfig eth0 | grep “inet addr” | cut -d ‘:’ -f 2 | cut -d ’ ’ -f 1
    register: ipv4_address

  • debug: var=ipv4_address.stdout_lines

  • name: Make the copy to CTM agents
    delegate_to: 127.0.0.1
    shell: sshpass -p ‘xyz@123456’ scp /home/admin/schoolproject/deploy/Agent_{{ipv4_address.stdout_lines}}.zip admin@{{ipv4_address.stdout_lines}}:/var/tmp/

After running it, I am getting the error below.

fatal: [10.200.2.6 → 127.0.0.1]: FAILED! => {“changed”: true, “cmd”: “sshpass -p ‘xyz@123456’ scp /home/admin/schoolproject/deploy/Agent_[u’10.200.2.6’].zip admin@[u’10.200.2.6’]:/var/tmp/”, “delta”: “0:00:00.925109”, “end”: “2018-05-04 18:16:52.188862”, “msg”: “non-zero return code”, “rc”: 1, “start”: “2018-05-04 18:16:51.263753”, “stderr”: “ssh: Could not resolve hostname u10.200.2.6: Name or service not known\r\nlost connection”, “stderr_lines”: [“ssh: Could not resolve hostname u10.200.2.6: Name or service not known”, “lost connection”], “stdout”: “”, “stdout_lines”: }

How do I remove the quotes and square bracket [u’ ] from the variable ?

You need to use ipv4_address.stdout not ipv4_address.stdout_lines.

By the way, if you gather facts, which is the default, you already have the ip in the variable {{ ansible_default_ipv4.address }}