Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible file module error - chown failed: failed to look up user

Tags:

linux

ansible

I am trying to change the owner of a file using file module. I tried this piece of code:

---
- hosts: super_group
  remote_user: ec2-user
  tasks:
  - name: Checking the user name
    shell: /usr/bin/whoami
    register: username

  - name: Debugging the whoami username
    debug: msg={{ username }}

  - name: Changing the owner of a file
    file: path=/home/ec2-user/test owner={{ username }}

Error:

TASK [Changing the owner of a file] ********************************************
fatal: [test]: FAILED! => {"changed": false, "failed": true, "gid": 0, "group": "root", "mode": "0644", "msg": "chown failed: failed to look up user {'stderr_lines': [], 'changed': True, 'end': '2017-07-10 01:49:11.495709', 'stdout': 'ec2-user', 'cmd': '/usr/bin/whoami', 'start': '2017-07-10 01:49:11.492286', 'delta': '0:00:00.003423', 'stderr': '', 'rc': 0, 'stdout_lines': ['ec2-user']}", "owner": "ec2-user", "path": "/home/ec2-user/test", "secontext": "unconfined_u:object_r:user_home_t:s0", "size": 0, "state": "file", "uid": 1000}
        to retry, use: --limit @/home/ec2-user/ansible/test.retry

debug module is giving me this output:

TASK [Debugging the whoami username] *******************************************
ok: [test] => {
    "msg": {
        "changed": true,
        "cmd": "/usr/bin/whoami",
        "delta": "0:00:00.003423",
        "end": "2017-07-10 01:49:11.495709",
        "rc": 0,
        "start": "2017-07-10 01:49:11.492286",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "ec2-user",
        "stdout_lines": [
            "ec2-user"
        ]
    }
}

Note:

If I hardcode the value of username then it works fine:

  - name: Changing the owner of a file
    file: path=/home/ec2-user/test owner=ec2-user

Please let me know how to resolve this issue.

like image 842
user182944 Avatar asked Oct 21 '25 11:10

user182944


1 Answers

There is no issue. You want to use username.stdout, not username.

Please check the value you printed with the debug module and use reasoning.

like image 146
techraf Avatar answered Oct 23 '25 02:10

techraf