Answers: <p>Running ansible with -vvv will tell you which ansible config you're using</p>
  <p>For instance</p>
  <pre><code>me@mycomputer:~$ ansible -vvv -m setup localhost
  ansible 2.10.8
    config file = None
  </code></pre>
  <p>shows that I'm not using any config file</p>
  <p>chances are pretty OK that is what is happening to you too.  ansible.cfg should be in the current directory or ~/.ansible  (maybe some other places depending on how you installed ansible) you can specify where it is with <code>ANSIBLE_CONFIG=/path/to/config</code> in your environment variables</p>
  <hr />
  <p>The other thing is, the parameters you used in the config file are actually the parameters that go in the playbook, not in the config file</p>
  <p><a href="https://docs.ansible.com/ansible/latest/reference_appendices/config.html" rel="nofollow noreferrer">https://docs.ansible.com/ansible/latest/reference_appendices/config.html</a></p>
  <p>explains where they go in the config file.</p>
  <p>When you're running adhoc tasks, you can use them on the commandline (as you wrote) with <code>-e remote_user=gcp_user -e ansible_become=yes -e ansible_become_method=sudo -e ansible_become_user=root</code></p>
  <p><a href="https://docs.ansible.com/ansible/latest/reference_appendices/config.html" rel="nofollow noreferrer">https://docs.ansible.com/ansible/latest/reference_appendices/config.html</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2023-11-06T05:16:52.473Z
Answers: 2023-11-06T05:16:52.473Z
Answers: Peter Turner
Body: <p>Say I want to execute the following command on target hosts (deployed on GCP): <code>ansible all -a &quot;head -1 /etc/shadow&quot;</code>.
  The command demands the root access. With the default Ansible configuration, I get the following error:</p>
  <pre><code>host1 | FAILED | rc=1 &gt;&gt;
  head: cannot open '/etc/shadow' for reading: Permission deniednon-zero return code
  </code></pre>
  <p>Hence, I extended my <code>ansible.cfg</code> to include the following:</p>
  <pre><code>remote_user = gcp_user
  ansible_become=yes
  ansible_become_method=sudo
  ansible_become_user=root
  </code></pre>
  <p>Despite these changes, I can't run the command mentioned above. I still get the same error. I am, however, capable of executing the following command: <code>ansible all -a &quot;sudo cat /etc/shadow&quot;</code></p>
  <p>Thus, it seems that <code>become_user=yes</code> does not take any effect.</p>
  
Comments: CC BY-SA 4.0
Comments: 2023-05-29T11:31:05.547Z
Comments: Try without sudo and --become as argument
Comments: Serve Laurijssen
Tags: <ansible><google-cloud-platform><gcp><ssh><ansible-inventory>
Title: ansible_become has no effect despite the ability to execute sudo on GCP hosts
UserDisplayName: SkogensKonung
ThreadText: # ansible_become has no effect despite the ability to execute sudo on GCP hosts
  
  (Asked by: username_0 on 2023-05-28)
  
  Say I want to execute the following command on target hosts (deployed on GCP): `ansible all -a "head -1 /etc/shadow"`.
  The command demands the root access. With the default Ansible configuration, I get the following error:
  
  ```
  host1 | FAILED | rc=1 >>
  head: cannot open '/etc/shadow' for reading: Permission deniednon-zero return code
  
  ```
  
  Hence, I extended my `ansible.cfg` to include the following:
  
  ```
  remote_user = gcp_user
  ansible_become=yes
  ansible_become_method=sudo
  ansible_become_user=root
  
  ```
  
  Despite these changes, I can't run the command mentioned above. I still get the same error. I am, however, capable of executing the following command: `ansible all -a "sudo cat /etc/shadow"`
  
  Thus, it seems that `become_user=yes` does not take any effect.
  
  **Tags:** ansible, google-cloud-platform, gcp, ssh, ansible-inventory
  
  > **Comments:**
  > **username_1**: Try without sudo and --become as argument  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2023-11-06. Score: 0
  
  Running ansible with -vvv will tell you which ansible config you're using
  
  For instance
  
  ```
  me@mycomputer:~$ ansible -vvv -m setup localhost
  ansible 2.10.8
    config file = None
  
  ```
  
  shows that I'm not using any config file
  
  chances are pretty OK that is what is happening to you too. ansible.cfg should be in the current directory or ~/.ansible (maybe some other places depending on how you installed ansible) you can specify where it is with `ANSIBLE_CONFIG=/path/to/config` in your environment variables
  
  ---
  
  The other thing is, the parameters you used in the config file are actually the parameters that go in the playbook, not in the config file
  
  <https://docs.ansible.com/ansible/latest/reference_appendices/config.html>
  
  explains where they go in the config file.
  
  When you're running adhoc tasks, you can use them on the commandline (as you wrote) with `-e remote_user=gcp_user -e ansible_become=yes -e ansible_become_method=sudo -e ansible_become_user=root`
  
  <https://docs.ansible.com/ansible/latest/reference_appendices/config.html>
  
  
  ---
  
  