title: How do I set register a variable to persist between plays in ansible?
body: <p>I have an ansible playbook, where I'd like a variable I register on one machine to be available on another.</p>
  <p>In my case, I'd like to run a command on <code>localhost</code>, in this case  <code>git rev-parse --abbrev-ref HEAD</code>, so I can make a note of the current git branch, and sha1, and register this output, so I can refer to it later when  working any machine in the <code>main</code> group, in the second play.</p>
  <p>However, it's not clear to me how I register a variable on localhost, so I can access it from main. When I try to access the variable in the second play I get this message:</p>
  <pre class="lang-yaml prettyprint-override"><code>    TASK: [debug msg={{ app_git_sha1.stdout }}] ***********************************
      fatal: [main] =&gt; One or more undefined variables: 'app_git_sha1' is undefined
  </code></pre>
  <p>Here's the play I'm using. Is there anything obvious I should be doing?</p>
  <pre><code>    ---
      - hosts: localhost
        connection: local
        gather_facts: no
        tasks:
          - name: register current branch
            command: git rev-parse --abbrev-ref HEAD
            register: git_branch
            sudo: no
            when: vagrant
            tags:
              - debugsha
  
          - debug: msg={{ git_branch.stdout }}
            tags:
              - debugsha
  
          - name: register the SHA1 of the branch being deployed
            command: git rev-parse origin/{{ git_branch.stdout }}
            register: app_git_sha1
            sudo: no
            tags:
              - slack
              - debugsha
  
          - debug: msg={{ app_git_sha1.stdout }}
            tags:
              - debugsha
  
  
  
      - hosts: main
        sudo: yes
        roles:
          - role: productscience.deploy_user
          # TODO reprovision using these roles, for consistency
          # - role: app.essentials
          # - role: zenoamaro.postgresql
          - role: productscience.papertrailapp
          - role: jdauphant.nginx
        tasks:
          - include: setup.yml
          # - include: db.yml
  
          - name: checkout source control when deploying to remote servers
            include: source.yml
            when: not vagrant
            tags:
                - deploy
  
          - include: django.yml
            tags:
                - deploy
  
  
          - name: include vagrant specific dependencies for local development
            include: vagrant.yml
            when: vagrant
  
        handlers:
          - name: restart postgres
            sudo: yes
            service: name=postgresql state=restarted
          - name: start restart uwsgi
            sudo: yes
            service: name={{ app }} state=restarted
  
      - hosts: localhost
        connection: local
        gather_facts: no
        tasks:
          - name: register the SHA1 of the branch being deployed
            when: not vagrant
            command: git rev-parse origin/{{ git_branch }}
            register: git_sha
            tags:
              - slack
  
          - name: Send notification message via Slack all options
            when: not vagrant
            tags:
              - slack
            local_action:
              module: slack
              token: &quot;{{ wof_slack_token }}&quot;
              msg: &quot;Deployment of `{{ git_branch }}` to {{ app_url }} completed with sha `{{ git_sha.stdout }}`&quot;
              channel: &quot;#wof&quot;
              username: &quot;Ansible deploy-o-tron&quot;
  </code></pre>