ansible / ansible-lint-action

❗️Replaced by https://github.com/marketplace/actions/run-ansible-lint
https://github.com/marketplace/actions/run-ansible-lint
MIT License
254 stars 132 forks source link

how to resolve FQCN error? #99

Closed officel closed 2 years ago

officel commented 2 years ago

It works fine locally, but FQCN seems to error in this actions.

2022-03-31T15:17:15.5495506Z INFO     Discovered files to lint using: git ls-files --cached --others --exclude-standard -z
2022-03-31T15:17:15.5534284Z INFO     Excluded removed files using: git ls-files --deleted -z
2022-03-31T15:17:16.4163926Z INFO     Executing syntax check on ansible/playbooks_module_examples/timezone.yml (0.82s)
2022-03-31T15:17:16.4531798Z INFO     Executing syntax check on ansible/playbooks/test.yml (0.86s)
2022-03-31T15:17:16.4554344Z WARNING  Listing 1 violation(s) that are fatal
2022-03-31T15:17:16.4585041Z syntax-check: couldn't resolve module/action 'community.general.timezone'. This often indicates a misspelling, missing collection, or incorrect module path.
2022-03-31T15:17:16.4585757Z ansible/playbooks_module_examples/timezone.yml:9:7 [WARNING]: No inventory was parsed, only implicit localhost is available
2022-03-31T15:17:16.4586146Z [WARNING]: provided hosts list is empty, only localhost is available. Note that
2022-03-31T15:17:16.4586967Z the implicit localhost does not match 'all'
2022-03-31T15:17:16.4587650Z ERROR! couldn't resolve module/action 'community.general.timezone'. This often indicates a misspelling, missing collection, or incorrect module path.
2022-03-31T15:17:16.4587997Z 
2022-03-31T15:17:16.4588407Z The error appears to be in '/github/workspace/ansible/playbooks_module_examples/timezone.yml': line 9, column 7, but may
2022-03-31T15:17:16.4588856Z be elsewhere in the file depending on the exact syntax problem.
2022-03-31T15:17:16.4589072Z 
2022-03-31T15:17:16.4589197Z The offending line appears to be:
2022-03-31T15:17:16.4589371Z 
2022-03-31T15:17:16.4589459Z   tasks:
2022-03-31T15:17:16.4590041Z     - name: set timezone {{ tz }}
2022-03-31T15:17:16.4590491Z       ^ here
2022-03-31T15:17:16.4590763Z We could be wrong, but this one looks like it might be an issue with
2022-03-31T15:17:16.4591088Z missing quotes. Always quote template expression brackets when they
2022-03-31T15:17:16.4591343Z start a value. For instance:
2022-03-31T15:17:16.4591481Z 
2022-03-31T15:17:16.4591556Z     with_items:
2022-03-31T15:17:16.4591799Z       - {{ foo }}
2022-03-31T15:17:16.4591899Z 
2022-03-31T15:17:16.4591985Z Should be written as:
2022-03-31T15:17:16.4592111Z 
2022-03-31T15:17:16.4592187Z     with_items:
2022-03-31T15:17:16.4592415Z       - "{{ foo }}"
2022-03-31T15:17:16.4592530Z 
2022-03-31T15:17:16.4592535Z 
2022-03-31T15:17:16.4632826Z ##[error]syntax-check couldn't resolve module/action 'community.general.timezone'. This often indicates a misspelling, missing collection, or incorrect module path.
2022-03-31T15:17:16.4652505Z 
2022-03-31T15:17:16.4682597Z Finished with 1 failure(s), 0 warning(s) on 11 files.

playbooks

$ cat ansible/playbooks_module_examples/timezone.yml
---
- name: timezone
  hosts: all
  gather_facts: true
  vars:
    tz: Asia/Tokyo

  tasks:
    - name: set timezone {{ tz }}
      community.general.timezone:
        name: "{{ tz }}"
      failed_when: false

local run(it's ok)

my-examples-ansible $ ansible-lint -vvv ansible
<ommit>
INFO     Discovered files to lint using: git ls-files --cached --others --exclude-standard -z
INFO     Excluded removed files using: git ls-files --deleted -z
INFO     Executing syntax check on ansible/playbooks/test.yml (4.96s)
INFO     Executing syntax check on ansible/playbooks_module_examples/timezone.yml (5.14s)
DEBUG    Examining ansible/playbooks_module_examples/timezone.yml of type playbook
DEBUG    Examining ansible/playbooks/test.yml of type playbook
DEBUG    Examining ansible of type role
DEBUG    Examining ansible/.ansible-lint of type yaml
DEBUG    Examining ansible/inventories/docker.yml of type yaml

Could you please tell me how to do this? 🙇

ssbarnea commented 2 years ago

See https://docs.ansible.com/ansible/latest/galaxy/user_guide.html#install-multiple-collections-with-a-requirements-file

cidrblock commented 2 years ago

I was able to get this working: (this doesn't cover the action part)

$ ansible-galaxy collection install community.general
---
- name: timezone
  hosts: all
  gather_facts: true
  vars:
    tz: Asia/Tokyo

  tasks:
    - name: "Set the timezone. {{ tz }}"
      community.general.timezone:
        name: "{{ tz }}"
      failed_when: false
officel commented 2 years ago

@ssbarnea @cidrblock thank you! I could complete.

my repo(ommit)

$ tree
.
|-- ansible
|   |-- .ansible-lint
|   |-- requirements.yml
|   `-- roles

my wf

    steps:
      - uses: actions/checkout@master

      - name: copy .ansible-lint
        shell: bash
        run: |
          cp ansible/.ansible-lint .
          cp ansible/requirements.yml .

      - name: Lint Ansible Playbook
        uses: ansible/ansible-lint-action@v6
        # uses: ./.github/actions/ansible-lint
        with:
          args: ansible
officel commented 2 years ago

wooops

$ cat ansible/requirements.yml
---
collections:
  - community.general

$ cat ansible/.ansible-lint
# see https://ansible-lint.readthedocs.io/en/latest/configuring/

# List of additional kind:pattern to be added at the top of the default
# match list, first match determines the file kind.
kinds:
  - playbook: "**/playbook*/*.{yml,yaml}"
  # - galaxy: "**/folder/galaxy.yml"
  # - tasks: "**/tasks/*.yml"
  # - vars: "**/vars/*.yml"
  # - meta: "**/meta/main.yml"
  - yaml: "**/*.yaml-too"
hemberger commented 1 year ago

I'm seeing this issue with ansible-lint 6.10.0 even after adding the community.general collection to my requirements.yml file.

WARNING  Unable to load module community.general.timezone at roles/system_config/tasks/main.yml:46 for options validation
WARNING  Unable to resolve FQCN for module community.general.timezone

Here's an example job:

https://github.com/smrealms/seed-server/actions/runs/3783650542/jobs/6432363437

And here's what my requirements.yml looks like:

---
collections:

  - community.general

roles:

  - src: geerlingguy.docker
    version: 5.2.1

  - src: hifis.unattended_upgrades
    version: v1.12.2

Am I doing something wrong here? Thanks!

officel commented 1 year ago

@hemberger

see https://docs.ansible.com/ansible/latest/galaxy/user_guide.html

Installing both roles and collections from the same requirements file will not work when specifying a custom collection or role install path. In this scenario the collections will be skipped and the command will process each like ansible-galaxy role install would.

and old version https://docs.ansible.com/ansible/2.9/galaxy/user_guide.html

While both roles and collections can be specified in one requirements file, they need to be installed separately. The ansible-galaxy role install -r requirements.yml will only install roles and ansible-galaxy collection install -r requirements.yml -p ./ will only install collections.

youe PR https://github.com/smrealms/seed-server/pull/16/files#diff-5c9ed18af9a5f902219d12c3044ccb193c2c304a3748d02702889c2ca5703978R23

I think, maybe not installed colections. need command like a ansible-galaxy collections install -r requirements.yml right?

hemberger commented 1 year ago

@officel Thanks for the pointers! I should have clarified that this is only an issue in the GitHub Action (locally, where I do have community.general installed, everything is working fine). I'm not sure what ansible-lint is doing under the hood to discover community collections. Do I need to run ansible-galaxy collections install -r requirements.yml in the GHA workflow before I run ansible-lint?

officel commented 1 year ago

I see. Then it does not seem necessary.

officel commented 1 year ago

@hemberger

I checked again. I misunderstood because I am not good at English. Sorry 🙇

ansible-lint-action uses creator-ee with docker. https://github.com/ansible/ansible-lint-action/blob/main/action.yml

creator-ee automatically reads requirements.yml in the top directory of the repository. The requirements.yml it is based on does not contain the community.general collection we need. https://github.com/ansible/creator-ee/blob/main/_build/requirements.yml

So when we use this ansible-lint-action, we need to put the collections in our requirements.yml.

No problem.

Schachte commented 1 year ago

@hemberger Did you get this working? I've modified my action to install the requirements from the root like so:

name: Ansible Deployment

on:
  workflow_dispatch:

jobs:
  linting:
    name: List Ansible Files
    runs-on: ubuntu-22.04

    steps:
    - name: Checkout Sources
      uses: actions/checkout@v2

    - name: Install Ansible
      run: sudo apt-get install -y ansible

    - name: Install Ansible-Galaxy Requirements
      run: ansible-galaxy install -r requirements.yaml

    - name: List Installed Ansible-Galaxy Requirements
      run: ansible-galaxy collection list

    - name: Linting
      uses: ansible/ansible-lint-action@dd225474d15635904348151b9e694472ce0f62a8
      with:
        path: ansible/

But I'm still getting FQCN resolution failures.

Collections

---
collections:
  - name: community.general
  - name: community.docker
hemberger commented 1 year ago

Did you get this working?

No, I haven't been able to get this to work either. Since ansible-lint is running in a docker container, my suspicion is that it will require changes to the creator-ee image or ansible-lint itself. https://github.com/ansible/ansible-lint-action/blob/dd225474d15635904348151b9e694472ce0f62a8/action.yml#L24-L27

Schachte commented 1 year ago

Bummer. @ssbarnea Any ideas on this one?

ssbarnea commented 1 year ago

You must have a requirements.yml file on your repo so linter could install these deps itself! Running galaxy before the action does not help as that is not inside the action container.

hemberger commented 1 year ago

You must have a requirements.yml file on your repo so linter could install these deps itself! Running galaxy before the action does not help as that is not inside the action container.

Thanks for the quick response! In my example job above (https://github.com/ansible/ansible-lint-action/issues/99#issuecomment-1365487875), I have community.general in my repo's requirements.yml file, but I still get the FQCN warning about it.

Schachte commented 1 year ago

Fair point. Can we override the path to avoid pushing the galaxy file into root?

ssbarnea commented 1 year ago

@Schachte You can pass path variable but that is not alone enough. You also need to create a config file at the desired location in order to convince linter to consider that path as project directory. Otherwise it will fallback to git and that will report the parent patch as being the project.

nielsk commented 1 year ago

Was there ever a solution to this? I have a chicken-egg-problem: when I put community.general into the requirements.yml I get an error that ERROR! Failed to find the collection dir deps: The collection galaxy.yml path '/Users/nik/.cache/ansible-compat/bec60e/collections/ansible_collections/community/general/galaxy.yml' does not exist.

If I do not have community.general in the requirements.yml I get errors with playbooks that use modules from community.general that the dependencies are not in the requirements.yml.

barzog commented 1 year ago

I've solved it by reverting to https://github.com/ansible/ansible-lint-action@v6.11.0 from current @main - which uses 6.14.4.

hervedevos commented 1 year ago

Same issue here, when i move back to v6.11.0 as suggested by @barzog it solves the issue but now my linter is buggy because of a loop-var-prefix[wrong] false positive, that seems to be fixed in a newer version of the linter...