Handy Prompts
This is a collection of prompts I’ve written for projects in variouslanguages that I work with on a 0001-1-1 08:0:0 Author: techvomit.net(查看原文) 阅读量:0 收藏

This is a collection of prompts I’ve written for projects in various languages that I work with on a regular basis.

Setup

There are some tools that I use with these prompts that you may want to add to your system by running the following commands:

bashutils_url="https://raw.githubusercontent.com/l50/dotfiles/main/bashutils"

bashutils_path="/tmp/bashutils"

if [[ ! -f "${bashutils_path}" ]]; then
   curl -s "${bashutils_url}" -o "${bashutils_path}"
fi

source "${bashutils_path}"

Next, navigate to the directory of the project you want to work on and run the following command:

process_files_from_config $bashutils_path/files/file_patterns.conf

Using patterns specified in the file_patterns.conf file, this function will process files recursively from the current directory and list them in a format that lends itself to parsing and comprehension by ChatGPT. Lastly, it will output the final results to your clipboard using pbcopy for macOS or xclip for Linux.

Note: if you reboot your system, you’ll need to run this script again unless you update the $bashutils_path to a permanent location on disk.

Ansible Prompts

These prompts help fix linting violations, create molecule tests, and handle other common tasks in Ansible.


Fix Linting Violations

Prompt:


Fix each of these linting violations:

```bash
# Placeholder for linting violations - replace with the linting violations
you need to resolve in your ansible code.
# Example Linting Violations - Replace with your data
- no-changed-when: Commands should not change things if nothing needs doing.
- roles/asdf/molecule/default/verify.yml:47 Task/Handler: Check sourcing of
  asdf.sh in shell profile for each user
# End of Placeholder
```

in this ansible role:

```bash
# Placeholder for an Ansible Role - replace with your ansible code content,
# courtesy of process_files_from_config.
# End of Placeholder
```


Refactor Ansible Role

Prompt:



I just refactored my `user_setup` role to be easier to manage
and eliminated a lot of unnecessary code:

"""
./vars/main.yml:

```yaml
---
user_setup_install_packages:
  - bash
  - sudo
```

./tasks/main.yml:

```yaml
---
- name: Gather the list of unique shells to install
  ansible.builtin.set_fact:
    unique_shells: "{{ user_setup_default_users | map(attribute='shell') | unique | map('regex_replace', '^/bin/(.*)$', '\1') | list }}"
  tags: always
- name: Install base packages
  become: true
  ansible.builtin.package:
    name: "{{ user_setup_install_packages }}"
    state: present
    update_cache: true
  environment: "{{ (ansible_os_family == 'Debian') | ternary({'DEBIAN_FRONTEND': 'noninteractive'}, {}) }}"
  tags: packages
- name: Install user-specific shells
  become: true
  ansible.builtin.package:
    name: "{{ item }}"
    state: present
  loop: "{{ unique_shells }}"
  when: item != '' and item != 'bash'
  environment:
    DEBIAN_FRONTEND: "{{ 'noninteractive' if ansible_distribution == 'Debian' else '' }}"
  tags: shells
- name: Ensure groups exist for users
  become: true
  ansible.builtin.group:
    name: "{{ item.usergroup }}"
    state: present
  loop: "{{ user_setup_default_users }}"
  when: item.usergroup is defined and item.usergroup != ''
- name: Create users
  become: true
  ansible.builtin.user:
    name: "{{ item.username }}"
    group: "{{ item.usergroup }}"
    shell: "{{ item.shell }}"
  loop: "{{ user_setup_default_users }}"
- name: Provide sudoers access for relevant users in sudoers.d
  become: true
  ansible.builtin.copy:
    dest: "/etc/sudoers.d/{{ item.username }}"
    content: "{{ item.username }} ALL=(ALL:ALL) NOPASSWD:ALL
"
    validate: "visudo -cf %s"
    mode: "0440"
  when: item.sudo
  loop: "{{ user_setup_default_users }}"
```

./meta/main.yml:

```yaml
---
galaxy_info:
  role_name: user_setup
  author: Jayson Grace
  namespace: cowdogmoo
  description: Sets up user accounts with optional sudo privileges for Unix-like systems.
  company: CowDogMoo
  license: MIT
  min_ansible_version: "2.14"
  platforms:
    - name: Ubuntu
      versions:
        - all
    - name: Kali
      versions:
        - all
    - name: EL
      versions:
        - all
  galaxy_tags:
    - user
    - setup
dependencies: []
```

./defaults/main.yml:

```yaml
---
user_setup_default_username: "{{ ansible_distribution | lower }}"
user_setup_default_users:
  - username: "{{ user_setup_default_username }}"
    usergroup: "{{ user_setup_default_username }}"
    sudo: true
    shell: /bin/zsh
```

./molecule/default/verify.yml:

```yaml
---
- name: Verify
  hosts: all
  gather_facts: true
  tasks:
    - name: Include default variables
      ansible.builtin.include_vars:
        file: "../../defaults/main.yml"
    - name: Include user setup variables
      ansible.builtin.include_vars:
        file: "../../vars/main.yml"
    - name: Check user exists
      ansible.builtin.command: "id -un {{ item.username }}"
      register: user_check
      changed_when: false
      failed_when: user_check.rc != 0
      loop: "{{ user_setup_default_users }}"
      loop_control:
        label: "{{ item.username }}"
    - name: Check user is in sudoers.d
      ansible.builtin.stat:
        path: "/etc/sudoers.d/{{ item.username }}"
      register: sudoers_file_check
      changed_when: false
      failed_when: not sudoers_file_check.stat.exists
      loop: "{{ user_setup_default_users }}"
      when: item.sudo
      loop_control:
        label: "{{ item.username }}"
    - name: Assert user creation
      ansible.builtin.assert:
        that:
          - "'{{ item.username }}' in (user_check.results | map(attribute='stdout') | list)"
      loop: "{{ user_setup_default_users }}"
      loop_control:
        label: "{{ item.username }}"
```

./molecule/default/molecule.yml:

```yaml
---
dependency:
  name: galaxy
driver:
  name: docker
platforms:
  - name: ubuntu_user_setup
    image: "geerlingguy/docker-ubuntu2204-ansible:latest"

    command: ""
    volumes:
      - /sys/fs/cgroup:/sys/fs/cgroup:rw
    cgroupns_mode: host
    privileged: true
  - name: kali_user_setup
    image: cisagov/docker-kali-ansible:latest

    command: ""
    pre_build_image: true
    volumes:
      - /sys/fs/cgroup:/sys/fs/cgroup:rw
    cgroupns_mode: host
    privileged: true
  - name: redhat_user_setup
    image: "geerlingguy/docker-rockylinux9-ansible:latest"

    command: ""
    volumes:
      - /sys/fs/cgroup:/sys/fs/cgroup:rw
    cgroupns_mode: host
    privileged: true
provisioner:
  name: ansible
  playbooks:
    converge: ${MOLECULE_PLAYBOOK:-converge.yml}
verifier:
  name: ansible
```

./molecule/default/converge.yml:

```yaml
---
- name: Converge
  hosts: all
  roles:
    - role: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') | basename }}"
```

./molecule/default/inventory:

```yaml
localhost
```

"""

ASK: I need you to do the same thing for my ansible role. It will be provided
below in its entirety. If there is not an existing molecule directory in the
role, I will need you to create one using a similar format as the above example.

ANSIBLE ROLE IN NEED OF REFACTORING:

```bash
# Placeholder for an Ansible Role - replace with your ansible code content,
# courtesy of process_files_from_config.
# End of Placeholder
```


Create Molecule Tests

Prompt:


Please create a `verify.yml` for my ansible role that provides
comprehensive test coverage:

"""
./tasks/http_proxy.yml:

```yaml
---
- name: Manage K3s HTTP Proxy Configuration
  ansible.builtin.block:
    - name: Create k3s.service.d directory
      ansible.builtin.file:
        path: "{{ systemd_dir }}/k3s.service.d"
        owner: root
        group: root
        state: directory
        mode: "0755"
    - name: Deploy the K3s http_proxy configuration file
      ansible.builtin.template:
        src: "http_proxy.conf.j2"
        dest: "{{ systemd_dir }}/k3s.service.d/http_proxy.conf"
        owner: root
        group: root
        mode: "0755"
  when: proxy_env is defined
```

./tasks/main.yml:

```yaml
---
- name: Check for PXE-booted system and configure K3s
  block:
    - name: Check if system is PXE-booted
      ansible.builtin.command:
        cmd: cat /proc/cmdline
      register: boot_cmdline
      changed_when: false
      check_mode: false
    - name: Set fact for PXE-booted system
      ansible.builtin.set_fact:
        is_pxe_booted: "{{ 'root=/dev/nfs' in boot_cmdline.stdout }}"
      when: boot_cmdline.stdout is defined
    - name: Include http_proxy configuration tasks
      ansible.builtin.include_tasks: http_proxy.yml
    - name: Configure and manage the K3s service
      ansible.builtin.template:
        src: "k3s.service.j2"
        dest: "{{ systemd_dir }}/k3s-node.service"
        mode: "0755"
      ansible.builtin.systemd:
        name: k3s-node
        daemon_reload: true
        state: restarted
        enabled: true
```

./templates/http_proxy.conf.j2:

```bash
[Service]
Environment=HTTP_PROXY={{ proxy_env.HTTP_PROXY }}
Environment=HTTPS_PROXY={{ proxy_env.HTTPS_PROXY }}
Environment=NO_PROXY={{ proxy_env.NO_PROXY }}
```

./templates/k3s.service.j2:

```bash
[Unit]
Description=Lightweight Kubernetes
Documentation=https:
After=network-online.target
[Service]
Type=notify
ExecStartPre=-/sbin/modprobe br_netfilter
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/local/bin/k3s agent \
  --server https:
  {% if is_pxe_booted | default(false) %}--snapshotter native \
  {% endif %}--token {{ hostvars[groups[group_name_master | default('master')][0]]['token'] | default(k3s_token) }} \
  {{ extra_agent_args | default("") }}
KillMode=process
Delegate=yes
LimitNOFILE=1048576
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
TimeoutStartSec=0
Restart=always
RestartSec=5s
[Install]
WantedBy=multi-user.target
```

./molecule/default/molecule.yml:

```yaml
---
dependency:
  name: galaxy
driver:
  name: docker
platforms:
  - name: ubuntu_k3s
    image: "geerlingguy/docker-ubuntu2204-ansible:latest"
    volumes:
      - /sys/fs/cgroup:/sys/fs/cgroup:rw
    privileged: true
provisioner:
  name: ansible
verifier:
  name: ansible
```

./molecule/default/converge.yml:

```yaml
---
- name: Converge
  hosts: all
  roles:
    - role: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') | basename }}"
```

Please stick to this idiomatic style for the complete verify.yml file you
will provide me that is chalk full of reliable molecule tests for my input
ansible role:

```yaml
---
- name: Verify
  hosts: all
  gather_facts: true
  tasks:
    - name: Include default variables
      ansible.builtin.include_vars:
        file: "../../defaults/main.yml"
    - name: Include user setup variables
      ansible.builtin.include_vars:
        file: "../../vars/main.yml"
    - name: Check user exists
      ansible.builtin.command: "id -un {{ item.username }}"
      register: user_check
      changed_when: false
      failed_when: user_check.rc != 0
      loop: "{{ user_setup_default_users }}"
      loop_control:
        label: "{{ item.username }}"
    - name: Check user is in sudoers.d
      ansible.builtin.stat:
        path: "/etc/sudoers.d/{{ item.username }}"
      register: sudoers_file_check
      changed_when: false
      failed_when: not sudoers_file_check.stat.exists
      loop: "{{ user_setup_default_users }}"
      when: item.sudo
      loop_control:
        label: "{{ item.username }}"
    - name: Assert user creation
      ansible.builtin.assert:
        that:
          - "'{{ item.username }}' in (user_check.results | map(attribute='stdout') | list)"
      loop: "{{ user_setup_default_users }}"
      loop_control:
        label: "{{ item.username }}"
```


Git Prompts

These prompts assist in creating commit messages from diffs and handling other git-related tasks.


Create commit message from git diff

Prompt:


Write a commit message for me using this diff:

```bash
# Placeholder for git diff - replace with your git diff output
# End of Placeholder
```

Please adhere to this format for your commit:

Created new `asdf` role with tests and linting.

**Added:**

- Added automated documentation generation for magefile utilities
- Automated Release Playbook - Introduced `galaxy-deploy.yml`, an automated
  release playbook for publishing the collection to Ansible Galaxy.
- Molecule Workflow - Added a new GitHub Actions workflow `molecule.yaml` for
  running Molecule tests on pull requests and pushes.
- Renovate Bot Configuration - Updated Renovate Bot configurations to reflect
  the new repository structure and naming.
- `molecule` configuration - Added new `molecule` configuration for the `asdf`
  role to support local testing and verification.
- asdf role - Added a new `asdf` role with enhanced functionality including
  OS-specific setup. Updated metadata and created new documentation under
  `roles/asdf/README.md` detailing role usage and variables.

**Changed:**

- GitHub Actions Workflows - Refactored the `release.yaml` workflow to align
  with Ansible collection standards, including updating working directory
  paths, setting up Python, installing dependencies, and automating the release
  to Ansible Galaxy.
- Pre-commit hooks - Added new pre-commit hooks for shell script validation and
  formatting.
- Refactored Ansible linting configuration - Moved the `.ansible-lint`
  configuration to `.ansible-lint.yaml` and adjusted linting rules.
  Also, added `mdstyle.rb` and `.mdlrc` for markdown linting configurations.
- Repository Metadata - Updated repository links in `README.md` and
  `galaxy.yml` to reflect the new repository naming and structure.
- Upgrade dependencies - Upgraded versions of pre-commit hooks and dependencies
  in `.pre-commit-config.yaml`, updated mage's `go.sum` to reflect the new
  dependency tree, and removed unused dependencies from mage's `go.sum`.

**Removed:**

- Removed old files in preparation for later refactoring.
- Windows Support for asdf role - Removed Windows support
  from `roles/asdf/README.md` as it is not supported in the tasks.

Keep your answer to 80 characters max per line!

文章来源: https://techvomit.net/handy-prompts/
如有侵权请联系:admin#unsafe.sh