Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

master branch: "formal argument cannot be a constant" #91

Closed
konstruktoid opened this issue Dec 12, 2020 · 6 comments · Fixed by #94
Closed

master branch: "formal argument cannot be a constant" #91

konstruktoid opened this issue Dec 12, 2020 · 6 comments · Fixed by #94

Comments

@konstruktoid
Copy link
Contributor

When using the master branch, it fails with "formal argument cannot be a constant" when creating a scenario.

$ git log -n1
commit 0c1aed1fb550a49cf584104a005aab1d67afc014 (HEAD -> master, upstream/master, origin/master, origin/HEAD)
[...]
$ molecule create -s centos
DEBUG    Validating schema /Users/xxxxx/Git/Github/ansible-role-hardening/molecule/centos/molecule.yml.
INFO     centos scenario test matrix: dependency, create, prepare
INFO     Running centos > dependency
WARNING  Skipping, dependency is disabled.
WARNING  Skipping, dependency is disabled.
INFO     Running centos > create
DEBUG    Using selector: KqueueSelector

PLAY [Create] ******************************************************************

TASK [Create molecule instance(s)] *********************************************
Sunday 13 December 2020  00:28:08 +0100 (0:00:00.022)       0:00:00.022 *******
failed: [localhost] (item=centos) => {"ansible_loop_var": "item", "changed": false, "item": {"box": "bento/centos-8", "instance_raw_config_args": ["vbguest.installer_options = { allow_kernel_upgrade: true }"], "memory": 1024, "name": "centos", "provider_options": {"vm.boot_timeout": 600}}, "msg": "Failed to validate generated Vagrantfile: b'Vagrant failed to initialize at a very early stage:\\n\\nThere is a syntax error in the following Vagrantfile. The syntax error\\nmessage is reproduced below for convenience:\\n\\n/Users/xxxxx/.cache/molecule/ansible-role-hardening/centos/Vagrantfile:45: formal argument cannot be a constant\\n...  c.vm.provider \"VBox\" do |VBox, override|\\n...                           ^~~~\\n'"}
failed: [localhost] (item=centos-stream) => {"ansible_loop_var": "item", "changed": false, "item": {"box": "centos-stream/20201019", "box_download_checksum": "88d388ff2170430f032149323ffbba20309b8e0da924e1ad6ec5f024936c2648", "box_download_checksum_type": "sha256", "box_url": "https://cloud.centos.org/centos/8-stream/x86_64/images/CentOS-Stream-Vagrant-8-20201019.1.x86_64.vagrant-virtualbox.box", "instance_raw_config_args": ["vbguest.installer_options = { allow_kernel_upgrade: true }"], "memory": 1024, "name": "centos-stream", "provider_options": {"vm.boot_timeout": 600}}, "msg": "Failed to validate generated Vagrantfile: b'Vagrant failed to initialize at a very early stage:\\n\\nThere is a syntax error in the following Vagrantfile. The syntax error\\nmessage is reproduced below for convenience:\\n\\n/Users/xxxxx/.cache/molecule/ansible-role-hardening/centos/Vagrantfile:45: formal argument cannot be a constant\\n...  c.vm.provider \"VBox\" do |VBox, override|\\n...                           ^~~~\\n'"}

PLAY RECAP *********************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Sunday 13 December 2020  00:28:13 +0100 (0:00:04.630)       0:00:04.653 *******
===============================================================================
Create molecule instance(s) --------------------------------------------- 4.63s
CRITICAL Ansible return code was 2, command was: ansible-playbook --inventory /Users/xxxxx/.cache/molecule/ansible-role-hardening/centos/inventory --skip-tags molecule-notest,notest /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/molecule_vagrant/playbooks/create.yml
$
# Molecule managed

Vagrant.configure('2') do |config|
  if Vagrant.has_plugin?('vagrant-cachier')
    config.cache.scope = 'machine'
  end

  config.vm.define "centos-stream" do |c|
    ##
    # Box definition
    ##
    c.vm.box = "centos-stream/20201019"
    
    c.vm.box_url = "https://cloud.centos.org/centos/8-stream/x86_64/images/CentOS-Stream-Vagrant-8-20201019.1.x86_64.vagrant-virtualbox.box"

    ##
    # Config options
    ##
    
    c.vm.synced_folder ".", "/vagrant", disabled: true
    

    
    
    
    c.ssh.insert_key = true
    

    c.vm.hostname = "centos-stream"

    ##
    # Network
    ##
    

    ##
    # instance_raw_config_args
    ##
    c.vbguest.installer_options = { allow_kernel_upgrade: true }
    

    ##
    # Provider
    ##
    c.vm.provider "VBox" do |VBox, override|
      
      VBox.memory = 1024
      VBox.cpus = 2
      

      
      VBox.vm.boot_timeout = 600
      

      

      

      
      
    end
  end
end

Related: #87 and #90

konstruktoid added a commit to konstruktoid/ansible-role-hardening that referenced this issue Dec 13, 2020
Signed-off-by: Thomas Sjögren <konstruktoid@users.noreply.github.com>
@apatard
Copy link
Member

apatard commented Dec 14, 2020

hm. Are you sure about the provider name you're using ? I don't remember reading that there was 'VBox' as provider for VirtualBox. I've always seen 'virtualbox' as provider name.

If 'VBox' comes from the line:

# Can be any supported provider (VBox, Parallels, libvirt, etc)

of the README.rst file, it's not how this line was meant to be read by its author. It's a list of providers but not the provider name. I guess it's working in your case only due to the fact that vagrant is falling back to the 'virtualbox' provider when the configured provider is not available.

As regards the error message, I'm going to modify the Vagrantfile template avoid that, in case some day a vagrant provider with a first letter with upper case. I guess it's not going to happen soon but better be safe than sorry.

@konstruktoid
Copy link
Contributor Author

I actually read # Can be any supported provider (VBox, Parallels, libvirt, etc) as vbox was a valid provider without giving it much thought since it worked. Using virtualbox doesn't.

$ git diff
diff --git a/molecule/centos/molecule.yml b/molecule/centos/molecule.yml
index 9f21233..2c94ee8 100644
--- a/molecule/centos/molecule.yml
+++ b/molecule/centos/molecule.yml
@@ -4,7 +4,7 @@ dependency:
 driver:
   name: vagrant
   provider:
-    name: vbox
+    name: virtualbox
 provisioner:
   name: ansible
   config_options:
$ pip3 list | grep molecule-vagrant
molecule-vagrant           0.6.2.dev1
$ molecule create -s centos
[...]
TASK [Create molecule instance(s)] *********************************************
Monday 14 December 2020  12:07:43 +0100 (0:00:00.054)       0:00:00.055 *******
failed: [localhost] (item=centos) => {"ansible_loop_var": "item", "changed": false, "item": {"box": "bento/centos-8", "instance_raw_config_args": ["vbguest.installer_options = { allow_kernel_upgrade: true }"], "memory": 1024, "name": "centos", "provider_options": {"vm.boot_timeout": 600}}, "msg": "Failed to validate generated Vagrantfile: b\"There are errors in the configuration of this machine. Please fix\\nthe following errors and try again:\\n\\nVirtualBox Provider:\\n* The following settings shouldn't exist: vm\\n\\n\""}
failed: [localhost] (item=centos-stream) => {"ansible_loop_var": "item", "changed": false, "item": {"box": "centos-stream/20201019", "box_download_checksum": "96d388ff2170430f032149323ffbba20309b8e0da924e1ad6ec5f024936c2648", "box_download_checksum_type": "sha256", "box_url": "https://cloud.centos.org/centos/8-stream/x86_64/images/CentOS-Stream-Vagrant-8-20201019.1.x86_64.vagrant-virtualbox.box", "instance_raw_config_args": ["vbguest.installer_options = { allow_kernel_upgrade: true }"], "memory": 1024, "name": "centos-stream", "provider_options": {"vm.boot_timeout": 600}}, "msg": "Failed to validate generated Vagrantfile: b\"There are errors in the configuration of this machine. Please fix\\nthe following errors and try again:\\n\\nVirtualBox Provider:\\n* The following settings shouldn't exist: vm\\n\\n\""}
$ more molecule/centos/molecule.yml
---
dependency:
  enabled: false
driver:
  name: vagrant
  provider:
    name: virtualbox
provisioner:
  name: ansible
  config_options:
    defaults:
      callback_whitelist: profile_tasks
  playbooks:
    converge: ../default/converge.yml
    verify: ../default/verify.yml
  log: true
  inventory:
    host_vars:
      centos:
        sshd_admin_net: "0.0.0.0/0"
        sshd_allow_groups: "vagrant sudo"
      centos-stream:
        sshd_admin_net: "0.0.0.0/0"
        sshd_allow_groups: "vagrant sudo"
platforms:
  - name: centos
    provider_options:
      vm.boot_timeout: 600
    instance_raw_config_args:
      - 'vbguest.installer_options = { allow_kernel_upgrade: true }'
    memory: 1024
    box: bento/centos-8
  - name: centos-stream
    provider_options:
      vm.boot_timeout: 600
    instance_raw_config_args:
      - 'vbguest.installer_options = { allow_kernel_upgrade: true }'
    memory: 1024
    box: "centos-stream/20201019"
    box_url: "https://cloud.centos.org/centos/8-stream/x86_64/images/CentOS-Stream-Vagrant-8-20201019.1.x86_64.vagrant-virtualbox.box"
    box_download_checksum: "96d388ff2170430f032149323ffbba20309b8e0da924e1ad6ec5f024936c2648"
    box_download_checksum_type: "sha256"
[...]

@apatard
Copy link
Member

apatard commented Dec 14, 2020

platforms:
  - name: centos
    provider_options:
      vm.boot_timeout: 600

Did you try something like:

platforms:
  - name: centos
    config_options:
      vm.boot_timeout: 600

? (provider_options vs config_options)

@konstruktoid
Copy link
Contributor Author

'config_options': {'vm.boot_timeout': 600}, 'instance_raw_config_args': ['vbguest.installer_options = { allow_kernel_upgrade: true }'], 'memory': 1024, 'name': 'centos-stream'}, 'ansible_loop_var': 'item'}

worked.

May I suggest that I write a PR that extends the README with a virtualbox example and some information about providers, so slow people like don't waste more of our time?

But why wasn't VBox caught earlier if it's not a valid provider, and how could it actually work if it was caught as a valid provider?

@apatard
Copy link
Member

apatard commented Dec 14, 2020

'config_options': {'vm.boot_timeout': 600}, 'instance_raw_config_args': ['vbguest.installer_options = { allow_kernel_upgrade: true }'], 'memory': 1024, 'name': 'centos-stream'}, 'ansible_loop_var': 'item'}

worked.

great !

May I suggest that I write a PR that extends the README with a virtualbox example and some information about providers, so slow people like don't waste more of our time?

I'm not sure what you need ? You can send a PR and I'll review but no promise about the merge. It would be nice to avoid adding a specific example for each provider.
About the provider_options vs config_options, the doc is mentioning the config.ssh namespace and not config.vm, so may something is missing. The config_options is for all config.* namespaces. btw, you could have used config_options to set the config.vm.box_download_checksum* settings too instead of patching.
And for 'VBox' vs 'virtualbox', I can only guess that the doc primary author is assuming that a user of molecule-vagrant is already familiar with vagrant and would use 'virtualbox' naturally.

But why wasn't VBox caught earlier if it's not a valid provider, and how could it actually work if it was caught as a valid provider?

I don't know. I can only guess that people used 'virtualbox' as they were already using vagrant with vbox or that vagrant is defaulting to virtualbox provider if the one specified is not available.

And for the original error of this bug, it's due to my change removing the vagrant.yml file. I've used the provider name as variable in the Vagrantfile and the ruby language consider that every symbol starting with a uppercase, it's a constant but here, I want a variable. The "formal argument cannot be a constant" means that I've tried to use a constant as variable and it was not detected since all providers are in lower case.

@konstruktoid
Copy link
Contributor Author

Regarding the README it was just about a small explanation about the providers without any short versions of the names etc.

And as you said config.vm.provider "virtualbox" do |vb| is the correct way in a Vagrantfile, but I just followed the documentation and used VBox without giving it much thought. And it worked (tm).

apatard added a commit to apatard/molecule-vagrant that referenced this issue Dec 14, 2020
…ider.name as variable

When using {{ instance.provider }} as variable in the generated
Vagrantfile, it has to respect ruby syntax, so the first letter
has to be a lower case.

Until recently, either the user was using the provider name as the one
specified in the Vagrant documentation or it was specifying the
wrong name (like VirtualBox or VBox) and things were "just" working,
since Vagrant will default to virtualbox in case it didn't find the
configured provider.
In the vagrant.yml removal patch, I've used the provider name as variable
and with the provider set to 'VirtualBox', this will now produced the
following ruby error:

Vagrantfile:45: formal argument cannot be a constant

So, even if it may be considered as a configuration error, it's
better to ensure on our side to not produce an invalid file.
The change is limited to this part of the Vagrantfile, as I guess
that user will respect the casing for the other configuration options.

Fix: ansible-community#91
Signed-off-by: Arnaud Patard <apatard@hupstream.com>
This was referenced Jun 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants