Skip to content

Commit

Permalink
Introduce ansible-lint to format ansible files
Browse files Browse the repository at this point in the history
Add ansible-lint to the pre-commit-config file, and fix
failures/warnings for ansible files as well.

Signed-off-by: Vector Li <huanli@redhat.com>
  • Loading branch information
idorax authored and psss committed Mar 22, 2024
1 parent 0190b8a commit 4ad9ce6
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 65 deletions.
16 changes: 16 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ repos:
- id: yamllint
files: ^tmt/schemas/.*\.yaml

- repo: https://github.com/ansible-community/ansible-lint.git
rev: v24.2.1
hooks:
- id: ansible-lint
args:
- ansible/
- examples/ansible/
- examples/redis/ansible/
- tests/finish/ansible/data/
- tests/run/worktree/data/ansible/
- tmt/steps/prepare/feature/
- --profile=production
- --exclude=examples/redis/ansible/tasks/redis.yml
# redis.yml example requires posix module to be installed
# in order to be parsed by ansible-lint

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.3.3
hooks:
Expand Down
11 changes: 6 additions & 5 deletions ansible/packages.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
- hosts: all
- name: Install packages
hosts: all
tasks:
- name: Install necessary dependencies
dnf:
state: present
name: tmt
- name: Install necessary dependencies
ansible.builtin.dnf:
state: present
name: tmt
9 changes: 6 additions & 3 deletions examples/ansible/test.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
- hosts: localhost
- name: A simple test
hosts: localhost
tasks:
- name: Create a simple test file
copy:
ansible.builtin.copy:
dest: /tmp/prepared
content: pass
mode: preserve
- name: Check the file content
shell: grep pass /tmp/prepared
ansible.builtin.command: grep pass /tmp/prepared
changed_when: false
11 changes: 7 additions & 4 deletions examples/redis/ansible/setup_server.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
---
- hosts: all
- name: Setup server
hosts: all
vars:
- version: "{{ ansible_distribution_version }}"
version: "{{ ansible_distribution_version }}"
tasks:
- import_tasks: tasks/redis.yml
- import_tasks: tasks/redis_variables.yml
- name: Task redis
ansible.builtin.import_tasks: tasks/redis.yml
- name: Task redis variables
ansible.builtin.import_tasks: tasks/redis_variables.yml
...
3 changes: 2 additions & 1 deletion examples/redis/ansible/tasks/redis_variables.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
- name: Set variables into redis database
ansible.builtin.shell: |
ansible.builtin.command: |
redis-cli set key value1
changed_when: false
args:
executable: /bin/bash
12 changes: 8 additions & 4 deletions tests/finish/ansible/data/playbook.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
- hosts: all
- name: A simple playbook in finish phase
hosts: all
vars:
simple: "{{ lookup('env', 'SIMPLE') }}"
spaces: "{{ lookup('env', 'SPACES') }}"
tasks:
- name: Create a simple test file
copy:
ansible.builtin.copy:
dest: /tmp/finished
content: pass
mode: preserve

- name: Make sure a simple environment variable is respected
command: echo "SIMPLE='{{ simple }}'"
ansible.builtin.command: echo "SIMPLE='{{ simple }}'"
failed_when: simple != 'word'
changed_when: false

- name: Make sure variables with spaces are correctly handed over
command: echo "SPACES='{{ spaces }}'"
ansible.builtin.command: echo "SPACES='{{ spaces }}'"
failed_when: spaces != 'several words with spaces'
changed_when: false
6 changes: 5 additions & 1 deletion tests/prepare/ansible/data/playbook.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
- hosts: all
- name: Verify the test file was prepared
hosts: all
vars:
simple: "{{ lookup('env', 'SIMPLE') }}"
spaces: "{{ lookup('env', 'SPACES') }}"
Expand All @@ -7,11 +8,14 @@
copy:
dest: /tmp/prepared
content: pass
mode: preserve

- name: Make sure a simple environment variable is respected
command: echo "SIMPLE='{{ simple }}'"
failed_when: simple != 'word'
changed_when: false

- name: Make sure variables with spaces are correctly handed over
command: echo "SPACES='{{ spaces }}'"
failed_when: spaces != 'several words with spaces'
changed_when: false
11 changes: 8 additions & 3 deletions tests/run/worktree/data/ansible/playbook.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
- hosts: all
- name: Copy and check test file
hosts: all
tasks:
- copy:
- name: Copy test file
ansible.builtin.copy:
src: testfile
dest: /tmp/testfile
- command: grep PASS /tmp/testfile
mode: preserve
- name: Check test file
ansible.builtin.command: grep PASS /tmp/testfile
changed_when: false
51 changes: 30 additions & 21 deletions tmt/steps/prepare/feature/epel-disable.yaml
Original file line number Diff line number Diff line change
@@ -1,45 +1,54 @@
- hosts: all
- name: Disable EPEL repositories
hosts: all
tasks:
# RHEL 7 or CentOS 7
- when: ansible_distribution_major_version|int == 7
- name: Disable EPEL repos on RHEL 7 or CentOS 7
when: ansible_distribution in ["RedHat", "CentOS"] and ansible_distribution_major_version | int == 7
block:
- name: Install package 'yum-utils'
ansible.builtin.yum:
name: yum-utils
state: present

# Disable repos of 'epel' if package 'epel-release' is installed
- name: Detect package 'epel-release' is installed
command: rpm -q epel-release
ansible.builtin.command: rpm -q epel-release # noqa: command-instead-of-module
register: result
ignore_errors: True
ignore_errors: true
changed_when: false

- name: Disable repos of 'epel'
shell: yum-config-manager --disable epel epel-debuginfo epel-source
- name: Disable EPEL repos
when: result.rc == 0
ansible.builtin.command: yum-config-manager --disable epel epel-debuginfo epel-source
register: output
changed_when: output.rc != 0

# RHEL N or CentOS Stream N where N >= 8
- when: ansible_distribution_major_version|int >= 8
- name: Disable EPEL and EPEL-Next repos on RHEL 8+ or CentOS Stream 8+
when: ansible_distribution in ["RedHat", "CentOS"] and ansible_distribution_major_version | int >= 8
block:
- name: Install 'dnf config-manager'
shell: dnf -y install 'dnf-command(config-manager)'
ansible.builtin.command: dnf -y install 'dnf-command(config-manager)'
register: output
changed_when: output.rc != 0

# Disable repos of 'epel' if package 'epel-release' is installed
- name: Detect package 'epel-release' is installed
command: rpm -q epel-release
ansible.builtin.command: rpm -q epel-release # noqa: command-instead-of-module
register: result
ignore_errors: True
ignore_errors: true
changed_when: false

- name: Disable repos of 'epel' if package 'epel-release' is installed
shell: dnf config-manager --disable epel epel-debuginfo epel-source
- name: Disable EPEL repos
when: result.rc == 0
ansible.builtin.command: dnf config-manager --disable epel epel-debuginfo epel-source
register: output
changed_when: output.rc != 0

# Disable repos of 'epel-next' if package 'epel-next-release' is installed
- name: Detect package 'epel-next-release' is installed
command: rpm -q epel-next-release
ansible.builtin.command: rpm -q epel-release # noqa: command-instead-of-module
register: result
ignore_errors: True
ignore_errors: true
changed_when: false

- name: Disable repos of 'epel-next'
shell: dnf config-manager --disable epel-next epel-next-debuginfo epel-next-source
- name: Disable EPEL-Next repos
when: result.rc == 0
ansible.builtin.command: dnf config-manager --disable epel-next epel-next-debuginfo epel-next-source
register: output
changed_when: output.rc != 0
63 changes: 40 additions & 23 deletions tmt/steps/prepare/feature/epel-enable.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
- hosts: all
- name: Enable EPEL repositories
hosts: all
tasks:
# RHEL 7
- when: ansible_distribution == 'RedHat' and ansible_distribution_major_version|int == 7
- name: Enable EPEL repos on RHEL 7
when: ansible_distribution == "RedHat" and ansible_distribution_major_version | int == 7
block:
- name: Install package 'epel-release'
ansible.builtin.yum:
Expand All @@ -14,11 +15,13 @@
name: yum-utils
state: present

- name: Enable repos of 'epel'
shell: yum-config-manager --enable epel epel-debuginfo epel-source
- name: Enable EPEL repos
ansible.builtin.command: yum-config-manager --enable epel epel-debuginfo epel-source
register: output
changed_when: output.rc != 0

# RHEL N, where N >= 8
- when: ansible_distribution == "RedHat" and ansible_distribution_major_version|int >= 8
- name: Enable EPEL and EPEL-Next repos on RHEL 8+
when: ansible_distribution == "RedHat" and ansible_distribution_major_version | int >= 8
block:
- name: Install package 'epel-release'
ansible.builtin.yum:
Expand All @@ -33,16 +36,22 @@
state: present

- name: Install 'dnf config-manager'
shell: dnf -y install 'dnf-command(config-manager)'
ansible.builtin.command: dnf -y install 'dnf-command(config-manager)'
register: output
changed_when: output.rc != 0

- name: Enable repos of 'epel'
shell: dnf config-manager --enable epel epel-debuginfo epel-source
- name: Enable EPEL repos
ansible.builtin.command: dnf config-manager --enable epel epel-debuginfo epel-source
register: output
changed_when: output.rc != 0

- name: Enable repos of 'epel-next'
shell: dnf config-manager --enable epel-next epel-next-debuginfo epel-next-source
- name: Enable EPEL-Next repos
ansible.builtin.command: dnf config-manager --enable epel-next epel-next-debuginfo epel-next-source
register: output
changed_when: output.rc != 0

# CentOS 7
- when: ansible_distribution == "CentOS" and ansible_distribution_major_version|int == 7
- name: Enable EPEL repos on CentOS 7
when: ansible_distribution == "CentOS" and ansible_distribution_major_version | int == 7
block:
- name: Install package 'epel-release'
ansible.builtin.yum:
Expand All @@ -54,11 +63,13 @@
name: yum-utils
state: present

- name: Enable repos of 'epel'
shell: yum-config-manager --enable epel epel-debuginfo epel-source
- name: Enable EPEL repos
ansible.builtin.command: yum-config-manager --enable epel epel-debuginfo epel-source
register: output
changed_when: output.rc != 0

# CentOS Stream N where N >= 8
- when: ansible_distribution == "CentOS" and ansible_distribution_major_version|int >= 8
- name: Enable EPEL and EPEL-Next repos on CentOS Stream 8+
when: ansible_distribution == "CentOS" and ansible_distribution_major_version | int >= 8
block:
- name: Install package 'epel-release'
ansible.builtin.yum:
Expand All @@ -71,10 +82,16 @@
state: present

- name: Install 'dnf config-manager'
shell: dnf -y install 'dnf-command(config-manager)'
ansible.builtin.command: dnf -y install 'dnf-command(config-manager)'
register: output
changed_when: output.rc != 0

- name: Enable repos of 'epel'
shell: dnf config-manager --enable epel epel-debuginfo epel-source
- name: Enable EPEL repos
ansible.builtin.command: dnf config-manager --enable epel epel-debuginfo epel-source
register: output
changed_when: output.rc != 0

- name: Enable repos of 'epel-next'
shell: dnf config-manager --enable epel-next epel-next-debuginfo epel-next-source
- name: Enable EPEL-Next repos
ansible.builtin.command: dnf config-manager --enable epel-next epel-next-debuginfo epel-next-source
register: output
changed_when: output.rc != 0

0 comments on commit 4ad9ce6

Please sign in to comment.