LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 07-05-2022, 10:34 AM   #1
skagnola
Member
 
Registered: May 2017
Distribution: CentOS
Posts: 41

Rep: Reputation: Disabled
Declaring Ansible variable within a var/main.yml


Hello, all

I'm looking for help, another pair of eyes on this. I have a feeling this is my misunderstanding of the way a 'root' playbook vars file is used to declare additional vars within it. :-)

What I am trying to do is have a single var line that can be edited for whatever RPM version is desired to install vs. using a text substitution method in VIM or similar.

Code in the vars/main.yml

Code:
phpver: php73

php_rpms:
 - '{{ vars["phpver"] }}'           # this works fine if left alone as-is, being a single line in the list. 
 - '{{ vars["phpver"] }}'-php-cli   # if I add this, it blows up due to the '-' after the declared var. 
 ...                                # multiple lines like the second one above for other RPMs following the same format.
I can make this work like so...

Code:
#phpver: php73                     # not using this guy, of course. just a fly on the wall. 

php_rpms:
 - php73
 - php73-php-cli
 - php73-php-fpm
...

... but then I have to use VIM and the usual :%s/php73/php74/g stuff. Figured if there was a way to have just the one variable phpver: whatever_you_want is easier/quicker to maintain after creation.
 
Old 07-05-2022, 10:27 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
I am only new to ansible myself, but looking at the doco I am wondering what the reason for "var[]" is?

Is it not as simple as:
Code:
phpver: php73

php_rpms:
 - '{{phpver}}'
 - '{{phpver}}-php-cli'
...
I was looking at the Simple Variable section found here
 
1 members found this post helpful.
Old 07-06-2022, 07:53 AM   #3
skagnola
Member
 
Registered: May 2017
Distribution: CentOS
Posts: 41

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by grail View Post
I am only new to ansible myself, but looking at the doco I am wondering what the reason for "var[]" is?

Is it not as simple as:
Code:
phpver: php73

php_rpms:
 - '{{phpver}}'
 - '{{phpver}}-php-cli'
...
I was looking at the Simple Variable section found here
Hey, grail! Thanks for the response.

I'm kicking myself! Heh, that was it.

I believe I was accustomed to using with_items loop in a playbook task, something like /var/opt/{{ item }}/. It got my wires crossed and I was thinking I had to do a single quote close around the mustaches only, in the root var file and not on the whole line. Doh!

Anywho, once again, you all are the best! Thanks so much!

P.S. I used the var[] from my troubleshooting and found a reference to try a var lookup that way.
 
  


Reply



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
question: 'onclick' within 'onmouseover' within 'form' within 'table' - how is it possible? rblampain Programming 4 04-25-2017 08:49 PM
declaring a thread within a class knobby67 Programming 6 07-07-2016 09:44 AM
Declaring and using a global variable in Kernel naftilos Linux - Kernel 3 06-15-2014 07:43 PM
C/C++ declaring an array with non-constant size variable (related to compiler) haxpor Programming 6 12-13-2009 05:00 PM

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

All times are GMT -5. The time now is 10:51 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