Using remote_user for a single task in a play

1,297 views
Skip to first unread message

Robert Beaver

unread,
Feb 12, 2018, 12:05:11 PM2/12/18
to Ansible Project
I'm guessing this isn't possible, but I might have been looking at it too long at this point to know if I'm missing something...

I have a playbook with a role in which one task has a remote_user and a delegate_to option set.   That task has to run for each host on the delegate_to server which  has a different ssh_user and password.   Using ssh-keys or the same password as the ansible_ssh_user, it works okay, however, without ssh keys or the same password, it will fail. 

For some people that will be executing this play, they will not be able to use ssh_keys and their passwords are different from the inventory and the delegate_to host.   I could prompt for the remote_user and password on play run, however, I have not figured out how to set the password for the single task.

Here is an example. Is there a way to specify the password for jdoe?    (note syntax and spacing may be off as I just typed this out)

- name: Sample play
  hosts: all

  roles:
    - myRole

  tasks:

  debug:
    msg: "End of play"

In the role/myRole/tasks/main.yml

- include: myfacts.yml

- name: Some task
  command: test 0
  changed_when: false

- name: Problem task
  remote_user: jdoe
  command: "shell command"
  delegate_to: ###.###.###.###

- name: Some other task
  command: "test -f file.txt"
  register: myOutput
  changed_when: false
    

Brian Coca

unread,
Feb 12, 2018, 12:09:35 PM2/12/18
to Ansible Project
remote_user is usable, but you need to know about precedence, it will
only work if you did NOT have an override
(ansible_user/ansible_ssh_user override it).
It is normally used to change the 'default' remote_user or the one set
in config/play/block/role containing the task.

To set a different remote user when using the variable overrides, just
set the variable for the task:


- name: Problem task
command: "shell command"
delegate_to: ###.###.###.###
vars:
ansible_ssh_user: jdoe


The vars override the keywords, but the vars are still vars and normal
variable precedence still applies.


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

Robert Beaver

unread,
Feb 12, 2018, 12:25:02 PM2/12/18
to Ansible Project
Okay, given that, i should be able to set the ansible_ssh_pass there as well.   Thanks, I figured I was overlooking something obvious!   
Reply all
Reply to author
Forward
0 new messages