You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用wp-cli更新WordPress的Ansible Playbook权限错误求助

问题:Ansible结合wp-cli更新WordPress时的权限错误

主机配置

ansible_user=ansible

Playbook配置

- name: WordPress setup
  hosts: servers
  gather_facts: false
  remote_user: ansible
  become: true
  roles:  
  - wp

角色任务

- name: Update admin user's password
  command: wp user update admin
           --user_pass="{{ wp_admin_pwd }}"
  args:
      chdir: "/var/www/{{ domain_name }}"
  become: yes
  become_user: www-data

错误信息

Failed to set permissions on the temporary files Ansible needs to create when becoming an unprivileged user
(rc: 1, err: chmod: invalid mode: ‘A+user:www-data:rx:allow’
Try 'chmod --help'


原因与解决方案

错误原因

当Ansible从特权用户(此处为root,因Playbook中become: true已切换到root)再切换到非特权用户www-data时,默认会尝试使用**ACL(访问控制列表)**设置临时文件权限,但目标服务器未安装ACL工具,或文件系统不支持ACL,导致chmod命令执行失败。

解决方案

方案1:安装ACL工具

在目标服务器上安装acl包,让Ansible可以正常使用ACL权限设置:

  • 对于Debian/Ubuntu系统,添加前置任务:
- name: 安装acl工具
  apt:
    name: acl
    state: present
  become: true
  • 对于RHEL/CentOS系统,添加前置任务:
- name: 安装acl工具
  yum:
    name: acl
    state: present
  become: true

方案2:修改Ansible配置,禁用ACL依赖

通过调整Ansible的特权升级参数,避免使用ACL:

  1. 在主机配置文件(inventory)中添加:
ansible_become_flags=-H -S -n
  1. 或者在ansible.cfg中添加:
[privilege_escalation]
become_allow_same_user = yes

这样Ansible切换用户时会直接使用sudo,不依赖ACL。

方案3:简化任务的特权升级配置

由于Playbook已经全局设置become: true(切换到root),任务中无需重复become: yes,可简化任务配置:

- name: Update admin user's password
  command: wp user update admin --user_pass="{{ wp_admin_pwd }}"
  args:
      chdir: "/var/www/{{ domain_name }}"
  become_user: www-data

配合方案2的配置,即可避免权限错误。


内容的提问来源于stack exchange,提问作者Roberto Jobet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 02:25:44