linux - Ansible 文件模块错误 - chown 失败 : failed to look up user

标签 linux ansible

我正在尝试使用 file 模块更改文件的 owner。我试过这段代码:

---
- 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 }}

错误:

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 模块给我这个输出:

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"
        ]
    }
}

注意:

如果我对 username 的值进行硬编码,那么它可以正常工作:

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

请让我知道如何解决这个问题。

最佳答案

没有问题。您要使用 username.stdout,而不是 username

请检查您使用调试模块打印的值并使用推理。

关于linux - Ansible 文件模块错误 - chown 失败 : failed to look up user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45004692/

相关文章:

Java URLConnection 适用于 windows,但不适用于 linux

linux - Ubuntu中vscode中的实时服务器未在Android手机中加载

linux - 在 svn 中有一些文件夹(它的地址)如何检查上次修改的确切时间?

c++ - 如何为 QWebEngineView 设置 OffTheRecord 配置文件?

c++ - 尽管存在符号链接(symbolic link),ld 仍无法加载库

linux - 尝试从 ansible 中通过 screen 启动进程

ansible - 在 Ansible 中将 roles/rolename/tasks/main.yml 重命名为 rolename.yml

ansible - 使用ansible docker_container模块获取容器的命令输出

ansible - 是否有将日期字符串转换为纪元的 ansible 函数

azure - Ansible 安装所有必需的 Azure 模块