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

10,404

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.

Share:
10,404

Related videos on Youtube

user182944
Author by

user182944

Updated on June 04, 2022

Comments

  • user182944
    user182944 almost 2 years

    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.

  • user182944
    user182944 almost 7 years
    Adding .stdout resolved this issue. But still I am getting an error since I need to perform chown with sudo as an ec2-user. Please let me know how to execute the same with sudo for ec2-user.
  • techraf
    techraf almost 7 years
    You, yourself wrote "If I hardcode the value of username then it works fine." I don't see any mention of sudo in the question. If you have another question, please post a new question.