Ansible 没有安装 Apache 重写模块

Ansible isn't installing Apache rewrite module

Ansible 没有为 Apache 安装重写模块。

Ansible 任务:

- name: Enable mod_rewrite
  apache2_module:
    state: present
    name: rewrite

成绩:

TASK [apache : Enable mod_rewrite] *********************************************
fatal: [local_vm]: FAILED! => {"changed": false, "failed": true, "msg": "Error executing /usr/sbin/apache2ctl: AH00526: Syntax error on line 33 of /etc/apache2/sites-enabled/mysite.conf:\nInvalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration\n"}

我注意到该模块未启用:

me@server:/etc/apache2/mods-enabled$ ls | grep write
me@server:/etc/apache2/mods-enabled$

更新 #1

这里是mysite.conf的内容:

32: # Redirect to https
33: RewriteEngine On
34: RewriteCond %{REQUEST_URI} !^\/lightbox

你能试试这个吗:

- name: Enable rewrite module
  apache2_module:
    name: rewrite
    state: present
    ignore_configcheck: True

试试这个:

为 .conf 文件创建模板并添加所需的重写。 (这是 Jinja2 语法)。

然后内容将包含在执行剧本时生成的 Apache VirtualHost 文件中。

您还可以使用 shell Ansible 模块来 运行 a2enmod 命令。这样您就可以使用 Apache 自己的工具来启用 mod_rewrite,并且您不依赖于 apache2_module Ansible 模块。

像这样:

- name: enable mod_rewrite in apache.
  shell: "a2enmod rewrite"
  register: a2enmodrewrite
  changed_when: "'already enabled' not in a2enmodrewrite.stdout"

另请注意,在此示例中,我使用 register 指令存储命令(shell 行)的输出。

然后我指定该任务应该 在 运行 时间 期间被标记为更改if 寄存器内容(通过 a2enmodrewrite.stdout 访问)不包含 'already enabled'.

因此,您启用了 mod_rewrite,并确保当您 运行 Ansible 剧本时,如果模块尚未启用,您只会收到更改通知。