molecule icon indicating copy to clipboard operation
molecule copied to clipboard

Shell variable expansion

Open zyphermonkey opened this issue 5 years ago • 6 comments

Issue Type

  • Bug report

Molecule and Ansible details

$ ansible --version && molecule --version
ansible 2.9.11
  config file = /home/vagrant/ansible.cfg
  configured module search path = ['/home/vagrant/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/vagrant/.local/lib/python3.6/site-packages/ansible
  executable location = /home/vagrant/.local/bin/ansible
  python version = 3.6.9 (default, Jul 17 2020, 12:50:27) [GCC 8.4.0]
molecule 3.0.6
   ansible==2.9.11 python==3.6

Molecule installation method (one of):

  • pip

Ansible installation method (one of):

  • pip

Detail any linters or test runners used:

|
  set -e
  yamllint -f colored .
  ansible-lint --force-color

Desired Behavior

Using shell expansion + fails inside of molecule.yml instead of correctly expanding/replacing

Trying to only include formatting characters if a variable is defined.

platforms:
  - name: instance${CI_JOB_ID:+-}${CI_JOB_ID}

According to the docs this should produce an instance name of either instance or instance-<CI_JOB_ID>.

Further shell expansion information:

${parameter:+word} parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.

Actual Behaviour

Please give some details of what is actually happening. Include a minimum complete verifiable example with output of running molecule --debug.

ERROR: parsing config file '/ansible/roles/ca-sso-policyserver/molecule/default/molecule.yml'.

Invalid placeholder in string: line 11, col 19
---
dependency:
  name: galaxy
driver:
  name: docker
lint: |
  set -e
  yamllint -f colored .
  ansible-lint --force-color
platforms:
  - name: instance${CI_JOB_ID:+-}${CI_JOB_ID}

zyphermonkey avatar Sep 18 '20 12:09 zyphermonkey

Hello,

I believe this is due to the fact that a fallback must be provied for the value if the variable is missing

- name: instance${CI_JOB_ID:-HELLO}

Produces instanceHELLO without passing CI_JOB_ID.

When passed CI_JOB_ID=WORLD, the resulting name is correctly instanceWORLD

fourstepper avatar Oct 20 '20 06:10 fourstepper

@zyphermonkey have you figured this out?

fourstepper avatar Nov 09 '20 16:11 fourstepper

I think you missing the + it acts as a replacement. So if the first variable has a value then use whatever is after the +. I don't have any issue with the more commonly use -.

zyphermonkey avatar Nov 21 '20 13:11 zyphermonkey

It isn't a full BASH compatible setup as @fourstepper correctly suggested. Here is the PR where I implemented the BASH style expansion, the implementation is rather simple and just checks for :- Might be worth noting in the docs :)

https://github.com/ansible-community/molecule/pull/2223

wilmardo avatar Nov 25 '20 16:11 wilmardo

@zyphermonkey can this be closed?

fourstepper avatar Dec 26 '20 11:12 fourstepper

@zyphermonkey can this be closed?

For sure, but I do agree the limited BASH functionality should be noted in the docs when possible or to make an update to include compatibility for :+.

I'll let you decide if you want to keep this open as a task to add :+ or close it if there are no plans to do so.

zyphermonkey avatar Dec 27 '20 13:12 zyphermonkey