Search code examples
ansibleansible-inventory

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


Hey Guys maybe you can help me on this Problem. Im trying to create some Folders in my apache role.(The role was initially from geerlenguy)

This is my a part of my vars file for my host:

 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>

My Task looks like this at the moment:

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

But this seems to looks like garbage to me. I can't get it to work because of this error:

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

Could you guys tell me how to access the documentroot var correctly in my Task? Would be great!


Solution

  • Try this

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

    Migrate from with_X to loop

      loop: "{{ apache_vhosts }}"
    


    Example. The task

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

    gives

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