LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Ansible nested variable is adding [''] around the variable (https://www.linuxquestions.org/questions/programming-9/ansible-nested-variable-is-adding-%5B%5D-around-the-variable-4175718418/)

skagnola 11-03-2022 11:23 AM

Ansible nested variable is adding [''] around the variable
 
Hello!

I have the following vars/main.yml file I am sourcing for an Ansible task. Ultimately, the purpose being that if there is a list of sites needed, they can be put in the var and looped over.

Code:

vars/main.yml

new_site:
 - sitename

The playbook task looks like so
Code:

playbook.yml

tasks:
    - name: Include local vars
      include_vars:
        dir: vars

    - name: Ensure proper ownership of the images and cache dirs
      ansible.builtin.file:
        path: /var/www/html/{{ item.site }}/{{ item.dir }}
        owner: apache
        group: apache
        mode: 0755
      with_items:
        - { site: "{{ new_site }}", dir: 'cache' }
        - { site: "{{ new_site }}", dir: 'images' }
      when: "'all_sites' in group_names"

This is pulling in the expected 'sitename' but there is a surrounding [' '] added to it and of course, it is failing the task since that is not the name of the dirs.

Code:

TASK [Ensure proper ownership of the images and cache dirs] *************************************************
failed: [host1] (item={'site': ['sitename'], 'dir': 'cache'}) => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "ansible_loop_var": "item", "changed": false, "item": {"dir": "cache", "site": ["sitename"]}, "msg": "file (/var/www/html/['sitename']/cache) is absent, cannot continue", "path": "/var/www/html/['sitename']/cache", "state": "absent"}
failed: [host1] (item={'site': ['sitename'], 'dir': 'images'}) => {"ansible_loop_var": "item", "changed": false, "item": {"dir": "images", "site": ["sitename"]}, "msg": "file (/var/www/html/['sitename']/images) is absent, cannot continue", "path": "/var/www/html/['sitename']/images", "state": "absent"}


Admittedly, I'm not very familiar with/understand what is presumably a more advanced method of trying to reference a main variable list within 'with_items' the way I have it now.

NevemTeve 11-04-2022 03:51 AM

I don't understand what you wish to accomplish here, but I'm sure it would be simpler without ansible. Sg like this:
Code:

someuser=...
somehost=...
user=apache
group=apache
dirlist="/var/www/html/cache /var/www/html/images"
ssh "$someuser@$somehost" "chown $user:$group $dirlist; chmod 0755 $dirlist" </dev/null


skagnola 11-04-2022 07:21 AM

Quote:

Originally Posted by NevemTeve (Post 6390285)
I don't understand what you wish to accomplish here, but I'm sure it would be simpler without ansible. Sg like this:
Code:

someuser=...
somehost=...
user=apache
group=apache
dirlist="/var/www/html/cache /var/www/html/images"
ssh "$someuser@$somehost" "chown $user:$group $dirlist; chmod 0755 $dirlist" </dev/null


Hey, NevemTeve. Thanks for the response! I will see if I can mess around with a variation like this via a bash script, too. :-)


The motivator for using Ansible, there is some other stuff going on the playbook that I left out of the code here - connecting to a different machine and creating supporting databases, the user that connects to the database, assigning their perms, etc. Creating the /var/www/html/[site,site2,site3]/ dirs.

That stuff is working well.

I wanted to try getting this particular task to have the ability to scale to having the...

Code:

new_site:
 - sitename

... become a longer list of sites like ...

Code:

new_site:
 - sitename
 - sitename2
 - sitename3
...
 - sitename10

Then, the task would just loop over each of the site docroots making sure the appropriate permissions are applied to the 'cache' and 'images' dirs in each. Each of the /var/www/html/[site,site2,site3]/[images,cache]/ dirs. Also, trying to keep the action of applying the perms in one 'step' and not breaking it out into two Ansible tasks.


All times are GMT -5. The time now is 08:23 AM.