【问题标题】:Need permissions changed of a folder + files together using Ansible synchronize module需要使用 Ansible 同步模块一起更改文件夹 + 文件的权限
【发布时间】:2021-04-20 09:20:39
【问题描述】:

我允许用户选择输入 3 位数字来设置正在传输的文件或文件夹的权限,例如 -e myperm: 775

我在提供rsync_opts: --chmod:F775(同步模块)的地方使用ansible将目标上传输的文件/文件夹的权限更改为775

- name: sync file
  synchronize:
    src: /tmp/file.py
    dest: /home/myuser/file.py
    mode: push
    rsync_opts:
      - "--chmod=F0{{ myperm }}"

以上方法适用于文件;但是,当 src: /tmp/folder 时,这同样不适用于传输文件夹

我在 ansible 中尝试了 --chmod=D0{{ myperm }},F0{{ myperm }},但它转换为 --chmod=D0775 F0775 并出现此错误:

msg": "Unexpected remote arg: user@desthost:/tmp/folder\nrsync error: syntax or usage error (code 1) at main.c(1344) [sender=3.1.2]\n", "rc": 1}

您能否建议带有变量mypermrsync_opts 来更改文件和文件夹的权限?

任何其他解决方案也可以。

【问题讨论】:

  • rsync 允许多个 chmod 选项,您是否尝试过:rsync_opts: - "--chmod=F0{{ myperm }}" - "--chmod=D0{{ myperm }}"
  • @guido 它仍然不起作用。查看输出:failed: [desthost] (item=/tmp/folder) => {"changed": false, "cmd": "/bin/rsync --delay-updates -F --compress --archive --rsh=/usr/share/centrifydc/bin/ssh -S none -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null - \"--chmod=D0777\" - \"--chmod=F0777\" --out-format=<<CHANGED>>%i %n%L destuser@desthost:/tmp/folder /web/playbooks/filecopy/tmpfiles/107/", "item": "/tmp/folder", "msg": "rsync: - \"--chmod=D0777\" - \"--chmod=F0777\": unknown option\nrsync error: syntax or usage error (code 1) at main.c(1568) [client=3.1.2]\n", "rc": 1}
  • "rsync: Invalid argument passed to --chmod (F0777 - --chmod=D0777)\nrsync error: syntax or usage error (code 1) at main.c(1568) [client=3.1.2]\n", "rc": 1}
  • 啊抱歉,这两个 chmod 项应该放在不同的行上(形成一个数组);或者,您可以使用紧凑的语法 rsync_opts: [ '--chmod=F0{{ myperm }}', '--chmod=D0{{ myperm }}' ]
  • @guido 使用新的不同行我能够让它同时适用于文件和文件夹。您能否将其发布为我接受的答案?顺便说一句,帮了大忙!

标签: directory ansible permissions file-permissions rsync


【解决方案1】:

在模块生成 rsync 命令行时解析逗号分隔的参数似乎有问题,但是,由于 rsync 允许多个 chmod 选项,您可以将任务重写为:

[..]
rsync_opts:  
  - "--chmod=F0{{ myperm }}"
  - "--chmod=D0{{ myperm }}"

【讨论】:

    猜你喜欢
    • 2011-04-13
    • 1970-01-01
    • 2016-06-18
    • 2012-02-16
    • 2017-07-28
    • 2012-04-27
    • 2018-09-06
    • 2020-10-30
    • 1970-01-01
    相关资源
    最近更新 更多