How to define with_items by variables in ansible 1.4?

In ansible 1.0, I can use $foo in with_items. For example,

with_items:

  • $404_error_page
  • $503_error_page
  • $403_error_page

Ansible 1.0 works well. It can find the variable and replace the value.

In Ansible 1.4, I try to use {{ foo }} or “{{ foo }}”, but both are wrong.

Case1, Syntax Error.

with_items:

  • {{ 404_error_page }}
  • {{ 503_error_page }}
  • {{ 403_error_page }}

Case2, ansible would not find the variable.

with_items:

  • “{{ 404_error_page }}”
  • “{{ 503_error_page }}”
  • “{{ 403_error_page }}”

Who knows how to define with_items by variables in ansible 1.4?

with_items:

  • bar
  • baz

Is all you need now

– Michael

I tried, but it is failed.

My task is:

  • name: Transfer custom error pages and restart httpd service
    action: copy src={{ local_custom_error_pages_loc }}/{{ item }} dest={{ custom_error_page_dir }}/{{ item }} owner=root group=root mode=0644
    with_items:
  • 404_error_page
  • 503_error_page
  • 403_error_page
    Note: 404_error_page, 503_error_page and 403_error_page are variable names which are defined in vars.yml.

404_error_page: 404_ErrorPage.html
503_error_page: MaintenancePage.html
403_error_page: 403_ErrorPage.html

The error message is:

failed: [192.168.126.143] => (item=404_error_page) => {“failed”: true, “item”: “404_error_page”}
msg: could not find src=/home/agilairedev/playbooks/jboss_deploy_harden/repositories/custom_error_pages/404_error_page
failed: [192.168.126.143] => (item=503_error_page) => {“failed”: true, “item”: “503_error_page”}
msg: could not find src=/home/agilairedev/playbooks/jboss_deploy_harden/repositories/custom_error_pages/503_error_page
failed: [192.168.126.143] => (item=403_error_page) => {“failed”: true, “item”: “403_error_page”}
msg: could not find src=/home/agilairedev/playbooks/jboss_deploy_harden/repositories/custom_error_pages/403_error_page

FATAL: all hosts have already failed – aborting

We can see, variable 404_error_page is not replaced with its value, 404_ErrorPage.html.
If I use $foo, it works well.

with_items:

  • $404_error_page
  • $503_error_page
  • $403_error_page

Hmm, it sounds like you want to use the names of the variables, and the system is trying to evaluate them for you.

You can solve this by renaming the variables.

I changed the variable name from 404_error_page to error_404_page which starts with letter. But it cannot resolve the issue.

I wrote:
with_items:

  • error_404_page

The error still occur.

failed: [192.168.126.143] => (item=error_404_page) => {“failed”: true, “item”: “error_404_page”}
msg: could not find src=/home/dev/playbooks/harden/repositories/custom_error_pages/error_404_page

We can see that the system didn’t take it as a variable.

If I write:

with_items:

  • {{ error_404_page }}

Syntax Error

  • “{{ error_404_page }}”

If that’s the name of your variable.

Hopefully, you will note Ansible gave you a longer syntax error message that told you exactly what to do about quoting, read the rest of the output :slight_smile:

Thank you. Quoting resolves the issue. But I want to say, indeed, the rest of the output didn’t tell me what to do. :slight_smile:
The whole output is:

ERROR: Syntax Error while loading YAML script, /home/dev/playbooks/harden/install.yml
Note: The error may actually appear before this position: line 39, column 8

with_items:

  • {{ error_404_page }}

I know some Syntax Error messages describe exactly what to do about quoting. But “with_items” does not. :slight_smile:

I’ll file a ticket to add this to the suggester code, thanks!