remote_user var persists between plays?

54 views
Skip to first unread message

Slim Slam

unread,
Nov 20, 2015, 12:25:55 PM11/20/15
to Ansible Project
Ansible v1.9.4

I have a playbook that I launch with:

$ ansible-playbook build.yml

build.yml includes set_os.yml (just a set of tasks) that launches some ec2 instances. build.yml looks like this.

- name: Build stuff
  hosts
: localhost
  sudo
: False
  gather_facts
: False
  tasks
:
    
- include: set_os.yml
- include: build_soft.yml

In the file set_os.yml, I set the remote user like this:

- set_fact:
      ruser
: ubuntu

When I  include build_soft.yml above (a new play), I want to use that remote user to login and configure the new EC2 instance like so:

---
- name: Configure the EC2 instance
  hosts
: ec2hosts
  gather_facts
: true
  remote_user
: "{{ ruser }}"
  tasks
:
   
- do something......

Since "set_fact" definitions are supposed to work between plays (?), I assume it will still be defined, but I always get this error when build_soft.yml executes:

GATHERING FACTS ***************************************************************
<33.34.115.33> ESTABLISH CONNECTION FOR USER: {{ ruser }}
<33.34.115.33> REMOTE_MODULE setup
<33.34.115.33> EXEC ssh -C -tt -vvv -F ../config/sshconfig -o ControlPersist=15m -o ControlPath="/someuser/.ansible/cp/ansible-ssh-%h-%p-%r" -o StrictHostKeyChecking=no -o IdentityFile="mykey.pem" -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User={{ ruser }} -o ConnectTimeout=10 33.34.115.33 /bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1448039631.2-14623163715246 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1448039631.2-14623163715246 && echo $HOME/.ansible/tmp/ansible-tmp-1448039631.2-14623163715246'
fatal
: [33.34.115.33] => SSH Error: command-line line 0: garbage at end of line; "ruser".

Any ideas?

-J

Slim Slam

unread,
Nov 20, 2015, 4:13:28 PM11/20/15
to Ansible Project
This:   http://stackoverflow.com/questions/31708736/passing-variables-between-nested-playbooks
suggests that if I change the "hosts" (which I do), then the set_fact vars are NOT persisted between plays. Correct?

J

Matt Martz

unread,
Nov 20, 2015, 4:37:21 PM11/20/15
to ansible...@googlegroups.com
set_fact sets a variable for the hosts that are in the play.  It does not set global variables.

As such, if you target different hosts between the different plays, then you do not have direct access to that fact, as it was set on another host.
--
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-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/baf0c1b0-b5e9-4965-bd58-6a56e6868962%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Brian Coca

unread,
Nov 20, 2015, 4:40:35 PM11/20/15
to Ansible Project
vars should still be there though,
hostvars['original_host']['varname'] should allow you to see the var
from another host.

--
Brian Coca

Slim Slam

unread,
Nov 20, 2015, 5:33:26 PM11/20/15
to Ansible Project
Ok. That works:

---
- name: Configure the EC2 instance
  hosts
: ec2hosts
  gather_facts
: true

  remote_user
: "{{ hostvars['localhost']['ruser'] }}"
  tasks
:
   
- do something......

That seems somewhat sub-optimal but since I'm generating all the vars dynamically (via set_fact or
include_vars based on parameterized pathnames ) in previous steps of the same playbook, I don't see any other
way to handle this if I want things to execute in one invocation of a playbook. 

Reply all
Reply to author
Forward
0 new messages