Could you please advise how 

```
sudo su - userb

```

translates into ansible ?

More specifically, I would like to login to linux server X as `usera` then become `userb` (with the command above), and then execute all the tasks defined in the playbook as `userb`

The following are configuration options in `ansible.cfg`:

```
sudo_user      = root
#ask_sudo_pass = True
#ask_pass      = True
#remote_user = root
#become=True
#become_method=sudo
#become_user=root
#become_ask_pass=False

```

An example task would be:

```
- name: "Create the version directory of app"
        remote_user: userb
        file: path="{{ app_dir }}/{{ app_version }}" state=directory owner=userb group=xxx

```

EDIT: please note that no sudo password is provided in order to become `userb`

```
usera@serverX:~> sudo su - userb
Last login: Fri Nov 17 15:46:48 CET 2017 on pts/1
-bash-4.2$ 

```
['https://Stackoverflow.com/questions/47354108', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4035529/']
```
template<class T>
struct async_queue {
  T pop() {
    auto l = lock();
    ++wait_count;
    cv.wait( l, [&]{ return !data.empty(); } );
    --wait_count;
    auto r = std::move(data.front());
    data.pop_front();
    return r;
  }
  void push(T in) {
    {
      auto l = lock();
      data.push_back( std::move(in) );
    }
    cv.notify_one();
  }
  void push_many(std::initializer_list<T> in) {
    {
      auto l = lock();
      for (auto&& x: in) 
        data.push_back( x );
    }
    cv.notify_all();
  }
  std::size_t readers_waiting() {
    return wait_count;
  }
  std::size_t data_waiting() const {
    auto l = lock();
    return data.size();
  }
private:
  std::queue<T> data;
  std::condition_variable cv;
  mutable std::mutex m;
  std::atomic<std::size_t> wait_count{0};
  auto lock() const { return std::unique_lock<std::mutex>(m); }

};

```

or somesuch.

In the push thread, busy wait on `readers_waiting` until it passes 1.

At which point you have the lock and are within `cv.wait` before the lock is unlocked. Do a `push`.

In theory an infinitely slow reader thread could have gotten into `cv.wait` and still be evaluating the first lambda by the time you call `push`, but an infinitely slow reader thread is no different than a blocked one...

This does, however, deal with slow thread startup and the like.

Using `readers_waiting` and `data_waiting` for anything other than debugging is usually code smell.
You can use a `std::condition_variable` to accomplish this. The help page of cppreference.com actually shows a very nice cosumer-producer example which should be exactly what you are looking for: <http://en.cppreference.com/w/cpp/thread/condition_variable>

EDIT: Actually the german version of cppreference.com has an even better example :-) <http://de.cppreference.com/w/cpp/thread/condition_variable>