ansible/molecule

Following getting started fails to complete

brianbruggeman opened this issue · 7 comments

Issue Type

  • Bug report

Molecule and Ansible details

ansible --version && molecule --version

ansible [core 2.13.4]
  config file = <redacted>/repos/mine/molecule-test/ansible.cfg
  configured module search path = ['<redacted>/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = <redacted>/.virtualenvs/mol-test/lib/python3.10/site-packages/ansible
  ansible collection location = <redacted>/.ansible/collections:/usr/share/ansible/collections
  executable location = <redacted>/.virtualenvs/mol-test/bin/ansible
  python version = 3.10.6 (main, Aug 30 2022, 05:12:36) [Clang 13.1.6 (clang-1316.0.21.2.5)]
  jinja version = 3.1.2
  libyaml = True
molecule 4.0.1 using python 3.10 
    ansible:2.13.4
    delegated:4.0.1 from molecule
    docker:2.1.0 from molecule_docker requiring collections: community.docker>=3.0.2 ansible.posix>=1.4.0

Molecule installation method: pip

Ansible installation method: pip

Desired Behavior

I am following the documentation provided for getting started with molecule.

First step is:

molecule init role acme.my_new_role --driver-name docker

My expectation is that this behavior is (more or less) identical to molecule 3.4, which had a fantastic DX. It should produce a skeleton role that I can then update.

Actual Behaviour

Immediately, there's an attempt to ssh. Of course the folder is created, but the failure condition provided below doesn't really elucidate what's happening.

molecule init role acme.my_new_role --driver-name docker

localhost | UNREACHABLE!: Failed to connect to the host via ssh: ssh: connect to host localhost port 22: Connection refused
WARNING  Retrying execution failure 4 of: ansible localhost -o -m lineinfile -a path=my_new_role/meta/main.yml line="  namespace: acme" insertafter="  author: your name"
Traceback (most recent call last):
  File "<redacted>/.virtualenvs/mol-test/bin/molecule", line 8, in <module>
    sys.exit(main())
  File "<redacted>/.virtualenvs/mol-test/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "<redacted>/.virtualenvs/mol-test/lib/python3.10/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "<redacted>/.virtualenvs/mol-test/lib/python3.10/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "<redacted>/.virtualenvs/mol-test/lib/python3.10/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "<redacted>/.virtualenvs/mol-test/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "<redacted>/.virtualenvs/mol-test/lib/python3.10/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "<redacted>/.virtualenvs/mol-test/lib/python3.10/site-packages/click/decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "<redacted>/.virtualenvs/mol-test/lib/python3.10/site-packages/molecule/command/init/role.py", line 180, in role
    r.execute()
  File "<redacted>/.virtualenvs/mol-test/lib/python3.10/site-packages/molecule/command/init/role.py", line 104, in execute
    util.run_command(cmd, check=True)
  File "<redacted>/.virtualenvs/mol-test/lib/python3.10/site-packages/molecule/util.py", line 157, in run_command
    raise CalledProcessError(
subprocess.CalledProcessError: Command '['ansible', 'localhost', '-o', '-m', 'lineinfile', '-a', 'path=my_new_role/meta/main.yml line="  namespace: acme" insertafter="  author: your name"']' returned non-zero exit status 4.

As an aside, I really wish ansible-galaxy was opt-in.

hi @brianbruggeman

I cannot reproduce this issue. What else did you do?

(v-3-10-4) x@build-ubuntu-01:~/src/tmp$ molecule init role acme.my_new_role --driver-name docker
INFO     Initializing new role my_new_role...
No config file found; using defaults
- Role my_new_role was created successfully
[WARNING]: No inventory was parsed, only implicit localhost is available
localhost | CHANGED => {"backup": "","changed": true,"msg": "line added"}
INFO     Initialized role in /mnt/src/tmp/my_new_role successfully.
(v-3-10-4) x@build-ubuntu-01:~/src/tmp$ ansible --version && molecule --version
ansible [core 2.13.4]
  config file = None
  configured module search path = ['/home/x/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/x/.pyenv/versions/3.10.4/envs/v-3-10-4/lib/python3.10/site-packages/ansible
  ansible collection location = /home/x/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/x/.pyenv/versions/v-3-10-4/bin/ansible
  python version = 3.10.4 (main, Oct  8 2022, 11:04:49) [GCC 9.3.0]
  jinja version = 3.1.2
  libyaml = True
molecule 4.0.1 using python 3.10
    ansible:2.13.4
    delegated:4.0.1 from molecule
    docker:2.1.0 from molecule_docker requiring collections: community.docker>=3.0.2 ansible.posix>=1.4.0

Thanks for the check.

It's very likely this is environmental; but my environment has been around (and working with molecule v3.4) for a few years. But that also tells me that the documentation probably isn't expressing all of the expected requirements. I'm on an apple silicon mac; does this matter? I'd really like to use Rancher Desktop; does that matter?

I am not sure why molecule is ssh-ing during init; I thought that this command would just build a directory structure. I can probably dig through the code, but is there any guidance someone can provide to figure out what init is sshing to? At the moment, this is a blocker for using molecule.

@brianbruggeman please execute the following command at the same place like your molecule command

$ ansible localhost -m command -a id -vvv | grep -i establish

this is a example for a local connection
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER:

and this for a ssh connection
ESTABLISH SSH CONNECTION FOR USER:

do you have the second, please check which inventory is in action.

Hope this helps.

Thank you.

ansible localhost -m command -a id -vvv | grep -i establish
<localhost> ESTABLISH SSH CONNECTION FOR USER: None

Looks like I'm using local. I'll dig further one I have some time, but at this point, I'm going to punt.

For the moment, I'm copy/pasta a working example and not using molecule's initialization. This appears to get me moving forward.

@brianbruggeman did you end up getting this fixed? I'm having the same issue on two separate environments (Arch and Ubuntu, both being a fresh install). I've been testing this with this example to no avail.

No. I don't have a resolution. But I suspect it is somehow environmental.

To define localhost and use local connection instead of ssh, define hosts as follows:

[local]
localhost ansible_connection=local

This way we define local group with localhost host with local connection.

https://stackoverflow.com/a/45197210/12453442

This works for me.

https://stackoverflow.com/a/37186096/12453442

or this is more detailed solution.