Monday, December 04, 2023

Running `go` From a Docker Container

 I followed instructions I found here: https://hub.docker.com/_/golang

% cat Dockerfile

FROM golang:1.21
WORKDIR /usr/src/app
# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
COPY go.mod go.sum ./
RUN go mod download && go mod verify
COPY . .
RUN go build -v -o /usr/local/bin/app ./...
CMD ["app"]

You can use it like this ...

% cat hello.go
package main
import ("fmt")

func main() {
  fmt.Println("Hello World!")
}

% docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp golang:1.21 go build /usr/src/myapp/hello.go

% docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp golang:1.21 /usr/src/myapp/hello
Hello World!


Saturday, August 12, 2023

Ansible Unit Test Molecule Invert Failure into Success

 This is the use case where you are expecting a task to fail during the unit test.  There are cases where you would want to test for this.  Use your imagination. 


Anyways add this to the end of your molecule/*/converge.yml file:

---
- name: Converge
  hosts: all
  vars:

    ... omitted ...

    task_that_should_fail: 'Check if web server returns 200 on localhost'
  tasks:

    ... omitted ...

    - name: Role Should Fail In This Test
      block:
        - name: Include my_role
          ansible.builtin.include_role:
            name: "my_role"
          ignore_errors: true

        - name: Passed is Fail
          fail:
            msg: "Role my_role unexpectedly successed!"

      rescue:
        - name: Failed is Pass
          debug:
            msg: "Role my_role failed when it should have!"
          when:
            - ansible_failed_task.name == task_that_should_fail

        - name: Passed is Fail
          fail:
            msg: "Role my_role unexpectedly succeed!"
          when:
            - not ansible_failed_task.name == task_that_should_fail

Wednesday, August 02, 2023

How to Add the Current Time to the Results of Your MySQL Select Statements

 MariaDB [xx_config]> select *, NOW() from servers where id = 'Fakeuuid-kinda'; 
| ID              |  VERSION |  NOW()    
| Thing One | Thing Two  | 2023-08-03 02:57:44 

Tuesday, August 01, 2023

What Data Directory is MariaDB 10.6 using?

 Run this SQL query ...

root@xoubdmz1:~ # mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 587
Server version: 10.6.14-MariaDB-1:10.6.14+maria~ubu2204-log mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show variables like 'datadir';
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| datadir       | /var/lib/mysql/ |
+---------------+-----------------+
1 row in set (0.001 sec)

MariaDB [(none)]>

Friday, June 16, 2023

vi mk%y'k

Copy lines of text between '{}'s 

Give this text file contents:

  1
  2 ; <<>> DiG 9.10.6 <<>> download.fedoraproject.org
  3 ;; global options: +cmd
  4 ;; Got answer:
  5 ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 52209
  6 ;; flags: qr rd ra; QUERY: 1, ANSWER: 10, AUTHORITY: 0, ADDITIONAL: 1
  7
  8 ;; OPT PSEUDOSECTION:
  9 ; EDNS: version: 0, flags:; udp: 512
 10 ;; QUESTION SECTION:
 11 ;download.fedoraproject.org.    IN      A
 12 {
 13 ;; ANSWER SECTION:
 14 download.fedoraproject.org. 300 IN      CNAME   wildcard.fedoraproject.org.
 15 wildcard.fedoraproject.org. 22  IN      A       38.145.60.20
 16 wildcard.fedoraproject.org. 22  IN      A       152.19.134.142
 17 wildcard.fedoraproject.org. 22  IN      A       67.219.144.68
 18 wildcard.fedoraproject.org. 22  IN      A       8.43.85.67
 19 wildcard.fedoraproject.org. 22  IN      A       38.145.60.21
 20 wildcard.fedoraproject.org. 22  IN      A       152.19.134.198
 21 wildcard.fedoraproject.org. 22  IN      A       8.43.85.73
 22 wildcard.fedoraproject.org. 22  IN      A       140.211.169.196
 23 wildcard.fedoraproject.org. 22  IN      A       34.221.3.152
 24 }
 25 (
 26 ;; Query time: 95 msec
 27 ;; SERVER: 2600:6c52:763f:e773:1691:82ff:fe86:3046#53(2600:6c52:763f:e773:1691:82ff:fe86:3046)
 28 ;; WHEN: Fri Jun 16 21:31:33 PDT 2023
 29 ;; MSG SIZE  rcvd: 222
 30 )

Move the cursor to the '{' on line 12. (:12<return>) then type: mk%y'k then move the cursor to line 7 and type: p and the file should look like this:

  1
  2 ; <<>> DiG 9.10.6 <<>> download.fedoraproject.org
  3 ;; global options: +cmd
  4 ;; Got answer:
  5 ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 52209
  6 ;; flags: qr rd ra; QUERY: 1, ANSWER: 10, AUTHORITY: 0, ADDITIONAL: 1
  7 {
  8 ;; ANSWER SECTION:
  9 download.fedoraproject.org. 300 IN      CNAME   wildcard.fedoraproject.org.
 10 wildcard.fedoraproject.org. 22  IN      A       38.145.60.20
 11 wildcard.fedoraproject.org. 22  IN      A       152.19.134.142
 12 wildcard.fedoraproject.org. 22  IN      A       67.219.144.68
 13 wildcard.fedoraproject.org. 22  IN      A       8.43.85.67
 14 wildcard.fedoraproject.org. 22  IN      A       38.145.60.21
 15 wildcard.fedoraproject.org. 22  IN      A       152.19.134.198
 16 wildcard.fedoraproject.org. 22  IN      A       8.43.85.73
 17 wildcard.fedoraproject.org. 22  IN      A       140.211.169.196
 18 wildcard.fedoraproject.org. 22  IN      A       34.221.3.152
 19 }
 20
 21 ;; OPT PSEUDOSECTION:
 22 ; EDNS: version: 0, flags:; udp: 512
 23 ;; QUESTION SECTION:
 24 ;download.fedoraproject.org.    IN      A
 25 {
 26 ;; ANSWER SECTION:
 27 download.fedoraproject.org. 300 IN      CNAME   wildcard.fedoraproject.org.
 28 wildcard.fedoraproject.org. 22  IN      A       38.145.60.20
 29 wildcard.fedoraproject.org. 22  IN      A       152.19.134.142
 30 wildcard.fedoraproject.org. 22  IN      A       67.219.144.68
 31 wildcard.fedoraproject.org. 22  IN      A       8.43.85.67
 32 wildcard.fedoraproject.org. 22  IN      A       38.145.60.21
 33 wildcard.fedoraproject.org. 22  IN      A       152.19.134.198
 34 wildcard.fedoraproject.org. 22  IN      A       8.43.85.73
 35 wildcard.fedoraproject.org. 22  IN      A       140.211.169.196
 36 wildcard.fedoraproject.org. 22  IN      A       34.221.3.152
 37 }
 38 (
 39 ;; Query time: 95 msec
 40 ;; SERVER: 2600:6c52:763f:e773:1691:82ff:fe86:3046#53(2600:6c52:763f:e773:1691:82ff:fe86:3046)
...


Wednesday, June 07, 2023

Remove last column in bash

awk -F'.' 'sub(FS $NF,x)' file

bash-3.2$ cat file
MOR22A2.S4000011.h23v22.061.2023063111017.hdf
MOR22A2.S4000011.h23v44.061.2023061111033.hdf
AST006_S003.zip
AST003_S066.zip
bash-3.2$ awk -F'.' 'sub(FS $NF,x)' file
MOR22A2.S4000011.h23v22.061.2023063111017
MOR22A2.S4000011.h23v44.061.2023061111033
AST006_S003
AST003_S066

Wednesday, May 03, 2023

Ansible code to update AWX Ansible Inventory

Ansible code to update AWX Ansible Inventory

Here is the playbook:

---
- name: Update Inventory
  hosts: localhost
  gather_facts: False   roles:
    - role: roles/update_inventor
y

localhost is used for hosts: because the AWX REST API endpoints are accessible from the Ansible master we are running on.


Use that playbook in an AWX Job Template

  • The job template needs to prompt the user for "Inventory" on launch.
Respond with the inventory the hosts you wish to update are in. This will provide the value to the special AWX variable tower_inventory_id. 
  • The job template needs to prompt the user for "Variables" on launch.
Example:
---
update_ati_fqdns:
- host1.example.com
- host2.example.com
ansible_connection: 'local'
  • You will need a survey for your Job Template
    • Admin Username: admin_username
    • Admin User Password: admin_password
    • URL for AWX REST API GROUP endpoint: http://awx.example.com/api/v2/groups
    • update_ati_group: integer
    • Action: pulldown [REMOVE_FROM_GROUP, ADD_TO_GROUP]

Here is the role:


defaults/main.yml --- awx_base_url: "https://awx.example.com"
tasks/main.yml --- - name:   ansible.builtin.include_tasks: test_block_loop.yml   loop: "{{ update_ati_fqdns }}"

tasks/test_block_loop.yml

--- - name: Get Host ID   uri:     url: "{{ awx_base_url }}/api/v2/inventories/{{ tower_inventory_id }}/hosts/?name={{ item }}"     method: GET     user: "{{ admin_username }}"     password: "{{ admin_password }}"     force_basic_auth: yes     validate_certs: no     headers:       Content-Type: "application/json"   register: host_result   failed_when: host_result.status != 200 or host_result.json.count != 1 - name: "Set payload for add."   set_fact:     body: "{ \"id\" : {{ host_result.json.results[0].id | int }} }" - name: "Set payload for disassociate/remove."   set_fact:     body: "{ \"disassociate\" : {{ host_result.json.results[0].id | int }}, \"id\" : {{ host_result.json.results[0].id | int }} }"   when: selected_action == "REMOVE_FROM_GROUP" - name: Update Ansible Tower inventory   uri:     url: "{{ update_ati_groups_api }}/{{ update_ati_group }}/hosts/"     user: "{{ admin_username }}"     password: "{{ admin_password }}"     method: "POST"     body: "{{ body }}"     force_basic_auth: yes     status_code: 204     body_format: json     validate_certs: no

Wednesday, March 22, 2023

Get process ids and command on Linux without `ps` command

bash-4.4$ for exe in /proc/*/exe; do ls -l $exe; done
lrwxrwxrwx 1 runner root 0 Mar 22 22:47 /proc/1/exe -> /usr/local/bin/dumb-init
lrwxrwxrwx 1 runner root 0 Mar 22 22:47 /proc/1039/exe -> /usr/bin/vi
lrwxrwxrwx 1 runner root 0 Mar 16 03:15 /proc/143/exe -> /usr/bin/bash
lrwxrwxrwx 1 runner root 0 Mar 17 22:44 /proc/1685/exe -> /usr/bin/bash
lrwxrwxrwx 1 runner root 0 Mar 16 03:04 /proc/17/exe -> /usr/bin/receptor
lrwxrwxrwx 1 runner root 0 Mar 18 03:21 /proc/1788/exe -> /usr/bin/bash
lrwxrwxrwx 1 runner root 0 Mar 22 22:45 /proc/2137/exe -> /usr/bin/bash
lrwxrwxrwx 1 runner root 0 Mar 22 22:47 /proc/self/exe -> /usr/bin/coreutils
lrwxrwxrwx 1 runner root 0 Mar 22 22:47 /proc/thread-self/exe -> /usr/bin/coreutils