Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set remote_user for set of tasks in Ansible playbook without repeating it per task

I am creating a playbook which first creates a new username. I then want to run "moretasks.yml" as that new user that I just created. Currently, I'm setting remote_user for every task. Is there a way I can set it for the entire set of tasks once? I couldn't seem to find examples of this, nor did any of my attempts to move remote_user around help.

Below is main.yml:

---
- name: Configure Instance(s)
  hosts: all
  remote_user: root
  gather_facts: true

  tags:
    - config
    - configure

  tasks:
    - include: createuser.yml new_user=username
    - include: moretasks.yml new_user=username
    - include: roottasks.yml #some tasks unrelated to username.

moretasks.yml:

---
  - name: Task1
    copy: 
      src: /vagrant/FILE
      dest: ~/FILE
    remote_user: "{{newuser}}"

  - name: Task2
    copy: 
      src: /vagrant/FILE
      dest: ~/FILE
    remote_user: "{{newuser}}"
like image 404
Shark Avatar asked Dec 05 '14 02:12

Shark


People also ask

How do you run or skip only certain tasks in playbook?

The easiest way to run only one task in Ansible Playbook is using the tags statement parameter of the “ansible-playbook” command. The default behavior is to execute all the tags in your Playbook with --tags all .

Can we have multiple tasks in a Ansible playbook?

A Playbook can have multiple Plays and a Play can have one or multiple Tasks. In a Task a Module is called, like the Modules in the previous chapter. The goal of a Play is to map a group of hosts to a list of Tasks. The goal of a Task is to implement Modules against those hosts.

How do I make an Ansible task run only once?

For such requirements where we need one tasks to run only once on a batch of hosts and we will be running that from Ansible controller node, we have feature parameter named run_once. When we have this parameter mentioned in a task, that task will run only once on first host it finds despite the host batch.

Does Ansible run sequentially?

By default, Ansible runs each task on all hosts affected by a play before starting the next task on any host, using 5 forks. If you want to change this default behavior, you can use a different strategy plugin, change the number of forks, or apply one of several keywords like serial .


2 Answers

First of all you surely want to use sudo_user (remote user is the one that logs in, sudo_user is the one who executes the task).

In your case you want to execute the task as another user (the one previously created) just set:

- include: moretasks.yml
  sudo: yes
  sudo_user: "{{ newuser }}"

and those tasks will be executed as {{ newuser }} (Don't forget the quotes)

Remark: In most cases you should consider remote_user as a host parameter. It is the user that is allowed to login on the machine and that has sufficient rights to do things. For operational stuff you should use sudo / sudo_user

like image 145
ProfHase85 Avatar answered Sep 20 '22 01:09

ProfHase85


You could split this up into to separate plays? (playbooks can contain multiple plays)

---
- name: PLAY 1
  hosts: all
  remote_user: root
  gather_facts: true

  tasks:
    - include: createuser.yml new_user=username
    - include: roottasks.yml #some tasks unrelated to username.

- name: PLAY 2
  hosts: all
  remote_user: username
  gather_facts: false

  tasks:
    - include: moretasks.yml new_user=username

There is a gotcha using separate plays: you can't use variables set with register: or set_fact: in the first play to do things in the second play (this statement is not entirely true, the variables are available in hostvars, but I recommend not using variables between roles). Defined variables like in group_vars and host_vars work just fine.

Another tip I'd like to give is to look into using roles http://docs.ansible.com/playbooks_roles.html. While it might seem more complicated at first, it's much easier to re-use them (as you seem to be doing with the "createuser.yml"). Looking at the type of things you are trying to achieve, the 'include all the things' path won't last much longer.

like image 32
Ramon de la Fuente Avatar answered Sep 19 '22 01:09

Ramon de la Fuente