Skip to content

Commit

Permalink
Ansible Lint corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
MozeBaltyk committed Aug 17, 2023
1 parent 1dfb236 commit 035b424
Show file tree
Hide file tree
Showing 24 changed files with 175 additions and 134 deletions.
5 changes: 4 additions & 1 deletion .ansible-lint
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ exclude_paths:
- .ansible-lint
- .yamllint
- scripts/
- playbooks/tasks

skip_list:
- yaml[line-length]
- yaml[line-length] # max line length
- command-instead-of-shell # Use shell only when shell functionality is required
- experimental # all rules tagged as experimental
8 changes: 0 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,3 @@ jobs:
steps:
- name: 🚀 Run ansible-lint
uses: ansible/ansible-lint@main


#- name: Auto Correct YAML lint errors
# run: |
# echo '# Correct no new line character at the end of file'
# sed -i -e '$a\' playbooks/tasks/connectRepo.yml
# echo '# Correct trailing spaces"
# sed -i -e 's/[ \t]*$//' playbooks/tasks/connectRepo.yml
8 changes: 4 additions & 4 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ _precheck_collection NAME:
#!/usr/bin/env bash
if ! echo "{{NAME}}" | egrep -q "^[a-zA-Z][a-zA-Z0-9_]+$"; then
printf "\e[1;31m[ERROR]\e[m The name choosen for the project is incompatible with an Ansible collection.\n";
printf "\e[1;34m[INFO]\e[m Respect this REGEX ^[a-zA-Z][a-zA-Z0-9_]+$ for Collection name.\n";
printf "\e[1;34m[INFO]\e[m Respect this REGEX ^[a-zA-Z][a-zA-Z0-9_]+$ in the name of your Ansible Collection.\n";
exit 1
fi
_precheck_role NAME:
#!/usr/bin/env bash
if ! echo "{{NAME}}" | egrep -q "^[a-z][a-z0-9_]+$"; then
printf "\e[1;31m[ERROR]\e[m The name choosen for the role is incompatible with Ansible collection.\n";
printf "\e[1;34m[INFO]\e[m Respect this REGEX ^[a-z][a-z0-9_]+$ for role name.\n";
printf "\e[1;34m[INFO]\e[m Respect this REGEX ^[a-z][a-z0-9_]+$ in the name of your role.\n";
exit 1
fi
Expand Down Expand Up @@ -71,7 +71,7 @@ install PROJECT *VERSION:
#!/usr/bin/env bash
if [[ {{PROJECT}} =~ .*.tar.gz ]]; then
printf "Install artifact: {{PROJECT}}\n"
ansible-galaxy collection install -U $HOME/{{PROJECT}}
ansible-galaxy collection install -U {{PROJECT}}
else
printf "Install {{PROJECT}} from {{REPOSITORY}}\n"
just -f scripts/justfile/install.justfile _install {{PROJECT}} {{REPOSITORY}} {{VERSION}}
Expand All @@ -93,7 +93,7 @@ build PROJECT NAMESPACE:
@just _precheck_collection {{PROJECT}}

#!/usr/bin/env bash
ansible-galaxy collection build {{NAMESPACE}}/{{PROJECT}}
ansible-galaxy collection build $HOME/{{NAMESPACE}}/{{PROJECT}} --output-path $HOME/{{NAMESPACE}}/{{PROJECT}}
# Test
_test PROJECT ROLE *NAMESPACE:
Expand Down
3 changes: 2 additions & 1 deletion playbooks/tasks/connectRepo.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
- hosts: '{{target|default("localhost")}}'
- name: Connect repository
hosts: '{{target|default("localhost")}}'
connection: local
gather_facts: false

Expand Down
28 changes: 14 additions & 14 deletions playbooks/tasks/createCollection.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,44 @@

pre_tasks:
# Email Authors for both collection and roles
- block:
- name: Block email authors
when: (global_author_email is not defined) or (global_author_email|length == 0)
run_once: true
block:
- name: Give an email to ansible collection
pause:
ansible.builtin.pause:
prompt: "Give an email that we can set as author of this collection (ENTER take from git config global)"
register: prompt_email

- name: Set authors email
set_fact:
ansible.builtin.set_fact:
global_author_email: "{{ prompt_email['user_input'] }}"
when: prompt_email['user_input']|length != 0

- block:
- name: Get User.Email from git global config to define authors of the collection
shell: "git config --global user.email"
ansible.builtin.shell: "git config --global user.email"
register: gitconfig_email

- name: Set authors email from git config
set_fact:
ansible.builtin.set_fact:
global_author_email: "{{ gitconfig_email['stdout'] }}"
when: prompt_email['user_input']|length == 0

when: (global_author_email is not defined) or (global_author_email|length == 0)
run_once: true

# Description for Project and Collection
- block:
- name: Block description
when: (global_collection_description is not defined) or (global_collection_description|length == 0)
run_once: true
block:
- name: Set description to ansible collection
pause:
ansible.builtin.pause:
prompt: "Please give a description to your collection"
register: prompt_description

- name: Set your description coming from prompt
set_fact:
ansible.builtin.set_fact:
global_collection_description: "{{ prompt_description['user_input'] }}"

when: (global_collection_description is not defined) or (global_collection_description|length == 0)
run_once: true

roles:
- { role: connect_to_repository, tags: repository, when: global_target_repository|length != 0 }
- { role: create_collection, tags: collection, }
Expand Down
3 changes: 2 additions & 1 deletion roles/connect_to_repository/meta/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
galaxy_info:
standalone: false # Part of a collection
author: MozeBaltyk
description: role to connect to your repository
company: not for a company
Expand All @@ -16,7 +17,7 @@ galaxy_info:
# - CC-BY-4.0
license: Apache-2.0

min_ansible_version: 2.11
min_ansible_version: "2.12.0"

# If this a Container Enabled role, provide the minimum Ansible Container version.
# min_ansible_container_version:
Expand Down
14 changes: 9 additions & 5 deletions roles/connect_to_repository/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@
ansible.builtin.include_role:
name: setup_repo_cmd

- block:
- name: Block connect repo
block:
- name: Check auth status
shell: "{{ CMD }} auth status"
ansible.builtin.shell: "{{ cmd }} auth status"
register: auth_status
changed_when: false

- debug:
- name: Debug
ansible.builtin.debug:
var: auth_status['stdout']

rescue:
- debug:
- name: Debug
ansible.builtin.debug:
msg: |
# Run manually command: "{{ CMD }} auth login {{ RIGHTS }}"
# Run manually command: "{{ cmd }} auth login {{ rights }}"
#
# Then relaunch your command...
3 changes: 2 additions & 1 deletion roles/connect_to_repository/tests/test.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
- hosts: localhost
- name: Test plays
hosts: localhost
remote_user: root
roles:
- connect_to_repository
4 changes: 2 additions & 2 deletions roles/create_collection/files/playbooks/ping.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
tasks:
# Print Facts
- name: Print facts - FQDN - distribution - version
debug:
ansible.builtin.debug:
msg:
- "This is playing on hosts: {{ ansible_play_hosts }}"
- "{{ ansible_fqdn }} - {{ ansible_distribution }} {{ ansible_distribution_version }}, connexion with {{ ansible_user }}, install with {{ install_user }}"
- "OS Family: {{ ansible_os_family }}"

- name: Print facts - FQDN - interface
debug:
ansible.builtin.debug:
msg: "{{ ansible_fqdn }} - {{ hostvars[inventory_hostname]['ansible_' + item]['device'] }}"
loop: "{{ ansible_interfaces }}"
36 changes: 20 additions & 16 deletions roles/create_collection/files/playbooks/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,35 @@
- name: Test and Display some vars
hosts: all
serial: 1
vars_files: ../vars/main.yml
vars:
- ip_address: "{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}"
- iface: "{{ hostvars[inventory_hostname]['ansible_default_ipv4']['interface'] }}"
- disk_limit: "0.8"
- fs: "/tmp"
ip_address: "{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}"
iface: "{{ hostvars[inventory_hostname]['ansible_default_ipv4']['interface'] }}"
disk_limit: "0.8"
fs: "/tmp"

tasks:
# Network
- name: Print Default interface info
debug:
ansible.builtin.debug:
msg: "{{ iface }} - {{ ip_address }}"

# Disks
- set_fact:
mount: '{{ ansible_mounts | selectattr("mount", "eq", fs ) }}'
- set_fact:
- name: Set Fact
ansible.builtin.set_fact:
mount: '{{ ansible_mounts | selectattr("mount", "eq", fs) }}'
- name: Set Fact
ansible.builtin.set_fact:
disk_usage: "{{ mount[0]['size_total'] - mount[0]['size_available'] }}"
- set_fact:
disk_usage_ratio: "{{ disk_usage|float / mount[0]['size_total'] }}"
- set_fact:
disk_usage_s: "{{ (disk_usage|float / 1000000000) | round(1, 'common') }}GB"
- name: Set Fact
ansible.builtin.set_fact:
disk_usage_ratio: "{{ disk_usage | float / mount[0]['size_total'] }}"
- name: Set Fact
ansible.builtin.set_fact:
disk_usage_s: "{{ (disk_usage | float / 1000000000) | round(1, 'common') }}GB"
disk_total_s: "{{ (mount[0]['size_total'] / 1000000000) | round(1, 'common') }}GB"
disk_usage_ratio_s: "{{ 100 * (disk_usage_ratio|float) | round(1, 'common') }}%"
disk_limit_ratio_s: "{{ (100 * disk_limit|float) |round }}%"
disk_usage_ratio_s: "{{ 100 * (disk_usage_ratio | float) | round(1, 'common') }}%"
disk_limit_ratio_s: "{{ (100 * disk_limit | float) | round }}%"

- debug:
- name: Debug
ansible.builtin.debug:
msg: "fs {{ mount[0]['mount'] }} - disk usage {{ disk_usage_s }} of total {{ disk_total_s }} ({{ disk_usage_ratio_s }}) (should be within limit {{ disk_limit_ratio_s }})"
3 changes: 2 additions & 1 deletion roles/create_collection/meta/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
galaxy_info:
standalone: false # Part of a collection
author: MozeBaltyk
description: Role to initiate new Collections on a SCM (by default github)

Expand All @@ -15,7 +16,7 @@ galaxy_info:
# - CC-BY-4.0
license: Apache-2.0

min_ansible_version: 2.11
min_ansible_version: "2.12.0"

# If this a Container Enabled role, provide the minimum Ansible Container version.
# min_ansible_container_version:
Expand Down
20 changes: 13 additions & 7 deletions roles/create_collection/tasks/create.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
---
- name: Get your current ansible version
shell: ansible --version | head -1 | awk '{sub(/]/,"");print $NF}'
- name: Get your current ansible version # noqa: command-instead-of-module
ansible.builtin.shell: set -o pipefail && ansible --version | head -1 | awk '{sub(/]/,"");print $NF}'
args:
executable: /bin/bash
register: version
changed_when: false

- name: Set ansible_current_version
set_fact:
ansible.builtin.set_fact:
ansible_current_version: "{{ version['stdout'] }}"

- name: Initialize a new Collection
command: "ansible-galaxy collection init {{ collection_namespace }}.{{ collection_name }} --init-path ~/"
ansible.builtin.command: "ansible-galaxy collection init {{ collection_namespace }}.{{ collection_name }} --init-path ~/"
args:
creates: "~/{{ collection_namespace }}/{{ collection_name }}"

- name: Create complementary directories
file:
ansible.builtin.file:
path: '{{ item }}'
state: 'directory'
recurse: true
Expand All @@ -26,11 +29,13 @@
- '~/{{ collection_namespace }}/{{ collection_name }}/playbooks/templates'
- '~/{{ collection_namespace }}/{{ collection_name }}/playbooks/tasks'
- '~/{{ collection_namespace }}/{{ collection_name }}/scripts/prerequis'
- '~/{{ collection_namespace }}/{{ collection_name }}/meta/'

- name: Push templates inside collection
template:
ansible.builtin.template:
src: '{{ item.src }}'
dest: '{{ item.dest }}'
mode: u=rw,g=r,o=r
loop:
- { src: 'plugins/inventory/hosts.yml', dest: '~/{{ collection_namespace }}/{{ collection_name }}/plugins/inventory/hosts.yml' }
- { src: 'collections/pre-commit-config.yaml', dest: '~/{{ collection_namespace }}/{{ collection_name }}/.pre-commit-config.yaml' }
Expand All @@ -52,9 +57,10 @@
- { src: 'collections/meta/ee-requirements.yml', dest: '~/{{ collection_namespace }}/{{ collection_name }}/meta/ee-requirements.yml' }

- name: Push files inside collection
copy:
ansible.builtin.copy:
src: '{{ item.src }}'
dest: '{{ item.dest }}'
mode: u=rw,g=r,o=r
loop:
- { src: 'playbooks/ping.yml', dest: '~/{{ collection_namespace }}/{{ collection_name }}/playbooks/tasks/ping.yml' }
- { src: 'playbooks/test.yml', dest: '~/{{ collection_namespace }}/{{ collection_name }}/playbooks/tasks/test.yml' }
Expand Down
6 changes: 4 additions & 2 deletions roles/create_collection/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
---
# tasks file for createCollection
- import_tasks: prerequisites.yml
- name: Import prerequisites
ansible.builtin.import_tasks: prerequisites.yml

- import_tasks: create.yml
- name: Import create
ansible.builtin.import_tasks: create.yml
36 changes: 21 additions & 15 deletions roles/create_collection/tasks/prerequisites.yml
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
# Collection Description
- block:
- name: Prompt description
when: (collection_description is not defined) or (collection_description|length == 0)
run_once: true
block:
- name: Set description to ansible collection
pause:
ansible.builtin.pause:
prompt: "Please give a description to your collection"
register: prompt_description

- name: Set your description coming from prompt
set_fact:
ansible.builtin.set_fact:
collection_description: "{{ prompt_description['user_input'] }}"
when: (collection_description is not defined) or (collection_description|length == 0)
run_once: true

# Email Authors for collection
- block:
- name: Prompt email authors
when: (collection_author_email is not defined) or (collection_author_email|length == 0)
run_once: true
block:
- name: Give an email to ansible collection
pause:
ansible.builtin.pause:
prompt: "Please give an email that we can set as author of this collection (if empty, take as default from the git config global)"
register: prompt_email

- name: Set authors email
set_fact:
ansible.builtin.set_fact:
collection_author_email: "{{ prompt_email['user_input'] }}"
when: prompt_email['user_input']|length != 0

- block:
- name: Get User.Email from git global config to define authors of the collection
shell: "git config --global user.email"
- name: Set email authors
when: prompt_email['user_input']|length == 0
block:
- name: Get User.Email from git global config to define authors of the collection # noqa: command-instead-of-module
ansible.builtin.shell: "set -o pipefail && git config --global user.email"
args:
executable: /bin/bash
register: gitconfig_email
changed_when: false

- name: Set authors email from git config
set_fact:
ansible.builtin.set_fact:
collection_author_email: "{{ gitconfig_email['stdout'] }}"
when: prompt_email['user_input']|length == 0
when: (collection_author_email is not defined) or (collection_author_email|length == 0)
run_once: true
3 changes: 2 additions & 1 deletion roles/create_collection/tests/test.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
- hosts: localhost
- name: Test plays
hosts: localhost
remote_user: root
roles:
- create_collection
Loading

0 comments on commit 035b424

Please sign in to comment.