# Understanding become: true and become\_user: root not working in custom GCP connection setup

**URL:** <https://forum.ansible.com/t/understanding-become-true-and-become-user-root-not-working-in-custom-gcp-connection-setup/42685>\
**Category:** Get Help\
**Tags:** gcp, playbook, ansible, awx\
**Created:** [May 15, 2025, 5:45pm UTC](https://forum.ansible.com/t/understanding-become-true-and-become-user-root-not-working-in-custom-gcp-connection-setup/42685 "2025-05-15T17:45:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SaravanaSelvaraj](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/saravanaselvaraj/32/2841_2.png) [@SaravanaSelvaraj](https://forum.ansible.com/u/SaravanaSelvaraj)\
**Post date:** [May 15, 2025, 5:45pm UTC](https://forum.ansible.com/t/understanding-become-true-and-become-user-root-not-working-in-custom-gcp-connection-setup/42685/1 "2025-05-15T17:45:20Z")

</div>

**become\_user: root is not working as expected**.  
Dear community,

In the background, We have established the connectivity to the GCP VMs using the Service account JSON with below command

```auto
# --- Connect using OS Login and static SSH key ---
exec gcloud compute ssh "$host" \
    "${opts[@]}" \
    --tunnel-through-iap \
    --ssh-key-file="$SSH_KEY_PATH" \
    --strict-host-key-checking=no \
    --quiet \
    --command="$cmd"

```

So when executing the normal tasks like gather\_facts, ls command and copy to /tmp directory works fine.

But when want to create some directory in a path owned by root (/) , I get permission denied error.  
Suppose below command

```auto
- name: Ensure target directory exists
  file:
    path: /opt/code/awx/
    state: directory
    mode: '0755'
    recurse: true
  become: true
  become_method: sudo
  become_user: root

```

gives this error

```auto
  "path": "/opt/code/awx/",
  "msg": "There was an issue creating /opt as requested: [Errno 13] Permission denied: b'/opt'",

```

The same works when I do something like this with sudo in shell module.

```auto
- name: Create target directory and set permissions
  ansible.builtin.shell: |
    sudo mkdir -p /opt/code/awx
    sudo chmod -R 777 /opt/code/awx
  become: true

```

I am not able to achieve this with multiple combinations specified [here](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_privilege_escalation.html).  
My User has root privileges but still fails with become:true & become\_user: root.

From server, it successfully switched to root.

```auto
sa_XXXXXX@XXXX-d-ubuntu24-XXX-1:~$ sudo -i
root@XXXX-d-ubuntu24-XXX-1:~#

```

Can some one please help here ? What am I missing here ?

Best regards

---

<div class="post-metadata">

**Author:** ![SaravanaSelvaraj](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/saravanaselvaraj/32/2841_2.png) [@SaravanaSelvaraj](https://forum.ansible.com/u/SaravanaSelvaraj)\
**Post date:** [May 15, 2025, 8:19pm UTC](https://forum.ansible.com/t/understanding-become-true-and-become-user-root-not-working-in-custom-gcp-connection-setup/42685/2 "2025-05-15T20:19:28Z")

</div>

Is this because I am using a custom way of SSH ( with gcloud ) which does not support become:true ? since I have not defined what become: true means in the connection mechanism .

---

<div class="post-metadata">

**Author:** ![SaravanaSelvaraj](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/saravanaselvaraj/32/2841_2.png) [@SaravanaSelvaraj](https://forum.ansible.com/u/SaravanaSelvaraj)\
**Post date:** [May 16, 2025, 1:46pm UTC](https://forum.ansible.com/t/understanding-become-true-and-become-user-root-not-working-in-custom-gcp-connection-setup/42685/3 "2025-05-16T13:46:46Z")

</div>

This is solved by adding below command in the wrapper script.  
`cmd="sudo -- bash -c $(printf '%q ' "$cmd")"`

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [June 15, 2025, 1:47pm UTC](https://forum.ansible.com/t/understanding-become-true-and-become-user-root-not-working-in-custom-gcp-connection-setup/42685/4 "2025-06-15T13:47:23Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
