title: Ansible IP address variable - host part
body: <p>I have the following problem:</p>
  
  <p>I'm writing playbook for setting IP address on the command line in Ansible. Lets say 10.10.10.x. I need to get the last part of my public IP lets say x.x.x.15 and assign it to the private: 10.10.10.15. Is there a variable for this? Can i capture some? I've tried to use something like:</p>
  
  <pre><code>shell: "ip addr show | grep inet ...." 
  register: host_ip
  </code></pre>
  
  <p>But it is not what i need. It works, but only for a limited number of servers.</p>
  
  <p>The whole thing should be like that:</p>
  
  <pre><code>"shell: /dir/script --options 10.10.10.{{ var }}"
  </code></pre>
  
  <p>and <code>{{ var }}</code> should be the host part of the public IP.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Thank you! Here's my final solution:</p>
  
  <pre><code>- name: Get the host part of the IP 
    shell: host {{ ansible_fqdn }} | awk '{print $4}' 
    register: host_ip 
  </code></pre>
  
  <p>And</p>
  
  <pre><code>{{ host_ip.stdout.split('.')[3] }}
  </code></pre>
  
  <p>For using it later in the playbook.</p>