title: Ansible: Store command's stdout in new variable?
body: <p>Inside my playbook I'd like to create a variable holding the output of an external command. Afterwards I want to make use of that variable in a couple of templates.  </p>
  
  <p>Here are the relevant parts of the playbook:</p>
  
  <pre><code>  tasks:
      - name: Create variable from command
        command: "echo Hello"
        register: command_output
      - debug: msg="{{command_output.stdout}}"
  
      - name: Copy test service
        template: src=../templates/test.service.j2 dest=/tmp/test.service
      - name: Enable test service
        shell: systemctl enable /tmp/test.service
      - name: Start test service
        shell: systemctl start test.service
  </code></pre>
  
  <p>and let's say this is my template:</p>
  
  <pre><code>[Unit]
  Description=MyApp
  After=docker.service
  Requires=docker.service
  
  [Service]
  TimeoutStartSec=0
  ExecStartPre=-/usr/bin/docker kill busybox1
  ExecStartPre=-/usr/bin/docker rm busybox1
  ExecStartPre=/usr/bin/docker pull busybox
  ExecStart=/usr/bin/docker run --name busybox1 busybox /bin/sh -c "while true; do echo {{ string_to_echo }}; sleep 1; done"
  
  [Install]
  WantedBy=multi-user.target
  </code></pre>
  
  <p>(Notice the <code>{{ string_to_echo }}</code>)</p>
  
  <p>So what I'm basically looking for is a way to store the contents of <code>command_output.stdout</code> (which is generated/retrieved during the first task) in a new variable <code>string_to_echo</code>.<br>
  That variable I'd like to use in multiple templates afterwards.  </p>
  
  <p>I guess I could just use <code>{{command_output.stdout}}</code> in my templates, but I want to get rid of that <code>.stdout</code> for readability.</p>