Skip to content

Add example of ansible provisioning with playbook#2676

Closed
afbjorklund wants to merge 1 commit into
lima-vm:masterfrom
afbjorklund:ansible-example
Closed

Add example of ansible provisioning with playbook#2676
afbjorklund wants to merge 1 commit into
lima-vm:masterfrom
afbjorklund:ansible-example

Conversation

@afbjorklund

@afbjorklund afbjorklund commented Oct 2, 2024

Copy link
Copy Markdown
Member

Example for:

This has the same problem with relative paths, as was fixed for the scripts:

INFO[0022] Waiting for ansible playbook "./ansible/playbook.yaml" 
ERROR! the playbook: ./ansible/playbook.yaml could not be found

commit 3b33549

Currently it uses the working directory, so it only works from the git repo top.

$ limactl start ./templates/ansible.yaml

With the awesome Unicode Unicow:

INFO[0067] Waiting for ansible playbook "./templates/ansible/playbook.yaml" 
╭──────────────────────╮
⟨ PLAY [My first play] ⟩
╰──────────────────────╯
        ╲   ^__^
         ╲  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

╭────────────────────────╮
⟨ TASK [Gathering Facts] ⟩
╰────────────────────────╯
        ╲   ^__^
         ╲  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

ok: [ansible]
╭───────────────────────────────────────────────╮
⟨ TASK [hostname : write hostname using jinja2] ⟩
╰───────────────────────────────────────────────╯
        ╲   ^__^
         ╲  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

ok: [ansible]
╭──────────────────────╮
⟨ TASK [Ping my hosts] ⟩
╰──────────────────────╯
        ╲   ^__^
         ╲  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

ok: [ansible]
╭──────────────────────╮
⟨ TASK [Print message] ⟩
╰──────────────────────╯
        ╲   ^__^
         ╲  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

ok: [ansible] => {
    "msg": "Hello world"
}
╭────────────╮
⟨ PLAY RECAP ⟩
╰────────────╯
        ╲   ^__^
         ╲  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

ansible                    : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

@afbjorklund
afbjorklund requested a review from jandubois October 2, 2024 16:50
@jandubois

Copy link
Copy Markdown
Member

Is there a way to reference environment variables from inside the playbook?

I realized that tests in #2570 don't verify that the $PARAM_FOO variables are visible to ansible provisioning (and probably they aren't). So it would be good if we could incorporate a test for that in the example?

@afbjorklund

afbjorklund commented Oct 2, 2024

Copy link
Copy Markdown
Member Author

Is there a way to reference environment variables from inside the playbook?

I think you can do something like {{ lookup('env','ENV_VAR') }}

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/env_lookup.html

Then there is the ansible_env, but that it set rather than get

@afbjorklund

afbjorklund commented Oct 2, 2024

Copy link
Copy Markdown
Member Author

I will add a separate test for params, they should be propagated but they fail to validate:

field param key "ANSIBLE" is not used in any provision, probe, copyToHost, or portForward

We will just add them to the environment of the ansible-playbook (local) execution.

@afbjorklund
afbjorklund force-pushed the ansible-example branch 2 times, most recently from 100be8b to 587ff3e Compare October 3, 2024 09:17
Currently the playbook is relative to the current workdir,
it should probably be relative to the lima.yaml instead.

The YAML syntax and the directory layout has been modified
slightly from the original, to work with Lima's setup.

Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we want to put non-lima yamls to the template store.
Maybe we should have a examples/ directory?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we want to put non-lima yamls to the template store.

We don't because they would be included in limactl create --list-templates, which would be confusing.

@afbjorklund afbjorklund Apr 18, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs to be next to the template, so that it can be referenced.

provision:
- mode: ansible
  playbook: ./templates/ansible/playbook.yaml

It could probably use the new feature for scripts:

@afbjorklund

afbjorklund commented Apr 18, 2025

Copy link
Copy Markdown
Member Author

Should be rewritten with the new feature to assemble templates.

i.e. provision.playbook should be converted to use locator as well

provision:
- mode: ansible
  file:
    url: ./templates/ansible/playbook.yaml

Currently it is being read into script, but should also be for playbook.

NOTE: The playbook is recursive, so needs to be parsed to be embedded...

templates/ansible
├── playbook.yaml
├── README.md
└── roles
    └── hostname
        ├── tasks
        │   └── main.yaml
        └── templates
            └── test.j2

4 directories, 4 files

Unfortunately Ansible does not provide a good way to use a single yaml file.

I have no idea if embedding works for scripts that include functions from files...

@jandubois

Copy link
Copy Markdown
Member

Should be rewritten with the new feature to assemble templates.

Yes, it should. Once created, instances should not rely on any external files.

Unfortunately there don't seem to be any tools to flatten playbooks; we would have to implement it ourselves. I'll create an issue for it, but I won't get around to it for quite a while.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants