0

I'm configuring Ansible role to install a specific Nginx version. This is the folder structure:

nginx
├── defaults
│   └── main.yml
├── handlers
│   └── main.yml
├── molecule
│   └── default
│       ├── collections.yml
│       ├── converge.yml
│       ├── molecule.yml

│       └── verify.yml
├── tasks
│   └── main.yml
└── templates
    ├── nginx.j2
    └── nginx.repo

Now I have two problems:

  1. How can define nginx_package variable using version variable?
---
defaults:
 user: nginx
 group: nginx
 version: 1.19.2
 #nginx_package: nginx-1.19.2-1.el7.ngx
 nginx_package: nginx-{{defaults.version}}-1.el7.ngx
 OS: centos
 OSRELEASE: 7
  1. Can I (and how can) use a variable version into the file molecule\default\verify.yml

Thanks for the support.

5
  • 2
    For the first question, you can directly use version variable while declaring nginx_package variable as follows: nginx_package: nginx-{{version}}-1.el7.ngx Commented Sep 13, 2021 at 17:38
  • 1
    Having a top-level defaults: dict will make it difficult to work with individual key-pairs inside it. Better to keep each role default variable separate, i.e. nginx_user: nginx, nginx_group: nginx, etc, Commented Sep 14, 2021 at 5:52
  • Hi Unsel, thanks for the reply. I tried to use nginx_package: nginx-{{version}}-1.el7.ngx but I receive this error: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: {'version': '1.19.2', 'nginx_package': 'nginx-\"{{ version }}\"-1.el7.ngx', 'version' is undefined Commented Sep 15, 2021 at 14:55
  • Hi Seshadri_c, thanks for your comment. I can't change the defaults structure. Commented Sep 15, 2021 at 15:04
  • 1
    Then you'll have to define other variable outside the defaults dict. Values within the dict structure can not be cross-referenced. nginx_package: nginx-{{defaults.version}}-1.el7.ngx will work when you put it outside defaults:. Commented Sep 15, 2021 at 15:18

1 Answer 1

0
  1. Use set_fact in main/task.yaml to set a variable using another variable. Doc: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/set_fact_module.html
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.