首页 > 解决方案 > Ansible 错误是:'unicode object'没有属性

问题描述

嘿伙计们,也许你可以帮助我解决这个问题。我试图在我的 apache 角色中创建一些文件夹。(该角色最初来自 geerlenguy)

这是我的主机的 vars 文件的一部分:

 apache_vhosts:
  - servername: myhost.com
    documentroot: "/var/www/html/web"
    extra_parameters: |
      <IfModule mod_rewrite.c>
          RewriteEngine On
          RewriteCond %{HTTPS} off
          RewriteCond %{REQUEST_URI} !^/server-status.*
          RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
      </IfModule>
      <Directory "/var/www/html/web">
        AuthType Basic
        Require valid-user
        AuthName "Please authenticate"
        AuthUserFile /var/www/html/.htpasswd
      </Directory>
  - servername: secondhost.com
    documentroot: "/var/www/learning/web"
    extra_parameters: |
      <IfModule mod_rewrite.c>
          RewriteEngine On
          RewriteCond %{HTTPS} off
          RewriteCond %{REQUEST_URI} !^/server-status.*
          RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
        </IfModule>
      <Directory "/var/www/learning/web">
        AuthType Basic
        Require valid-user
        AuthName "Please authenticate"
        AuthUserFile /var/www/learning/.htpasswd
      </Directory>

我的任务目前看起来像这样:

- name: Create Apache vhost Folders
  file:
    path: "{{ item.0.documentroot }}"
    state: directory
    mode: '0755'
    owner: root
    group: root
  with_items:
    - apache_vhosts

但这对我来说似乎是垃圾。由于这个错误,我无法让它工作:

fatal: [webserver.company.com]: FAILED! => {
    "msg": "The task includes an option with an undefined variable. The error was: 'unicode object' has no attribute 'documentroot'

你们能告诉我如何documentroot在我的任务中正确访问 var 吗?会很好!

标签: ansibleansible-inventory

解决方案


尝试这个

- name: Create Apache vhost Folders
  file:
    path: "{{ item.documentroot }}"
    ...
  with_items: "{{ apache_vhosts }}"

从 with_X 迁移到循环

  loop: "{{ apache_vhosts }}"


例子。任务

    - debug:
        msg: "{{ item.documentroot }}"
      loop: "{{ apache_vhosts }}"

    "msg": "/var/www/html/web"
    "msg": "/var/www/learning/web"

推荐阅读