title: Variabalizing a file line with Ansible?
body: <p>I'm looking to put an ip in variables to pass into a template, unfortunately something somewhere is messing up with registering the variable?</p>
  
  <pre><code>- name: Get Controller[0] ip
    command: "cat {{ controller0 }} |grep -A1 controller0 |tail -1 |awk '{print$2}'"
    register: controller0
    with_file:
      - "{{ playbook_dir }}/../ssh.cfg"
  
  - debug: var=controller0
  </code></pre>
  
  <p>Error</p>
  
  <pre><code>fatal: [100.24.12.41]: FAILED! =&gt; {"msg": "The task includes an option with an undefined variable. The error was: 'controller0' is undefined
  
      The error appears to have been in '~/Terraform/terraform-kubernetes/ansible/roles/worker/tasks/main.yml': line 110, column 3, but may
      be elsewhere in the file depending on the exact syntax problem.
  
      The offending line appears to be:
  
  
      - name: Get the file contents
        ^ here
      "}
  </code></pre>
  
  <p>Local grep outputs</p>
  
  <pre><code>$ cat ssh.cfg |grep -A1  controller0 |tail -1 |awk '{print$2}'
  35.171.150.231 
  </code></pre>
  
  <p>Template is</p>
  
  <pre><code>[Unit]
  Description=Kubernetes Kube Proxy
  Documentation=https://github.com/GoogleCloudPlatform/kubernetes
  
  [Service]
  ExecStart=/usr/bin/kube-proxy \
    --master=https://{{ controller0 }}:6443 \
    --kubeconfig=/var/lib/kubelet/kubeconfig \
    --proxy-mode=iptables \
    --v=2
  
  Restart=on-failure
  RestartSec=5
  
  [Install]
  WantedBy=multi-user.target
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <pre><code>- name: Get the file contents
    command: "echo -ne '{{ item }}' |grep -A1 controller0 |tail -1 |awk '{print$2}'"
    with_file:
      - "{{playbook_dir}}/../ssh.cfg"
    register: controller
  
  - debug: var=controller
  </code></pre>