title: Ansible - append a string var itself in a loop
body: <p>I have a problem with append a string var itself in a loop.</p>
  <p>like</p>
  <pre><code>var: 
    a: &quot;&quot;
  
  var:
    package:
       - {{ iterations: 0 , newvar : b }}
       - {{ iterations: 1 , newvar : c }}
       - {{ iterations: 2 , newvar : d }}
       - {{ iterations: 3 , newvar : e }}
  - set_fact:
      a: a + {{newvar}} 
    loop: {{ package }}
  </code></pre>
  <p>And I expected the answer should be a = bcde</p>
  <p>But I also have the code if-else inside the set_fact. ( it make the code more complicated )
  The code as below:</p>
  <pre><code>
   var:
     portq: &quot;&quot;
  
   var:
     package: 
       - {{ iterations: 0 }}
       - {{ iterations: 1 }}
       - {{ iterations: 2 }}
       - {{ iterations: 3 }}
  
  
  - set_fact:
  
        portq: &quot;{% if q_port_result.results[item.iterations].cde != 'unknown' %}{{ portq + q_port_result.results[item.iterations].cde}}{% else %}{% endif %}&quot;
  
        loop: &quot;{{ package }}&quot;
  
  </code></pre>
  <p>My problem is how to add append the &quot;portq&quot; with the new variable.</p>
  <p>It always prompts the error &quot;The error was: 'portq' is undefined\n\n&quot;</p>
  <p>Many thanks!</p>