LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-03-2022, 12:23 PM   #1
skagnola
Member
 
Registered: May 2017
Distribution: CentOS
Posts: 41

Rep: Reputation: Disabled
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.

Last edited by skagnola; 11-03-2022 at 01:35 PM.
 
Old 11-04-2022, 04:51 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,387
Blog Entries: 1

Rep: Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665
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
 
1 members found this post helpful.
Old 11-04-2022, 08:21 AM   #3
skagnola
Member
 
Registered: May 2017
Distribution: CentOS
Posts: 41

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Ansible Guide: Manage Files using Ansible LXer Syndicated Linux News 0 04-26-2019 11:32 AM
LXer: Ansible Guide: Create Ansible Playbook for LEMP Stack LXer Syndicated Linux News 0 04-13-2019 06:03 AM
LXer: Ansible Tutorial: Introduction to simple Ansible commands LXer Syndicated Linux News 0 05-21-2018 11:28 AM
LXer: Ansible Tutorial: Intorduction to simple Ansible commands LXer Syndicated Linux News 0 01-14-2018 06:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration