# Best method of storing shell output to variable to use on other hosts?

**URL:** <https://forum.ansible.com/t/best-method-of-storing-shell-output-to-variable-to-use-on-other-hosts/22227>\
**Category:** Ansible Project\
**Created:** [August 6, 2016, 3:15pm UTC](https://forum.ansible.com/t/best-method-of-storing-shell-output-to-variable-to-use-on-other-hosts/22227 "2016-08-06T15:15:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tony\_Owens](https://avatars.discourse-cdn.com/v4/letter/t/58956e/32.png) [@Tony\_Owens](https://forum.ansible.com/u/Tony_Owens)\
**Post date:** [August 6, 2016, 3:15pm UTC](https://forum.ansible.com/t/best-method-of-storing-shell-output-to-variable-to-use-on-other-hosts/22227/1 "2016-08-06T15:15:56Z")

</div>

I’m looking for some tips on the best way to store a variable persistently across all hosts or multiple plays.

I am attempting to create a docker swarm manager using something similar to this:

1. 
  - hosts: swarm-init

2. gather\_facts: no
3. tasks:
4. 
  - shell: docker swarm init

5. 
  - shell: docker swarm join-token -q manager

6. register: manager
7. 
  - shell: docker swarm join-token -q worker

8. register: worker

I would run this play once and don’t have that logic built yet but I have not been able to find a method of storing {{ worker }} in a variable so that I could join the swarm from another host in another play. Any advice?

---

<div class="post-metadata">

**Author:** ![viper233](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/viper233/32/2060_2.png) [@viper233](https://forum.ansible.com/u/viper233)\
**Post date:** [August 7, 2016, 4:22am UTC](https://forum.ansible.com/t/best-method-of-storing-shell-output-to-variable-to-use-on-other-hosts/22227/2 "2016-08-07T04:22:35Z")

</div>

[http://docs.ansible.com/ansible/set\_fact\_module.html](http://docs.ansible.com/ansible/set_fact_module.html)

- name: set fact from hosts output  
set\_fact:  
docker\_manager: manager.stdout  
docker\_worker: worker.stdout

# you may want to just pick out a line too, see the debug for what you may want

- debug:  
var:
- worker.stdout\_lines
- manager.stdout\_lines

---

<div class="post-metadata">

**Author:** ![Tony\_Owens](https://avatars.discourse-cdn.com/v4/letter/t/58956e/32.png) [@Tony\_Owens](https://forum.ansible.com/u/Tony_Owens)\
**Post date:** [August 7, 2016, 8:31pm UTC](https://forum.ansible.com/t/best-method-of-storing-shell-output-to-variable-to-use-on-other-hosts/22227/3 "2016-08-07T20:31:01Z")

</div>

Thanks for the tip. I am much closer but I’m getting some undesirable characters added to the var named “key” I’m setting.

---

<div class="post-metadata">

**Author:** ![Tony\_Owens](https://avatars.discourse-cdn.com/v4/letter/t/58956e/32.png) [@Tony\_Owens](https://forum.ansible.com/u/Tony_Owens)\
**Post date:** [August 8, 2016, 12:51am UTC](https://forum.ansible.com/t/best-method-of-storing-shell-output-to-variable-to-use-on-other-hosts/22227/4 "2016-08-08T00:51:35Z")

</div>

I actually got this to work by change key variable to:

1. key: “{{ hostvars[groups[‘swarm-init’][0]][‘worker’] | replace(‘[u’, ‘’) | replace(‘]’, ‘’)}}”

What a pia! Surely i am doing something early in the play that is causing me to have to replace those characters.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 8, 2016, 5:41pm UTC](https://forum.ansible.com/t/best-method-of-storing-shell-output-to-variable-to-use-on-other-hosts/22227/5 "2016-08-08T17:41:25Z")

</div>

Your worker is a list so maybe something like this  
hostvars[groups['swarm-init'][0]]['worker'][0]

---

<div class="post-metadata">

**Author:** ![Tony\_Owens](https://avatars.discourse-cdn.com/v4/letter/t/58956e/32.png) [@Tony\_Owens](https://forum.ansible.com/u/Tony_Owens)\
**Post date:** [August 12, 2016, 7:07pm UTC](https://forum.ansible.com/t/best-method-of-storing-shell-output-to-variable-to-use-on-other-hosts/22227/6 "2016-08-12T19:07:53Z")

</div>

I am facing an issue where I need to specify hosts like this

1. 
  - hosts: “swarm-init-{{ env }}”

but trying to use that group value in hostvars stanza yields the below error. any suggestions?

1. mtoken: “{{ hostvars[groups[‘swarm-init-{{ env }}’][0]][‘manager’] | replace(‘[u’, ‘’) | replace(‘]’, ‘’)}}”

{“failed”: true, “msg”: “‘dict object’ has no attribute ‘swarm-init-{{ env }}’”}

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [August 12, 2016, 7:58pm UTC](https://forum.ansible.com/t/best-method-of-storing-shell-output-to-variable-to-use-on-other-hosts/22227/7 "2016-08-12T19:58:50Z")

</div>

hosts: only has access to extra-vars, you cannot get group vars when they depend on the hosts you select in hosts: …

---

<div class="post-metadata">

**Author:** ![Tony\_Owens](https://avatars.discourse-cdn.com/v4/letter/t/58956e/32.png) [@Tony\_Owens](https://forum.ansible.com/u/Tony_Owens)\
**Post date:** [August 12, 2016, 8:15pm UTC](https://forum.ansible.com/t/best-method-of-storing-shell-output-to-variable-to-use-on-other-hosts/22227/8 "2016-08-12T20:15:42Z")

</div>

{{ env }} is an extra-vars in this instance

---

<div class="post-metadata">

**Author:** ![Tony\_Owens](https://avatars.discourse-cdn.com/v4/letter/t/58956e/32.png) [@Tony\_Owens](https://forum.ansible.com/u/Tony_Owens)\
**Post date:** [August 12, 2016, 8:49pm UTC](https://forum.ansible.com/t/best-method-of-storing-shell-output-to-variable-to-use-on-other-hosts/22227/9 "2016-08-12T20:49:51Z")

</div>

This works just fine when I set\_fact for “manager” on the desired host.  
1.

1. mtoken: “{{ hostvars[‘[myhost.net](http://myhost.net)’][‘manager’] | replace(‘[u’, ‘’) | replace(‘]’, ‘’)}}”  
but I need to be able to get the name of the single host that is in the group passed with something like this. Perhaps i need to step back and regroup and go about this in a different way. Any tips would be appreciated.

2. 
  - hosts: “swarm-init-{{ env }}”

---

<div class="post-metadata">

**Author:** ![Tony\_Owens](https://avatars.discourse-cdn.com/v4/letter/t/58956e/32.png) [@Tony\_Owens](https://forum.ansible.com/u/Tony_Owens)\
**Post date:** [August 13, 2016, 4:53pm UTC](https://forum.ansible.com/t/best-method-of-storing-shell-output-to-variable-to-use-on-other-hosts/22227/10 "2016-08-13T16:53:10Z")

</div>

I concede…there is no way to do this without passing an extra var at run time.
