示例。任务
嘿,伙计们,也许你们可以帮我解决这个问题。我试图在我的apache角色中创建一些文件夹。(这个角色最初来自吉利(
这是我的主机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 }}"
从with_X迁移到循环
loop: "{{ apache_vhosts }}"
示例。任务
- debug:
msg: "{{ item.documentroot }}"
loop: "{{ apache_vhosts }}"
给出
"msg": "/var/www/html/web"
"msg": "/var/www/learning/web"