Ansible 错误是:'unicode object' 没有属性

Ansible The error was: 'unicode object' has no attribute

嘿伙计们,也许你可以帮助我解决这个问题。 我试图在我的 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 变量吗? 太棒了!

试试这个

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

Migrate from with_X to loop

  loop: "{{ apache_vhosts }}"


例子。任务

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

给予

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