Skip to content

Add example of ansible provisioning with playbook #2676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

afbjorklund
Copy link
Member

@afbjorklund afbjorklund commented Oct 2, 2024

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
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
Copy link
Member Author

afbjorklund commented Oct 2, 2024

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
Copy link
Member Author

afbjorklund commented Oct 2, 2024

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
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
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.

Copy link
Member Author

@afbjorklund afbjorklund Apr 18, 2025

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
Copy link
Member Author

afbjorklund commented Apr 18, 2025

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
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.

None yet

3 participants