Outils pour utilisateurs

Outils du site


blog

Notes JSON et YAML

jc JSON Convert JSONifies the output of many CLI tools and file-types
jq Command-line JSON processor
json_pp JSON Pretty Printer
JMESPath JMESPath is a query language for JSON (utilisé par json_query d'Ansible
jp The jp command is a command line interface to JMESPath

Voir :

  • JSONPath et go_template
  • JSON RFC 6902 patch

Voir Ansible :

Voir aussi :

sudo apt-get install jc
dig www.google.com | jc --dig -p
[
  {
    "id": 17598,
    "opcode": "QUERY",
    "status": "NOERROR",
    "flags": [
      "qr",
      "rd",
      "ra"
    ],
    "query_num": 1,
    "answer_num": 1,
    "authority_num": 0,
    "additional_num": 1,
    "opt_pseudosection": {
      "edns": {
        "version": 0,
        "flags": [],
        "udp": 4096
      }
    },
    "question": {
      "name": "www.google.com.",
      "class": "IN",
      "type": "A"
    },
    "answer": [
      {
        "name": "www.google.com.",
        "class": "IN",
        "type": "A",
        "ttl": 38,
        "data": "142.250.178.132"
      }
    ],
    "query_time": 8,
    "server": "208.67.222.123#53(208.67.222.123) (UDP)",
    "when": "Sun Oct 16 21:54:04 CEST 2022",
    "rcvd": 59,
    "when_epoch": 1665950044,
    "when_epoch_utc": null
  }
]
cat fic.json |python3 -m json.tool |native2ascii -encoding UTF-8 -reverse

Extension Firefox http://jsonview.com/

sudo apt-get install yajl-tools

yajl-tools

  • json_reformat
  • json_verify
mongoimport --host localhost --db database --collection collection <fichier.json --jsonArray

Convert JSON to YAML

yq -P sample.json

Ou encore

python -c 'import sys, yaml, json; yaml.safe_dump(json.load(sys.stdin), sys.stdout, default_flow_style=False)' < /opt/stack/keystone/etc/policy.v3cloudsample.json > /etc/keystone/policy.yaml
pip install jsbeautifier
js-beautify file.js

Dico vers JSON ?

cat plop.t |tr \' \" |sed -e 's/None/""/g' |jq .

VIM - Formater le JSON

:%!python -m json.tool

Requête (Query)

Avec curl

curl -k https://aap-controller.local/api/v2/users/27/roles/ -X POST -u user:password --data-raw '{"id":534}'

Avec jq voir : https://hyperpolyglot.org/json

Top level sections

cat single-instance.json |jq 'keys[]'

Exemple wildcard

 
$ curl -s -u "${AAP_USER}:${AAP_PASS}" "https://aap.acme.local/api/v2/job_templates/" | jq '.results[0].id'
1680

$ curl -s -u "${AAP_USER}:${AAP_PASS}" "https://aap.acme.local/api/v2/job_templates/" | jq '.results[].id' | tail -3
1572
1569
2343

jq retours sans guillemet (quote) :

jq -r
docker inspect portainer/portainer-ce |jq '.[0].RepoTags'
ansible -i testsrv, -m setup all |sed -e 's/^.*=>//' |jq -r '.ansible_facts.ansible_mounts[] |.mount'
Filtres

Conversion string integer avec tonumber

Exemple

jq '.blockdevices[].size | tonumber'

Autres

Ansible - Utilisation de boolean en extra vars via JSON
ansible-playbook playbook.yml -i inventory.ini -e '{ "remove_vhost": false }'

YAML

yq portable command-line YAML processor
yb YAML Parser in bash

Voir :

Contrôle syntaxe :

Ce que jq est à json, yq l'est pour le yaml.

yq permet également de convertir des fichiers JSON en YAML

cat .kube/config |egrep -v "\-data"  |yq .
# pip install --user yq
sudo apt-get install yq
openstack image show IMAGE1 -c properties -f yaml |yq '.properties.direct_url'

Filtrer sur les clefs (keys)

cat meta/runtime.yml | yq '.action_groups | keys'
# Ou
cat meta/runtime.yml | yq -r '.action_groups | keys[]'

Mofifier un fichier YAML

avec yq :

En Python et autres :

Avec Ansible : https://github.com/kwoodson/ansible-role-yedit

Exemple non-specific tag

configuration: !include config.d/*.yaml

Reférences

vars:
  webapp:
      version: &my_version 1.0
      custom_name:
          - "ToDo_App"
          - *my_version
Exemple Python
import yaml
 
with open("filter.yml", "r") as yamlfile:
    #filters = yaml.load(yamlfile, Loader=yaml.FullLoader)
 
    filters = yaml.safe_load(yamlfile)

Échapper certains caractères

Passer un fichier YAML à une API REST qui fonctionne en JSON
cat inv.yaml |sed -e 's/$/\\n/' -e 's/"/\\"/g' |tr -d '\n'
Exemple YAML vers JSON

inv.yaml

---
server1:
  hosts:
    server1.inf.acme.lan:
  vars:
    SET_LINGERING_LIST:
      - SET_LINGERING_USER: testplop
        SET_LINGERING_ENABLED: "false"
      - SET_LINGERING_USER: testplop
        SET_LINGERING_ENABLED: "true"

type_host:
  hosts:
    server1.inf.acme.lan: {}
  vars:
    type: host
#! /bin/bash
set -euo pipefail
 
YAML="$(cat inv.yaml |sed -e 's/$/\\n/' -e 's/"/\\"/g' |tr -d '\n')"
 
curl -v -k -u user1:'P@ssw0rd' -H 'Content-Type: application/json' -X POST https://awx.acme.fr/api/v2/job_templates/81/launch/ -d '{
"extra_vars": {
"foo1": "bar1",
"foo2": "bar2",
"inventory_content": "'"${YAML}"'"
 
}
}'

Ou avec un heredoc

YAML="$(cat <<'EOF' | sed -e 's/$/\\n/' -e 's/"/\\"/g' |tr -d '\n'
---
server1:
  hosts:
    server1.inf.acme.lan:
  vars:
    SET_LINGERING_LIST:
      - SET_LINGERING_USER: testplop
        SET_LINGERING_ENABLED: "false"
      - SET_LINGERING_USER: testplop
        SET_LINGERING_ENABLED: "true"
 
type_host:
  hosts:
    server1.inf.acme.lan: {}
  vars:
    type: host
EOF
)"
En convertissant le YAML en JSON

En passant un JSON dans un JSON en string

#JS="$(ansible-inventory -i inv.yaml --list)"
JS="$(cat inv.js)"
 
JS="$(echo $JS |sed 's/"/\\"/g')"
URL=''
 
curl -v -k -u user1:'P@ssw0rd' -H 'Content-Type: application/json' -X POST https://awx.acme.fr/api/v2/job_templates/81/launch/ -d '{
"extra_vars": {
"foo1": "bar1",
"foo2": "bar2",
"inventory_content": "'"${JS}"'"
 
}
}'
Exemple de yaml
CRON_HOST_TARGETS:
  all:
    - CRON_FILENAME: crontab_app1
      CRON_USER: app1
      CRONS:
        - DESCRIPTION: "topmem.sh exécuté toutes les heures"
	        SCHED: "0 * * * *"
	        CMDLINE: "~/scripts/topmem.sh >> ~app1/logs/topmem.out 2>&1"

  sc01:
    - CRON_FILENAME: crontab_app1_sc01
      CRON_USER: app1
      CRONS:
        - DESCRIPTION: "archivage de certains fichiers applicatifs"
	        SCHED: "06 03 * * *"
	        CMDLINE: ". ~/scripts/archive_plopsc.sh -l${LINE} >> ~app1/logs/archive_sc_${LINE}.out 2>&1"

        - DESCRIPTION: Performances
	        SCHED: "05 03 * * *"
	        CMDLINE: ". ~/scripts/appexec.sh appenv.sh ${LINE} > /dev/null; ~/scripts/stat_report_run.sh ${LINE} > /dev/null"

Pb

Compile Error - Échapper certains caractères

plop.yml

rec:plop:
  stage: plop
  variables:
    ENV: "RECETTE"

prd:plop:
  stage: plop
  variables:
    ENV: "PRODUCTION"
$ cat plop.yml |yq '.rec:plop'
jq: error: syntax error, unexpected ':', expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
.rec:plop    
jq: 1 compile error
Solution
cat plop.yml |yq '.["rec:plop"]'
2025/03/24 15:06

Notes Jenkins

Serveur

# podman run -p 8080:8080 -p 50000:50000 --restart=on-failure -v jenkins_home:/var/jenkins_home docker.io/jenkins/jenkins:lts
podman run -p 8080:8080 -p 50000:50000 --restart=on-failure -v jenkins_home:/var/jenkins_home docker.io/jenkins/jenkins:lts-jdk21
 
podman exec -l cat /var/jenkins_home/secrets/initialAdminPassword

Plugins :

  • Role-based Authorization Strategy
  • Pipeline

Recommandés :

  • Folders
  • OWASP Markup Formatter
  • Build Timeout
  • Credentials Binding
  • Timestamper
  • Workspace Cleanup
  • Ant
  • Gradle
  • Pipeline
  • GitHub Branch Source
  • Pipeline: GitHub Groovy Libraries
  • Pipeline Graph View
  • Git
  • SSH Build Agents
  • Matrix Authorization Strategy
  • LDAP
  • Email Extension
  • Mailer
  • Dark Theme
Pb Erreur Form too large 208753>200000
Solution

Ajouter l'argument ci-dessous dans le fichier jenkins.xml :

-Dorg.eclipse.jetty.server.Request.maxFormContentSize=5000000

API

read -s MDP
# curl -u admin:"$MDP" -s http://localhost:8080/api/json | python3 -m json.tool
curl -u admin:"$MDP" -s http://localhost:8080/api/json | jq .

CLI

wget http://jenkins:8080/jnlpJars/jenkins-cli.jar
java -jar jenkins-cli.jar -s http://localhost:8080/ help

Salve

wget http://jenkins:8080/jnlpJars/agent.jar
java -jar agent.jar  -jnlpUrl http://jenkins:8080/computer/vmdeb1/slave-agent.jnlp -jnlpCredentials user:password

Pipeline

A Jenkinsfile is written using a Groovy-based DSL (Domain-Specific Language) and can be structured in two ways:

  • Declarative Pipeline (recommended for most users)
  • Scripted Pipeline
2025/03/24 15:06

Notes Java JVM

Notes dev Java

Quarkus

Voir :

Voir aussi :

Le FrameWork Quarkus pour développer en Java pour du K8B / Docker

La compilation est plus “complète”, donc plus de temps de compiliation, mais un temps d'execution / démarrage réduit. Permet le Serverless. La sortie de la compilation est un binaire, plus un jar. Cela réduit l'emprunte mémoire.

Install du JRE Java 8 sur Debian 7

mkdir /opt/
cd /opt/
tar xvf /root/jre-8u161-linux-x64.tar.gz
 
ln -s /opt/jre1.8.0_161 /opt/jre
 
cat >> /etc/environment <<EOF
JAVA_HOME="/opt/jre"
EOF
 
update-alternatives --install /usr/bin/java java /opt/jre/bin/java 1
update-alternatives --set java /opt/jre/bin/java
 
java -version

Lib utilisée

Savoir si la lib log4j est utilisée

for pid in $(pidof java);do cat /proc/$pid/cmdline|xargs -0 && cat /proc/$pid/maps |grep log4j;done

Notes

Defaut Max Heap Size

java -XX:+PrintFlagsFinal -version 2>&1 | grep MaxHeapSize

Voir :

  • jps
  • jstat
  • jconsole
jstatd.all.policy
grant codebase "file:${java.home}/../lib/tools.jar" {
  permission java.security.AllPermission;
};

JVM OPT_ARGS

java --illegal-access=<paramater> -jar plop.jar

parameter :

  • permit
  • warn
  • debug
  • deny
Autres
  • JAVA_MAX_MEM_RATIO
  • -XX:MaxRAMPercentage
  • JAVA_MAX_MEM_RATIO=80.0
2025/03/24 15:06

Bash astuces

Check syntax avec shellcheck

Voir aussi :

shellcheck monscript.sh

Si faux positif

# shellcheck disable=SC2086
rsync $RSYNC_OPT "${PART}/" "${PART_TMP}/"
$ shellcheck plop.sh
 
In mkiso-debian.sh line 56:
        source "$FIC_PROPERTIES"
        ^-- SC1090: Can't follow non-constant source. Use a directive to specify location.

Solution

Ajouter en commentaire shellcheck source=

plop.sh

# shellcheck source=vars/vm-deb10.var
source "$FIC_PROPERTIES"

puis

shellcheck -x plop.sh

.shellcheckrc

external-sources=true
shell=bash
color=always

Variables

Alternative à eval pour les variables Meta variables

$ a=1
$ b=2
$ meta_var="a"
$ echo ${!meta_var}
1

Appel de variable

$ B=2
$ KEY=B
$ echo ${KEY}
B
$ echo ${!KEY}
2

Déclaration / affectation de variables dynamiques

$ declare $KEY=12
$ echo ${!KEY}
12

Ou encore

K=V
 
declare -n V2=K
# Ce qui revient à :
# V2="$K"
 
$ LETTRE=ALPHA
$ ALPHA=A
$ declare -n PLOP=$LETTRE
$ echo $PLOP
A
Linter

shfmt https://github.com/mvdan/sh

~/go/bin/shfmt -i 4 -s -w plop.sh

Auto indent vim

:set expandtab ts=4 sw=4 ai
:retab

bashate

pip3 install bashate
 
bashate file.sh
bashate -i E010,E011 file.sh file2.sh
Autres

/etc/skel/.bash_logout

/usr/bin/clear
2025/03/24 15:06

Notes iTop ITSM ESM CMDB ITIL

Voir :

Voir aussi :

sudo docker run -d -p 8000:80 --name=my-itop vbkunin/itop
2025/03/24 15:06
blog.txt · Dernière modification : de 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki