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

117 views
Skip to first unread message

Tony Owens

unread,
Aug 6, 2016, 11:15:56 AM8/6/16
to Ansible Project
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?

Stephen Granger

unread,
Aug 7, 2016, 12:23:17 AM8/7/16
to ansible...@googlegroups.com

    - 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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/43c19f78-9572-4af2-8e52-784878805bcc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Steve

Tony Owens

unread,
Aug 7, 2016, 4:31:01 PM8/7/16
to Ansible Project
Thanks for the tip.  I am much closer but I'm getting some undesirable characters added to the var named "key" I'm setting.

  1. - hosts: swarm-init
  1. tasks:
  2. - shell: docker swarm init
  3. - shell: docker swarm join-token -q manager
  4. register: manager
  1. - set_fact:
  2. manager: "{{ manager.stdout_lines }}"
  1. - shell: docker swarm join-token -q worker
  2. register: worker
  1. - set_fact:
  2. worker: "{{ worker.stdout_lines }}"
  3. - hosts: swarm-worker
  4. vars:
  5. key: "{{ hostvars[groups['swarm-init'][0]]['worker'] }}"
  6. tasks:
  7. - shell: "docker swarm join --token {{ key }} {{ swarmmanager }}:2377"

Inventory
  1. [swarm-init]
  2. myhost1
  3.  
  4. [swarm-manager]
  5. myhost1
  6.  
  7. [swarm-worker]
  8. myhost2
  9.  
  10. [swarm-manager:vars]
  11. swarmmanager=myhost1
  12.  
  13. [swarm-worker:vars]
  14. swarmmanager=myhost1

The output:

PLAY ***************************************************************************

TASK [setup] *******************************************************************
ok: [myhost1]

TASK [command] *****************************************************************
changed: [myhost1]

TASK [command] *****************************************************************
changed: [myhost1]

TASK [set_fact] ****************************************************************
ok: [myhost1]

TASK [command] *****************************************************************
changed: [myhost1]

TASK [set_fact] ****************************************************************
ok: [myhost1]

PLAY ***************************************************************************

TASK [setup] *******************************************************************
ok: [myhost2]

TASK [command] *****************************************************************
fatal: [myhost2]: FAILED! => {"changed": true, "cmd": "docker swarm join --token [u'SWMTKN-1-0lanrq32u7f1v47noxftd1ekpzgipo3noufg7ppox3aooqhmqh-3peerw7aalzg8ko13zrqh6hee'] myhost1:2377", "delta": "0:00:00.036979", "end": "2016-08-07 20:20:16.500147", "failed": true, "rc": 1, "start": "2016-08-07 20:20:16.463168", "stderr": "Error response from daemon: invalid join token", "stdout": "", "stdout_lines": [], "warnings": []}


PLAY RECAP *********************************************************************
myhost1   : ok=6    changed=3    unreachable=0    failed=0   
myhost2   : ok=1    changed=0    unreachable=0    failed=1   

FATAL: command execution failed
hudson.AbortException: Ansible playbook execution failed
	at org.jenkinsci.plugins.ansible.AnsiblePlaybookBuilder.perform(AnsiblePlaybookBuilder.java:218)
	at org.jenkinsci.plugins.ansible.AnsiblePlaybookBuilder.perform(AnsiblePlaybookBuilder.java:192)
	at hudson.tasks.BuildStepCompatibilityLayer.perform(BuildStepCompatibilityLayer.java:78)
	at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
	at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:779)
	at hudson.model.Build$BuildExecution.build(Build.java:205)
	at hudson.model.Build$BuildExecution.doRun(Build.java:162)
	at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:534)
	at hudson.model.Run.execute(Run.java:1741)
	at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
	at hudson.model.ResourceController.execute(ResourceController.java:98)
	at hudson.model.Executor.run(Executor.java:410)
ERROR: Ansible playbook execution failed
Notifying upstream projects of job completion
Finished: FAILURE
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.



--
Steve

Tony Owens

unread,
Aug 7, 2016, 8:51:35 PM8/7/16
to Ansible Project
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.

Kai Stian Olstad

unread,
Aug 8, 2016, 1:41:36 PM8/8/16
to ansible...@googlegroups.com
On 08. aug. 2016 02:51, Tony Owens wrote:
> 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.

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

--
Kai Stian Olstad

Tony Owens

unread,
Aug 12, 2016, 3:07:53 PM8/12/16
to Ansible Project
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 }}'"}

Brian Coca

unread,
Aug 12, 2016, 3:59:11 PM8/12/16
to ansible...@googlegroups.com
hosts: only has access to extra-vars, you cannot get group vars when they depend on the hosts you select in hosts: ....


----------
Brian Coca

Tony Owens

unread,
Aug 12, 2016, 4:15:42 PM8/12/16
to Ansible Project
{{ env }} is an extra-vars in this instance

Tony Owens

unread,
Aug 12, 2016, 4:49:51 PM8/12/16
to Ansible Project
This works just fine when I set_fact for "manager" on the desired host.

  1. mtoken: "{{ hostvars['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.
  1. - hosts: "swarm-init-{{ env }}"

Tony Owens

unread,
Aug 13, 2016, 12:53:10 PM8/13/16
to Ansible Project
I concede..there is no way to do this without passing an extra var at run time.
Reply all
Reply to author
Forward
0 new messages