Skip to content
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

[Bug] output_template not used #69

Closed
rndmh3ro opened this issue Sep 19, 2024 · 17 comments · Fixed by #74
Closed

[Bug] output_template not used #69

rndmh3ro opened this issue Sep 19, 2024 · 17 comments · Fixed by #74

Comments

@rndmh3ro
Copy link
Collaborator

Regarding the use of templates.

I have created a configuration file with the following content:

---
output_template: |
  <!-- BEGIN_ANSIBLE_DOCS -->

  This is my role: {{ role }}

  <!-- Including files is also possible in relation to the role's directory with Jinja2's include directive -->
  {% include "defaults/main.yml" %}

  <!-- 'argument_specs' contains all the things in meta/argument_specs.yml -->
  {% for entrypoint, specs in argument_specs | items %}
  Task file name: {{ entrypoint }}.yml has {{ specs | length }} input variables!
  {% endfor %}
  <!-- END_ANSIBLE_DOCS -->

And use it with the following command:
python -m aar_doc.cli --output-file someoutput.md --config-file=/path/to/configuration/file/config.yaml /path/to/role markdown

Unfortunately, the generated file has the same content as without the configuration file.

Originally posted by @montaropdf in #66 (comment)

@rndmh3ro rndmh3ro changed the title output_template not used [Bug] output_template not used Sep 19, 2024
@rndmh3ro
Copy link
Collaborator Author

@montaropdf I just tested this locally and for me it works.

It creates a new file someoutput.md in the role's folder:

> head !$
head roles/os_hardening/someoutput.md
<!-- BEGIN_ANSIBLE_DOCS -->

This is my role: devsec.hardening.os_hardening

<!-- Including files is also possible in relation to the role's directory with Jinja2's include directive -->
---
os_desktop_enable: false
os_env_user_paths: [/usr/local/sbin, /usr/local/bin, /usr/sbin, /usr/bin, /sbin, /bin]
os_env_extra_user_paths: []
os_auth_pw_max_age: 60

Is there any other option in the config.yaml?

@montaropdf
Copy link

No this is the only option in the file.

@montaropdf
Copy link

As a side note, I encounter the following issue while testing again with the template option in the configuration:

[dev@2004L14EDCF Automated-Ansible-Role-Documentation-feature-meta_main]$ python -m aar_doc.cli --output-file=~/tmp/vast-doc2.md --config-file=~/repos/sftp-automation/aar-doc.config.yaml  ~/repos/sftp-automati
on/roles/sshd markdown
╭────────────────────────────────────────────────────────────────────────────────────── Traceback (most recent call last) ──────────────────────────────────────────────────────────────────────────────────────╮
│ /home/dev/tmp/Automated-Ansible-Role-Documentation-feature-meta_main/aar_doc/cli.py:46 in markdown                                                                                                            │
│                                                                                                                                                                                                               │
│    43 │                                                                                        ╭───────────────────────────────────────────────── locals ─────────────────────────────────────────────────╮   │
│    44 │   # FIXME: ctx should not be passed to these non-command()'s                           │ content = '<!-- BEGIN_ANSIBLE_DOCS -->\n# Ansible Role: sshd\n---\nblabla\n\n\n## Requirements\n--'+4965 │   │
│    45 │   content = render_content(ctx, "markdown.j2")                                         │     ctx = <click.core.Context object at 0x7f9bc0f7b110>                                                  │   │
│ ❱  46 │   write(ctx, content)                                                                  ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────╯   │
│    47                                                                                                                                                                                                         │
│    48                                                                                                                                                                                                         │
│    49 @app.callback()                                                                                                                                                                                         │
│                                                                                                                                                                                                               │
│ /home/dev/tmp/Automated-Ansible-Role-Documentation-feature-meta_main/aar_doc/cli.py:125 in write                                                                                                              │
│                                                                                                                                                                                                               │
│   122 │   if not output.exists():                                                              ╭───────────────────────────────────────────────── locals ─────────────────────────────────────────────────╮   │
│   123 │   │   ctx.obj["config"]["output_mode"] = OutputMode.replace                            │ content = '<!-- BEGIN_ANSIBLE_DOCS -->\n# Ansible Role: sshd\n---\nblabla\n\n\n## Requirements\n--'+4965 │   │
│   124 │                                                                                        │     ctx = <click.core.Context object at 0x7f9bc0f7b110>                                                  │   │
│ ❱ 125 │   with open(output, "a+") as f:                                                        │  output = PosixPath('~/tmp/vast-doc2.md')                                                                │   │
│   126 │   │   if ctx.obj["config"]["output_mode"] == OutputMode.inject:                        ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────╯   │
│   127 │   │   │   f.seek(0)                                                                                                                                                                                   │
│   128 │   │   │   lines = f.readlines()                                                                                                                                                                       │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
FileNotFoundError: [Errno 2] No such file or directory: '~/tmp/vast-doc2.md'

However, with --output-file=vast-doc2.md, I have no error and the file is generated inside the role directory. But, still with the default template for the markdown command.

@rndmh3ro
Copy link
Collaborator Author

Does the folder ~/tmp/ exist and you have access to it?

Can you do a ls -lsah ~/ | grep tmp?

@montaropdf
Copy link

I have performed further tests, the options --output-file and --config-file does not seems to expand the ~ character.

However, with absolute path, I got the below error.

aar_doc-error.txt

@rndmh3ro
Copy link
Collaborator Author

I have performed further tests, the options --output-file and --config-file does not seems to expand the ~ character.

This works for me.

However, with absolute path, I got the below error.

This seems related somehow since its not finding the template.

Can you answer these questions please?

  • What kind of OS are you using?
  • What Python version are you using?

@montaropdf
Copy link

I am using a Fedora 39 distribution on WSL.
My version of python is Python 3.12.4.

I don't run the tool inside a virtual environment I don't know if it could have an impact.

@rndmh3ro
Copy link
Collaborator Author

I am using a Fedora 39 distribution on WSL.

WSL could be the problem here. Do you have the possibility to test it on a non-WSL system?

In the meantime we'll try to reproduce it on WSL (I don't use Windows..)

@montaropdf
Copy link

Same problem on a RedHat Enterprise Linux 9 machine with Python 3.9.18.

@rndmh3ro
Copy link
Collaborator Author

Okay, I found the reason: pathlib.Path does not expand ~. Will push a fix.

@montaropdf
Copy link

With your fix, the ~ seems to be expanded, now. However, I still suffer from the error reported in this comment:

#69 (comment)

@rndmh3ro
Copy link
Collaborator Author

Does your role have a defaults/main.yml-file?

@montaropdf
Copy link

Not all of them.

@rndmh3ro
Copy link
Collaborator Author

In your output template you got this:

<!-- Including files is also possible in relation to the role's directory with Jinja2's include directive -->
  {% include "defaults/main.yml" %}

This part tries to include the defaults file. If it does not exist for a role, this will fail. Does this fail for roles where the defaults file exists?

@schurzi schurzi reopened this Sep 30, 2024
@montaropdf
Copy link

It fails when defaults/main.yml is present too. Could it be due to the fact that I run the tool from its directory and not from the base directory of my playbooks?

@rndmh3ro
Copy link
Collaborator Author

rndmh3ro commented Oct 1, 2024

Could it be due to the fact that I run the tool from its directory and not from the base directory of my playbooks?

No, this works for me.

I just tested it, even with an empty defaults/main.yml, it works. So I see three things here:

  • Check if your file is called, defaults/main.yml and not defaults/main.yaml
  • Provide a minimal reproduction role where its not working for you, so I can test it
  • Don't include the defauits :)

@montaropdf
Copy link

When the file is called defaults/main.yml the tool does not report an error and generate the expected output.

So in the end for this issue it was a stupid CKI problem.

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

Successfully merging a pull request may close this issue.

3 participants