become: true does not work but -b works

Hello team,

I am running a role like below despite putting become true for install task i get error. However if run with -b it works.

Getting error while running this ,

ansible-playbook prometheusjmxrole.yml -i …/inventory/staging.yaml

The full traceback is:

WARNING: The below traceback may not be related to the actual failure.

File “/tmp/ansible_file_payload_CA4MQM/ansible_file_payload.zip/ansible/module_utils/basic.py”, line 841, in set_owner_if_different

os.lchown(b_path, uid, -1)

failed: [10.xx.xx.xxx] (item=/etc/jmx_prometheus/) => {

“ansible_loop_var”: “item”,

“changed”: false,

“gid”: 0,

“group”: “root”,

“invocation”: {

“module_args”: {

“_diff_peek”: null,

“_original_basename”: null,

“access_time”: null,

“access_time_format”: “%Y%m%d%H%M.%S”,

“attributes”: null,

“follow”: true,

“force”: false,

“group”: “confluent”,

“mode”: 493,

“modification_time”: null,

“modification_time_format”: “%Y%m%d%H%M.%S”,

“owner”: “cp-kafka”,

“path”: “/etc/jmx_prometheus/”,

“recurse”: false,

“selevel”: null,

“serole”: null,

“setype”: null,

“seuser”: null,

“src”: null,

“state”: “directory”,

“unsafe_writes”: false

}

},

“item”: “/etc/jmx_prometheus/”,

“mode”: “0755”,

“msg”: “chown failed: [Errno 1] Operation not permitted: ‘/etc/jmx_prometheus/’”,

“owner”: “root”,

“path”: “/etc/jmx_prometheus/”,

“secontext”: “unconfined_u:object_r:etc_t:s0”,

“size”: 4096,

“state”: “directory”,

“uid”: 0

Task which throwing error:

name: Create Prometheus install and config directory

become: true. <------ does not work

file:

path: “{{ item }}”

state: directory

mode: 0755

owner: “{{user}}”

group: “{{group}}”

loop:

  • “{{ jmxexporter_jar_path | dirname }}”

  • “{{ jmxexporter_config_path }}”

If i put become: true at role playbook it works but i dont want to give blanket become: true hence i was trying to do at task level.

vim prometheusjmxrole.yml

name: Deploy jmx_exporter

hosts: all

become: true. <---------------- works fine if we set it here but i dont want it in this way.

tasks:

  • name: Importing jmx prometheus role

import_role:

name: prometheusjmx

We have tests for this and i just tested manually, become: true on a
task does work. Tried with your task (supplying my own values for the
vars) and it works the same, putting it on the task, the play, a
block, role or command line. The only way i get it to 'not work' is
setting ansible_become: false , but that makes them ALL not work, not
just the task level become.

I cannot tell from the email as it breaks formatting, but my guess
would be that it is set to the task.

Hello Brian,

Yes it works , Issue was my role was picking the values from roles directory instead from current directory hence despite making changes at current file become was not working.