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

Add ansible-lint support #54

Open
wants to merge 63 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
96c1298
See issue #4: Created a deploy goal rule
sureshjoshi Feb 13, 2022
bdcbb80
See issue #4: Added link to Readme on main page
sureshjoshi Feb 13, 2022
6093610
See issue #4: Created a barebones ansible-playbook command by re-usin…
sureshjoshi Feb 14, 2022
8e0f41e
first pass at ansible-lint task
lilatomic Feb 23, 2022
9b045dc
change to ansible-lint version prior to automagic ansible detection
lilatomic Mar 7, 2022
661241d
use `--venv prepend` for PEX to get multiple binaries working
lilatomic Mar 7, 2022
76ac152
update for pants 2.11.0.dev2
lilatomic Mar 20, 2022
59f7559
expand ansible example with other playbook and tasklist
lilatomic Mar 20, 2022
71d1e01
add play context as SourcesField
lilatomic Mar 20, 2022
398415d
add ansible context loading to linting
lilatomic Mar 20, 2022
8e2a060
fmt
lilatomic Mar 20, 2022
920e2ae
extract rule for turning play contexts into Digests
lilatomic Mar 20, 2022
2d45ddd
extract function to get contexts from request
lilatomic Mar 20, 2022
0031bb7
remove dead code
lilatomic Mar 20, 2022
3523b0f
multiget for sources and ansible
lilatomic Mar 20, 2022
7f41d5b
update AnsibleCheckRequest with name for pants 2.11
lilatomic Mar 20, 2022
7ff0141
[refactor] move resolve_ansible_context
lilatomic Mar 20, 2022
36720e7
rebuild check to use context
lilatomic Mar 20, 2022
24b38ef
use mutliget in check
lilatomic Mar 21, 2022
0b36102
get context in deployment task
lilatomic Mar 21, 2022
50d7ab7
lint
lilatomic Mar 21, 2022
e4ed498
resolve Ansible context in one rule
lilatomic Mar 23, 2022
bde3146
better processing of arbitrary requests into contexts
lilatomic Mar 23, 2022
04716fd
use common resolver
lilatomic Mar 23, 2022
62f0d9d
linting doesn't need target playbooks
lilatomic Mar 24, 2022
8fbb399
extract sources to own file
lilatomic Mar 24, 2022
e8c227c
pull together more sources
lilatomic Mar 24, 2022
7645913
clarify generation of source globs
lilatomic Mar 24, 2022
bcf776a
unalias source name and create sources parent class
lilatomic Mar 24, 2022
45d90e1
stub Ansible collection and role
lilatomic Mar 26, 2022
b80dad0
Add ansible_role target type
lilatomic Mar 26, 2022
dc2949d
add some content to the role stub
lilatomic Mar 26, 2022
21ccfea
retarget AnsibleLintFieldSet to generic AnsibleSources
lilatomic Mar 26, 2022
27d5768
Add ansible_collection target type
lilatomic Mar 27, 2022
11743ef
capture helloansible dir as test resources
lilatomic Mar 28, 2022
3deb5c2
add basic tests for `check`
lilatomic Mar 28, 2022
658211f
use parseable output for ansible-lint
lilatomic Mar 28, 2022
2c19290
add test for ansible-lint and generalise utilities
lilatomic Mar 28, 2022
f217929
add tests for other ansible source types
lilatomic Mar 28, 2022
31ce9aa
add tests for deploy rule
lilatomic Mar 30, 2022
b707acb
don't exclude Pants-supplied flake8 ignores
lilatomic Mar 30, 2022
9aafb21
fmt & lint
lilatomic Mar 30, 2022
993aae4
use to_pex_request
lilatomic Mar 30, 2022
59cc298
add ansible-lint "--args" config setting
lilatomic Mar 30, 2022
99cf850
add ansible deployment field for extra arguments
lilatomic Mar 30, 2022
8dff05a
[merge] pants.toml
lilatomic Apr 6, 2022
f3965e7
[merge] readmes
lilatomic Apr 6, 2022
072a2b1
[merge] use roles in playbook.yml
lilatomic Apr 6, 2022
3bcc624
[merge] move subsystems into subfolder
lilatomic Apr 6, 2022
fd82c99
Merge branch 'main' into 4/lint
lilatomic Apr 6, 2022
6de0110
[merge] recapture test directory
lilatomic Apr 6, 2022
3f06e92
playbooks can pull in local roles
lilatomic Apr 6, 2022
63334f5
turn ansible-galaxy into PythonToolBase
lilatomic Apr 6, 2022
fa70106
insert galaxy requirement installation in ansible deploy
lilatomic Apr 6, 2022
8cb2e62
tell ansible-playbook to use installed collections
lilatomic Apr 6, 2022
0e5c96b
install dependent collections in tests
lilatomic Apr 6, 2022
a650ce9
extract galaxy fetching to rule
lilatomic Apr 7, 2022
660b6ea
install galaxy dependencies in "check" too
lilatomic Apr 7, 2022
1c6dd92
actually use collections path
lilatomic Apr 7, 2022
182b6a8
remove comments from ansible skeletons
lilatomic Apr 7, 2022
dc5d4dd
Incorporate new changes in Pants
lilatomic Jun 6, 2022
e9a617a
fix some typehints
lilatomic Jun 6, 2022
3a64c08
fix more typehints
lilatomic Jun 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,6 @@ cython_debug/
/dist/
/.pids
/.pants.workdir.file_lock*

# IDEs
/.idea/
14 changes: 8 additions & 6 deletions helloansible/BUILD.pants
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
ansible_sources(
name="libansible",
sources=["**/*"],
)

ansible_deployment(
name="helloansible",
dependencies=[":libansible"],
dependencies=[""],
playbook="playbook.yml",
# inventory="", # TODO: Could this be setup like the Docker registries? in pants.toml
# tags="", # TODO: Might need a different name to not overlap
# timeout="",
)

ansible_deployment(
name="helloansible_with_tags",
dependencies=[""],
playbook="playbook.yml",
ansible_playbook_args=["--tags", "hihello"],
)
Empty file added helloansible/hello/BUILD
Empty file.
4 changes: 4 additions & 0 deletions helloansible/hello/collection/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ansible_collection(
name="hello.collection",
dependencies=[""],
)
3 changes: 3 additions & 0 deletions helloansible/hello/collection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Ansible Collection - hello.collection

Documentation for the collection.
19 changes: 19 additions & 0 deletions helloansible/hello/collection/galaxy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### REQUIRED
namespace: hello
name: collection
version: 1.0.0
readme: README.md
authors:
- your name <[email protected]>
### OPTIONAL but strongly recommended
description: your collection description
license:
- GPL-2.0-or-later
license_file: ''
tags: []
dependencies: {}
repository: http://example.com/repository
documentation: http://docs.example.com
homepage: http://example.com
issues: http://example.com/issue/tracker
build_ignore: []
6 changes: 6 additions & 0 deletions helloansible/hello/collection/playbooks/playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- hosts: localhost
tasks:
- name: Create an error in the collection lints
ansible.builtin.shell: ls
register: shell_result
1 change: 1 addition & 0 deletions helloansible/hello/collection/plugins/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Collections Plugins Directory
Empty file.
4 changes: 4 additions & 0 deletions helloansible/hello/collection/roles/hellorole/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ansible_role(
name="helloansiblerole",
dependencies=[""],
)
4 changes: 4 additions & 0 deletions helloansible/hello/collection/roles/hellorole/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Role Name
=========

A brief description of the role goes here.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
# defaults file for hellorole
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- name: trigger handler
debug:
msg: |
handler triggered
8 changes: 8 additions & 0 deletions helloansible/hello/collection/roles/hellorole/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
galaxy_info:
author: your name
description: your role description
company: your company (optional)
license: license (GPL-2.0-or-later, MIT, etc)
min_ansible_version: 2.1
galaxy_tags: []
dependencies: []
17 changes: 17 additions & 0 deletions helloansible/hello/collection/roles/hellorole/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
- name: display in role
debug:
msg: |
Hello from role

- name: trigger error in ansible-lint
shell: >-
echo "hello from shell in role"

- name: trigger handler to test that handlers are included
debug:
msg: |
trigger handler in role
changed_when: true
notify:
- "trigger handler"
2 changes: 2 additions & 0 deletions helloansible/hello/collection/roles/hellorole/tests/inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
localhost

5 changes: 5 additions & 0 deletions helloansible/hello/collection/roles/hellorole/tests/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- hosts: localhost
remote_user: root
roles:
- hellorole
2 changes: 2 additions & 0 deletions helloansible/hello/collection/roles/hellorole/vars/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
# vars file for hellorole
11 changes: 11 additions & 0 deletions helloansible/other_playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
- name: other playbook
hosts: localhost
connection: local
gather_facts: false

tasks:
- name: log from other playbook
debug:
msg: >-
Hello I am in another playbook
14 changes: 14 additions & 0 deletions helloansible/playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,17 @@
roles:
- role: example
- role: collectionexample

tasks:
- name: A do-nothing shell command
ansible.builtin.shell: ls
register: shell_result

- name: Print out the results of the shell command
ansible.builtin.debug:
var: shell_result

- name: include some tasks from context
include_tasks: tasks/main.yml

- import_playbook: other_playbook.yml
5 changes: 5 additions & 0 deletions helloansible/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- name: log from tasks folder
debug:
msg: >-
Hello I am in the tasks folder
10 changes: 10 additions & 0 deletions pants-plugins/experimental/ansible/BUILD
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
file(
name="helloansible", source="helloansible.test.toml"
)


python_sources()
python_tests(
name="tests",
dependencies=[":helloansible"]

)
9 changes: 2 additions & 7 deletions pants-plugins/experimental/ansible/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
## How to use

In your `BUILD` file, use the new `ansible_sources` to pull in the entire target directory and `ansible_deployment` to support the new `./pants deploy` goal:

```python
ansible_sources(
name="libansible",
sources=["**/*"],
)

```python
ansible_deployment(
name="helloansible",
dependencies=[":libansible"],
dependencies=[""],
playbook="playbook.yml",
#inventory="", # TODO: Could this be setup like the Docker registries? in pants.toml
#tags="", # TODO: Might need a different name to not overlap
Expand Down
12 changes: 5 additions & 7 deletions pants-plugins/experimental/ansible/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
from pants.engine.fs import EMPTY_DIGEST, Digest
from pants.engine.goal import Goal, GoalSubsystem
from pants.engine.process import FallibleProcessResult
from pants.engine.rules import Get, collect_rules, goal_rule
from pants.engine.rules import Get, Rule, collect_rules, goal_rule
from pants.engine.target import FieldSet
from pants.engine.unions import union
from pants.engine.unions import UnionRule, union
from pants.util.logging import LogLevel
from pants.util.memo import memoized_property
from pants.util.meta import frozen_after_init
Expand Down Expand Up @@ -158,13 +158,11 @@ async def deploy(
)

field_set = target_roots_to_deployment_field_sets.field_sets[0]
_ = await Get(
DeployResults, DeploymentFieldSet, field_set
) # TODO: Make Flake happy
# request = await Get(DeployResults, DeploymentFieldSet, field_set)
request = await Get(DeployResults, DeploymentFieldSet, field_set)
# TODO: Do something with the result
logger.info(request)
return Deploy(exit_code=0)


def rules():
def rules() -> Iterable[Rule | UnionRule]:
return collect_rules()
24 changes: 24 additions & 0 deletions pants-plugins/experimental/ansible/helloansible.test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"helloansible/playbook.yml" = "---\n- hosts: localhost\n roles:\n - role: example\n - role: collectionexample\n\n tasks:\n - name: A do-nothing shell command\n ansible.builtin.shell: ls\n register: shell_result\n\n - name: Print out the results of the shell command\n ansible.builtin.debug:\n var: shell_result\n\n - name: include some tasks from context\n include_tasks: tasks/main.yml\n\n- import_playbook: other_playbook.yml\n"
"helloansible/requirements.yml" = "---\ncollections:\n # With just the collection name\n - community.docker\n"
"helloansible/other_playbook.yml" = "---\n- name: other playbook\n hosts: localhost\n connection: local\n gather_facts: false\n\n tasks:\n - name: log from other playbook\n debug:\n msg: >-\n Hello I am in another playbook\n"
"helloansible/BUILD.pants" = "ansible_deployment(\n name=\"helloansible\",\n dependencies=[\"\"],\n playbook=\"playbook.yml\",\n # inventory=\"\", # TODO: Could this be setup like the Docker registries? in pants.toml\n # tags=\"\", # TODO: Might need a different name to not overlap\n # timeout=\"\",\n)\n\nansible_deployment(\n name=\"helloansible_with_tags\",\n dependencies=[\"\"],\n playbook=\"playbook.yml\",\n ansible_playbook_args=[\"--tags\", \"hihello\"],\n)\n"
"helloansible/roles/collectionexample/tasks/main.yml" = "---\n- name: Get info on docker host\n community.docker.docker_host_info:\n register: docker_result\n\n- name: Print out the results of the docker command\n ansible.builtin.debug:\n var: docker_result\n\n"
"helloansible/roles/example/tasks/main.yml" = "---\n- name: A do-nothing shell command\n ansible.builtin.shell: ls\n register: shell_result\n\n- name: Print out the results of the shell command\n ansible.builtin.debug:\n var: shell_result\n\n"
"helloansible/tasks/main.yml" = "---\n- name: log from tasks folder\n debug:\n msg: >-\n Hello I am in the tasks folder\n"
"helloansible/hello/BUILD" = ""
"helloansible/hello/collection/BUILD" = "ansible_collection(\n\tname=\"hello.collection\",\n\tdependencies=[\"\"],\n)"
"helloansible/hello/collection/README.md" = "# Ansible Collection - hello.collection\n\nDocumentation for the collection.\n"
"helloansible/hello/collection/galaxy.yml" = "### REQUIRED\nnamespace: hello\nname: collection\nversion: 1.0.0\nreadme: README.md\nauthors:\n- your name <[email protected]>\n### OPTIONAL but strongly recommended\ndescription: your collection description\nlicense:\n- GPL-2.0-or-later\nlicense_file: ''\ntags: []\ndependencies: {}\nrepository: http://example.com/repository\ndocumentation: http://docs.example.com\nhomepage: http://example.com\nissues: http://example.com/issue/tracker\nbuild_ignore: []\n"
"helloansible/hello/collection/plugins/README.md" = "# Collections Plugins Directory\n"
"helloansible/hello/collection/playbooks/playbook.yml" = "---\n- hosts: localhost\n tasks:\n - name: Create an error in the collection lints\n ansible.builtin.shell: ls\n register: shell_result\n"
"helloansible/hello/collection/roles/BUILD" = ""
"helloansible/hello/collection/roles/hellorole/BUILD" = "ansible_role(\n\tname=\"helloansiblerole\",\n\tdependencies=[\"\"],\n)"
"helloansible/hello/collection/roles/hellorole/README.md" = "Role Name\n=========\n\nA brief description of the role goes here.\n"
"helloansible/hello/collection/roles/hellorole/defaults/main.yml" = "---\n# defaults file for hellorole\n"
"helloansible/hello/collection/roles/hellorole/vars/main.yml" = "---\n# vars file for hellorole\n"
"helloansible/hello/collection/roles/hellorole/meta/main.yml" = "galaxy_info:\n author: your name\n description: your role description\n company: your company (optional)\n license: license (GPL-2.0-or-later, MIT, etc)\n min_ansible_version: 2.1\n galaxy_tags: []\ndependencies: []\n"
"helloansible/hello/collection/roles/hellorole/tasks/main.yml" = "---\n- name: display in role\n debug:\n msg: |\n Hello from role\n\n- name: trigger error in ansible-lint\n shell: >-\n echo \"hello from shell in role\"\n\n- name: trigger handler to test that handlers are included\n debug:\n msg: |\n trigger handler in role\n changed_when: true\n notify:\n - \"trigger handler\"\n"
"helloansible/hello/collection/roles/hellorole/handlers/main.yml" = "---\n- name: trigger handler\n debug:\n msg: |\n handler triggered\n"
"helloansible/hello/collection/roles/hellorole/tests/test.yml" = "---\n- hosts: localhost\n remote_user: root\n roles:\n - hellorole\n"
"helloansible/hello/collection/roles/hellorole/tests/inventory" = "localhost\n\n"
"helloansible/inventories/localhost" = "[localhost]\n"
10 changes: 3 additions & 7 deletions pants-plugins/experimental/ansible/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from experimental.ansible.deploy import rules as deploy_rules
from experimental.ansible.rules import rules as ansible_rules
from experimental.ansible.target_types import (
AnsibleCollection,
AnsibleDeployment,
AnsibleSourcesGeneratorTarget,
AnsibleSourceTarget,
AnsibleRole,
)
from pants.engine.rules import Rule
from pants.engine.target import Target
Expand All @@ -22,8 +22,4 @@ def rules() -> Iterable[Rule | UnionRule]:


def target_types() -> Iterable[type[Target]]:
return (
AnsibleDeployment,
AnsibleSourceTarget,
AnsibleSourcesGeneratorTarget,
)
return (AnsibleDeployment, AnsibleRole, AnsibleCollection)
Loading