# package TASK ... 'should be run under the root user'

**URL:** <https://forum.ansible.com/t/package-task-should-be-run-under-the-root-user/35904>\
**Category:** Ansible Project\
**Created:** [October 20, 2021, 1:26pm UTC](https://forum.ansible.com/t/package-task-should-be-run-under-the-root-user/35904 "2021-10-20T13:26:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [October 20, 2021, 1:26pm UTC](https://forum.ansible.com/t/package-task-should-be-run-under-the-root-user/35904/1 "2021-10-20T13:26:37Z")

</div>

- I have [this TASK in a playbook](https://pastebin.com/h83HqMn0) which should install a bunch of packages on my (rhel8) target rrequired for postgresql

- I am shooting my playbok as a user with root priviledges via sudo

- In earlier TASKS of the playbook I become a ‘postgres’ user, but would expect this to be ephemeral (so I become the intital user as soon a TASK is finished)

still I am [getting an (inexplicable to me) error](https://pastebin.com/6fQ8xNuK) upon running this playbook, which seems to indicate I am not a root-capable user when trying to install those packages.

can anybody help me out with this?

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [October 20, 2021, 1:32pm UTC](https://forum.ansible.com/t/package-task-should-be-run-under-the-root-user/35904/2 "2021-10-20T13:32:21Z")

</div>

> &nbsp;&nbsp;\* I have this TASK in a playbook \<[https://pastebin.com/h83HqMn0&gt](https://pastebin.com/h83HqMn0&gt); which should install a bunch of packages on my (rhel8) target rrequired for postgresql  
> \*  
> &nbsp;&nbsp;&nbsp;&nbsp;I am shooting my playbok as a user with root priviledges via sudo

Please show the code that you are using for that user in your playbook.

> \*  
> &nbsp;&nbsp;&nbsp;&nbsp;In earlier TASKS of the playbook I become a 'postgres' user, but would expect this to be ephemeral (so I become the intital user as soon a TASK is finished)

If that is part of the task, it should be fine.

Regards

```
         Racke

```

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [October 20, 2021, 1:49pm UTC](https://forum.ansible.com/t/package-task-should-be-run-under-the-root-user/35904/3 "2021-10-20T13:49:03Z")

</div>

it is the paste [https://pastebin.com/h83HqMn0](https://pastebin.com/h83HqMn0). However … here you go

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [October 20, 2021, 2:07pm UTC](https://forum.ansible.com/t/package-task-should-be-run-under-the-root-user/35904/4 "2021-10-20T14:07:19Z")

</div>

> > \>
> > 
> > > \* I have this TASK in a playbook \<[https://pastebin.com/h83HqMn0&gt](https://pastebin.com/h83HqMn0&gt); which should install a bunch of packages on my (rhel8) target rrequired for postgresql  
> > > \*  
> > > I am shooting my playbok as a user with root priviledges via sudo
> > 
> > Please show the code that you are using for that user in your playbook.
> 
> it is the paste [https://pastebin.com/h83HqMn0](https://pastebin.com/h83HqMn0). However ... here you go

Sorry, I wanted to see the head of the playbook respective the place in inventory where you set up the  
become: variables.

> ##############################################################  
> - name: check installed system packages for postgres  
> package:  
> name:  
> - psmisc  
> - strace  
> - gdb  
> - nagios-plugins-mysql  
> - nagios-plugins-pgsql  
> - nagios-plugins-http  
> - openssl  
> - pam  
> - readline  
> - libselinux  
> - systemd-libs  
> - zlib  
> state: present  
> ignore\_errors: yes  
> become: yes  
> # become\_user: [root\_user]  
> ##############################################################

(zealous\_mode: yes)

Why do you use ignore\_errors: yes here?

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [October 20, 2021, 2:45pm UTC](https://forum.ansible.com/t/package-task-should-be-run-under-the-root-user/35904/5 "2021-10-20T14:45:16Z")

</div>

actually I figured out in the meantime that aparently my trouble lies exactly there:  
I have a defaults.yaml that injects a bunch of variables into the playbook.  
In that defaults.yml there is a variable referencing to user ‘postgres’ as ansible\_become\_user

ansible\_become\_user: postgres

So I assume I’ll have to figure out how to utilize different become users in a single playbook. I tried or overrule the defaults entry temporarily with …

become: yes  
become\_user: [root\_user]

… inside the TASK, but that apparently was not interpreted in the way I wanted

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [October 20, 2021, 3:01pm UTC](https://forum.ansible.com/t/package-task-should-be-run-under-the-root-user/35904/6 "2021-10-20T15:01:56Z")

</div>

> > > > \>
> > > > 
> > > > > \* I have this TASK in a playbook \<[https://pastebin.com/h83HqMn0](https://pastebin.com/h83HqMn0) \<[https://pastebin.com/h83HqMn0&gt;&gt](https://pastebin.com/h83HqMn0&gt;&gt); which should install a bunch of packages on my (rhel8) target rrequired for postgresql  
> > > > > \*  
> > > > > I am shooting my playbok as a user with root priviledges via sudo
> > > > 
> > > > Please show the code that you are using for that user in your playbook.
> > > 
> > > it is the paste [https://pastebin.com/h83HqMn0](https://pastebin.com/h83HqMn0) \<[https://pastebin.com/h83HqMn0&gt;\](https://pastebin.com/h83HqMn0&gt;%5C). However ... here you go
> > 
> > Sorry, I wanted to see the head of the playbook respective the place in inventory where you set up the  
> > become: variables.
> 
> actually I figured out in the meantime that aparently my trouble lies exactly there:  
> I have a defaults.yaml that injects a bunch of variables into the playbook.  
> In that defaults.yml there is a variable referencing to user 'postgres' as ansible\_become\_user
> 
> ansible\_become\_user: postgres
> 
> So I assume I'll have to figure out how to utilize different become users in a single playbook. I tried or overrule the defaults entry temporarily with ...
> 
> become: yes  
> become\_user: [root\_user]
> 
> ... inside the TASK, but that apparently was not interpreted in the way I wanted

The default for the become\_user should be root and you can override it in your tasks.

It can be tricky, so here an example how I do it:

- name: Ensure PostgreSQL database is initialized (RedHat, Suse)  
command: "{{ postgresql\_initdb\_path }} -D {{ postgresql\_data\_dir }}"  
become: true  
become\_user: "{{ postgresql\_user }}"  
vars:  
ansible\_ssh\_pipelining: true  
when:  
- postgresql\_data\_version\_dir.stat.exists == false  
- ansible\_os\_family in ['Alpine', 'RedHat', 'Suse']

Regards

```
          Racke

```
