Reviewing and correcting common mistakes in code or a configuration file is important to ensure that it's readable and easy to maintain.
Although Ansible is a popular open source configuration management tool, it is not immune to mistakes. It is critical that system administrators check and review playbooks, roles, dependencies, and collections to prevent possible bugs and ensure easy maintenance.
[ Take a no-cost technical overview course from Red Hat. Ansible Essentials: Simplicity in Automation Technical Overview. ]
A linter is a tool designed to catch data errors before processing a file. One linter specifically designed for Ansible playbooks is Ansible Lint, a readily available Python command-line tool that helps content creators to write, standardize, and package high-quality Ansible content.
Traditionally, to check for basic syntax errors in an Ansible playbook, you would run the playbook with --syntax-check. However, the --syntax-check flag is not as comprehensive or in-depth as the ansible-lint tool. You can integrate Ansible Lint into a CI/CD pipeline to check for potential issues such as deprecated or removed modules, syntax errors, idempotent playbooks, and more. It offers suggestions as to which Ansible module can best suit a particular situation. Ansible Lint ensures best practices by using a set of default rules built into the tool.
Install Ansible Lint
You can install Ansible Lint with the pip3 or the dnf package manager.
On Red Hat Enterprise Linux (RHEL) systems without the Red Hat Ansible Automation Platform subscription, install ansible-lint in the command line, as shown below:
# pip3 install ansible-lint
On RHEL systems with a Red Hat Ansible Automation Platform subscription, install ansible-lint with this command:
# dnf install ansible-lint
Alternatively, you can install ansible-lint from source using pip3. The installation requires pip>=22.3.1. Use the following command to install from source:
# pip3 install git+https://github.com/ansible/ansible-lint
Use ansible-lint
First, run ansible-lint against a playbook to print all potential rule violations.
In the following example, I created a sample playbook called test.yml, which installs the sos package on localhost:
---
- hosts: localhost
tasks:
- name: install package
shell: |
yum install -y sos
Next, I will run ansible-lint against this playbook and observe the result:
$ ansible-lint test.yml
The resulting output looks like this:
WARNING Listing 5 violation(s) that are fatal
name[play]: All plays should be named.
test.yml:2
command-instead-of-module: yum used in place of yum module
test.yml:4 Task/Handler: install package
fqcn[action-core]: Use FQCN for builtin module actions (shell).
test.yml:4 Use `ansible.builtin.shell` or `ansible.legacy.shell` instead.
name[casing]: All names should start with an uppercase letter.
test.yml:4 Task/Handler: install package
no-changed-when: Commands should not change things if nothing needs doing.
test.yml:4 Task/Handler: install package
Read documentation for instructions on how to ignore specific rule violations.
Rule Violation Summary
count tag profile rule associated tags
1 command-instead-of-module basic command-shell, idiom
1 name[play] basic idiom
1 name[casing] moderate idiom
1 no-changed-when shared command-shell, idempotency
1 fqcn[action-core] production formatting
Failed after min profile: 5 failure(s), 0 warning(s) on 1 files.
[ Write your first playbook in this hands-on interactive lab. ]
As shown above, Ansible Lint discovered 5 violations:
1. All plays should be named.
name[play]: All plays should be named.
test.yml:2
As seen in the playbook, the play does not have a name.
2. The yum command is used in place of the yum module.
command-instead-of-module: yum used in place of yum module
test.yml:4 Task/Handler: install package
The yum module should be used instead of the shell module, which is inappropriate for this case.
3. Use the FQCN for built-in module actions.
fqcn[action-core]: Use FQCN for builtin module actions (shell).
test.yml:4 Use `ansible.builtin.shell` or `ansible.legacy.shell` instead.
The fully qualified collection name (FQCN) should be used for the builtin module. In this case, even if I used the shell module, a more proper way is with ansible.builtin.shell
4. All names should start with an uppercase letter.
name[casing]: All names should start with an uppercase letter.
test.yml:4 Task/Handler: install package
The name of a task should start with an uppercase letter. In this playbook, the task should be Install package. This practice ensures standardization across all playbooks.
5. Commands should not change things if nothing needs to be done.
no-changed-when: Commands should not change things if nothing needs doing.
test.yml:4 Task/Handler: install package
This violates of the principle of idempotency, which ensures that a playbook can be applied repeatedly with the same results. The shell module is generally not considered idempotent.
Here is a cleaner way of writing the same playbook:
---
- name: Install sos on servers
hosts: localhost
tasks:
- name: Install package
ansible.builtin.yum:
name: sos
state: present
If you run ansible-lint again, it passes:
$ ansible-lint test.yml
Passed with production profile: 0 failure(s), 0 warning(s) on 1 files.
Wrap up
Ansible Lint allows system administrators to verify that playbooks and other artifacts conform to recommended practices and don't contain errors or bugs. It improves automation functionality, reliability, and the readability and maintainability of your content.
In future articles, I will look at the configuration, rules, and profiles used with Ansible Lint.
저자 소개
I work as Unix/Linux Administrator with a passion for high availability systems and clusters. I am a student of performance and optimization of systems and DevOps. I have passion for anything IT related and most importantly automation, high availability, and security.
유사한 검색 결과
Slash VM provisioning time on Red Hat Openshift Virtualization using Red Hat Ansible Automation Platform
Red Hat Ansible Automation Platform: Measuring Business Impact with Dashboard and Analytics
Technically Speaking | Taming AI agents with observability
How Do We Mentor the Next Generation of IT Leaders? | Compiler
채널별 검색
오토메이션
기술, 팀, 인프라를 위한 IT 자동화 최신 동향
인공지능
고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트
오픈 하이브리드 클라우드
하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요
보안
환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보
엣지 컴퓨팅
엣지에서의 운영을 단순화하는 플랫폼 업데이트
인프라
세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보
애플리케이션
복잡한 애플리케이션에 대한 솔루션 더 보기
가상화
온프레미스와 클라우드 환경에서 워크로드를 유연하게 운영하기 위한 엔터프라이즈 가상화의 미래