title: proper way to declare variable in ansible playbook
body: <p>I have the following playbook: </p>
  
  <pre><code>  1 ---
    2 - hosts: lxc_hosts
    3   name:  install software on lxc container
    4   tasks:
    5   - name: get list of containers on lxc host {{inventory_hostname}}
    6     shell: &gt;
    7       lxc-ls | more | tr '\n' ',' | sed 's/,$//'
    8     register: containers
    9   - set_fact:
   10       container_list: "{{ containers.stdout.split(',')|select('match', 'server*')|list }}"
   11   - debug: msg="{{item}}"
   12     with_items:
   13       - "{{container_list}}"
   14   - name: Run memory command within "running" container
   15     lxc_container:
   16       name: "{{item}}"
   17       with_items: 
   18         - "{{container_list}}"
   19       container_command: |
   20         df -h
   21       register: memory_check
   22   - debug: msg="{{memory_check.stdout}}"
  </code></pre>
  
  <p>This returns the following results: </p>
  
  <pre><code>PLAY [install software on lxc container] 
  ****************************************
  
  TASK [setup]
  ******************************************************************* 
  ok: [10.1.1.1]
  
  TASK [get list of containers on lxc host 10.1.1.1]
  ************************* 
  changed: [10.1.1.1]
  
  TASK [set_fact]
  **************************************************************** 
  ok: [10.1.1.1]
  
  TASK [debug]
  *******************************************************************
  ok: [10.1.1.1] =&gt; (item=server1-container) =&gt; {
      "item": "server1-container", 
      "msg": "server1-container" } 
  ok: [10.1.1.1] =&gt; (item=server2-container) =&gt; {
      "item": "server2-container", 
      "msg": "server2-container" } 
  ok: [10.1.1.1] =&gt; (item=server3-container) =&gt; {
       "item": "server3-container", 
      "msg": "server3-container" }
  
  TASK [Run memory command within "running" container]
  *************************** 
  fatal: [10.1.1.1]: FAILED! =&gt; {"failed": true, "msg": "'item' is undefined"}
  
  NO MORE HOSTS LEFT
  *************************************************************   
       to retry, use: --limit @playbooks/inventory_get_containers_on_lxc.retry
  
  PLAY RECAP
  *********************************************************************
  10.1.1.1               : ok=4    changed=1    unreachable=0    failed=1   
  
  mymachine:/etc/ansible#
  </code></pre>
  
  <p>I've been playing around between set_fact and "vars" but I can't seem to get this going. As you can see, the debug statement on line 11 results in the list you see below... which seems to be work... and which seems to prove that I set the variable correctly.
  I'm not sure what else to try.</p>
  
  <p>Thanks. </p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>This is what my code looks like for that specific section: </p>
  
  <pre><code> 14   - name: Run memory command within "running" container
   15     lxc_container:
   16       name: "{{item}}"
   17     with_items:
   18 #         - "{{ containers.stdout.split(',')|select('match', 'server*')|list }}"
   19         - "{{container_list}}"
   20       container_command: |
   21         df -h
   22     register: memory_check
   23   - debug: msg="{{memory_check.stdout}}"
  </code></pre>
  
  <p>When I run it, i get the following error message: </p>
  
  <p>ERROR! Syntax Error while loading YAML.</p>
  
  <p>The error appears to have been in '/etc/ansible/playbooks/lxc_container_test.yml': line 20, column 7, but may
  be elsewhere in the file depending on the exact syntax problem.</p>
  
  <p>The offending line appears to be:</p>
  
  <pre><code>    - "{{container_list}}"
    container_command: |
    ^ here
  </code></pre>
  
  <p>Line 20 is indented 2 spaces compared to the "with_items" line on line 17</p>