Supprimer Rendre public Rendre privé Add tags Delete tags
  Ajouter un tag   Annuler
  Supprimer le tag   Annuler
  • • DevOps notes •
  •  
  • Tags
  • Connexion

Functions, Docstrings/shaare/r7T2Ww

  • python
  • python

Functions

Functions package reusable code into named blocks, improving modularity, readability, and testability. They prevent duplication (DRY) and make scripts easier to maintain.

Defining a Function (def)

Use def name(params): followed by an indented block. An optional """docstring""" explains purpose, parameters, and return value.

def greet_user(name):
    """Greets the user by name.

    Args:
        name (str): The user to greet
    """

    print(f"Hello, {name}!")

greet_user("Alice")

Calling a Function

Invoke via name(args). Control jumps into the function body and (optionally) returns a value back.

import random

def random_number(min_val, max_val):
    """Generates an integer between min_val and max_val

    Args:
        min_val (int): The lower boundary of the interval
        max_val (int): The upper boundary of the interval

    Returns:
        int: The generated random number
    """
    return random.randint(min_val, max_val)

generated_number = random_number(0, 10)
print(f"Generated number: {generated_number}")

Parameters vs Arguments

Summary: Parameters are named in the def signature; arguments are the actual values passed when calling.

generated_number = random_number(-1, 100)

Positional vs Keyword Arguments

Positional args match by order; keyword args match by name and can be out of order. Positional arguments must come first.

def check_service_status(service_name, expected_status):
    print(f"Checking {service_name} for {expected_status}...")
    return True

check_service_status("nginx", "running")
check_service_status("running", "nginx")

check_service_status(service_name="nginx", expected_status="running")
check_service_status(expected_status="running", service_name="nginx")

# Positional arguments must come before keyword arguments
# check_service_status(service_name="nginx", "running") # Uncommenting raises a SyntaxError

Default Parameter Values

It's possible to give parameters default values in the signature (param=default), making them optional.

def connect(host, port=22, timeout=30):
    print(f"Connect to host {host} on port {port} (timeout {timeout})")

connect("web01")
connect("web02", 443, 60)

# When wanting to set the value of timeout but use the default value of port
# We need to use keyword arguments, since positional arguments would be
# incorrectly mapped

# Bad exaple - see how port is set to 60 and timeout remains 30
connect("web03", 60)

# Good example - both values are set as we expect
connect("web03", timeout=60)

Docstrings – Documenting Functions

The first string in a function is its docstring, explaining purpose, Args: and Returns:. Used by help() and IDEs. Observing the following conventions is considered good practice:

  1. One-line summary
  2. Blank line
  3. Detailed description (optional)
  4. Args: section for parameters
  5. Returns: section for return values
  6. Raises: section for exceptions
import socket

def check_port(host, port, timeout=5):
    """Checks if a TCP port is open on a given host.

    Args:
        host (str): Hostname or IP address.
        port (int): TCP port number.
        timeout (int, option): Connection timeout in seconds. Defaults to 5.

    Returns:
        bool: True if the port is open, False otherwise.
    """

    try:
        with socket.create_connection((host, port), timeout):
            return True
    except Exception:
        return False

print(check_port("www.google.com", 443))

# Port 22 is not open, should return False
print(check_port("www.google.com", 22))

# Host does not exist, should return False
print(check_port("www.afbdoaubfdoabdfoubaf.com", 22))
3 months ago Permalien
cluster icon
  • Dictionaries : Dictionaries (dict) Dictionaries are mutable, insertion-ordered collections of key-value pairs. Keys must be unique and immutable; values can be of an...
  • Typing : Introduction Python is a dynamically typed language, meaning you can assign values to variables without declaring their types, and type checking happ...
  • Regex : Regex Essentials: Overview Regular expressions (regex) are a language for defining text search patterns. Python’s re module provides functions like...
  • Editable Installs with pyproject.toml : Editable Installs with pyproject.toml The Python interpreter doesn't automatically know about our project's structure. The modern and most robust solu...
  • Temporary Files and Directories : Temporary Files and Directories Automation scripts often need scratch space for intermediate data without cluttering the filesystem or risking name c...

Concise Iteration: List Comprehensions/shaare/dAsQzQ

  • python
  • python

Concise Iteration: List Comprehensions

Simple for loops to create lists can be verbose. We can leverage list comprehensions to define the list contents directly within square brackets, obtaining a more compact syntax.

# Example: Double items using a for loop
old_items = [1, 2, 3, 4]
doubled_items = []

for item in old_items:
    doubled_items.append(item * 2)

print(doubled_items)

# Example: Double items using list comprehension
doubled_items_with_comprehension = [item * 2 for item in old_items]
print(doubled_items_with_comprehension)

List Comprehension Syntax

  • Syntax: [<expression> for <item> in <iterable>]
  • [] indicates a new list is created eagerly.
  • <expression> is applied to each item.
  • for <item> in <iterable> defines the loop.
servers = ["web", "db", "backend"]
uppercase_servers = [server.upper() for server in servers]
print(uppercase_servers)

Filtering with if in Comprehensions

  • Purpose: Include only items meeting a condition.
  • Syntax: [<expression> for <item> in <iterable> if <condition>].
  • The condition filters items before expression is evaluated.
numbers = [1, 5, 10, 8, 2, 15]
even_numbers = [num + 1 for num in numbers if num % 2 == 0]
print(even_numbers)

Set and Dictionary Comprehensions

  • Set comprehension uses {} and produces unique items.
  • Dictionary comprehension uses {key: value ...}.
  • Both evaluated eagerly like list comprehensions.
numbers = [1, 2, 3, 2, 4, 1, 3]
unique_squares = {x * x for x in numbers}
print(unique_squares)

servers = ["web", "backend"]
server_ips = {server: f"192.168.1.{i}" for i, server in enumerate(servers)}
print(server_ips)

Conditional Expression (Ternary Operator)

  • Purpose: Apply different expressions based on a condition within the comprehension.
  • Syntax: <value_if_true> if <condition> else <value_if_false> inside the comprehension.
  • Places the ternary before the for clause.
numbers = [1, 5, 10, 8, 2, 15]
categories = ["PASS" if num >= 8 else "FAIL" for num in numbers]
print(categories)
3 months ago Permalien
cluster icon
  • Fixtures in Pytest : Fixtures in Pytest As tests grow more complex, repeating setup and cleanup steps makes tests harder to read and maintain. Pytest fixtures allow centr...
  • Logging to Files : Logging to Files Basic File Logging with FileHandler Use logging.FileHandler to write log records to a file. mode='a' (append) preserves existing log...
  • Generics typing : Introduction to Generics Generic types let you write reusable, type-safe functions and classes that work uniformly across different data types. They ...
  • Running External Commands with subprocess.run : Running External Commands with subprocess.run DevOps automation often requires invoking existing CLI tools or scripts to leverage their functionality...
  • Python package and subpackage : Introduction to Packages (__init__.py) What is a Package? A Python package provides a way to structure a project's module namespace by using directori...

For & While Loops/shaare/VAK_8g

  • python
  • python

For & While Loops

Python provides two main ways to repeat actions: for loops (for iterating over known sequences) and while loops (for repeating as long as a condition is true). These are essential for automating repetitive tasks in DevOps, such as processing lists of servers, retrying operations, or polling for status changes.

Automating Repetition: Loops

  • for loop: Iterates through each item in a known sequence (list, tuple, string, dictionary items, range, file lines). Best when you know the items to process.
  • while loop: Repeats as long as a condition remains True. Best when the number of repetitions isn't known beforehand, but a stopping condition is.

for Loops: Processing Each Item

for loops are used to process each item in a sequence. The loop variable takes on the value of each item, one at a time, and the indented block runs for each item.

servers = ["web01", "web02", "web03"]

for server in servers:
    print("Pinging server:", server)

for char in "SUCCESS":
    print(char)

for idx in range(10):
    print("Pinging server:", idx)

while Loops: Repeating While True

while loops repeat a block of code as long as a condition remains True. This is useful when you don't know in advance how many times you'll need to repeat the action.

connection_attempts = 0
max_attempts = 5
connected = False

while not connected and connection_attempts < max_attempts:
    print(f"Attempting to reach server: {connection_attempts + 1}")
    # Simulating for the purposes of demonstration - Succeeds on 4th attempt
    if connection_attempts == 3:
        connected = True

    connection_attempts += 1

if not connected:
    print("Failed to connect after maximum attempts.")

Important: The code inside the while loop must eventually make the condition False (e.g., by incrementing a counter or changing a flag), or you'll create an infinite loop.

Controlling Loop Flow: break and continue

  • break: Immediately exits the innermost loop. Useful when you've found what you need or hit an error.
  • continue: Skips the rest of the current iteration and moves to the next one. Useful for skipping items that don't meet criteria.
users = ["guest", "tester", "admin01", "admin02", "dev01"]
found_admin = None

for user in users:
    print(f"Checking user: {user}")
    if user.startswith("admin"):
        found_admin = user
        print(f"Admin user found: {found_admin}. Stopping search.")
        break
filenames = ["nginx.conf", "app.yaml", "db.yaml", "notes.txt"]

for file in filenames:
    if not file.endswith(".yaml"):
        print(f"Skipping non-yaml file: {file}") 
        continue
    print(f"Processing YAML config: {file}")
3 months ago Permalien
cluster icon
  • The Iteration Protocol : The Iteration Protocol We use for item in sequence: all the time. But how does Python get each item? Iterable: An object that can be looped over. It...
  • List : Lists (list) Lists are ordered, mutable sequences defined with square brackets []. You can add, remove, or change items after creation. Characteristic...
  • Working with YAML files : Working with YAML files YAML (“YAML Ain’t Markup Language”) focuses on human readability. Indentation replaces braces and brackets, comments are allo...
  • Context managers : Context Managers When opening files or acquiring locks, resources must be released even if errors occur. Manual try...finally ensures cleanup but a...
  • Working with Environment Variables : Working with Environment Variables Environment variables are dynamic, named values provided by the operating system to running processes, enabling co...

If / Elif / Else Logic/shaare/i3YPdw

  • python
  • python

If / Elif / Else Logic

Control the flow of scripts based on conditions using if, elif, and else.

The if Statement

An if statement executes a block of code only if a condition is True.

  • Syntax: if <condition>: followed by an indented block
  • Comparison operators: ==, !=, <, >, <=, >=, in
  • Combine conditions with and, or, not
server_status = "running"

if server_status == "running":
    print("Service is active.")

Truthiness

Python treats many values as truthy or falsy in conditionals.

  • Falsy: False, None, 0, 0.0, '', [], {}
  • Truthy: non-zero numbers, non-empty sequences/collections
servers = ["web01", "web02"]
error_message = ""
default_config = {}

if servers:
    print(f"Processing {len(servers)} servers.")
if error_message:
    print("Something went wrong:", error_message)
if not default_config:
    print("Default config not available, please provide the configuration values.")

The else statement

Use else to execute code when the if condition is false.

cpu_usage = 85.0

if cpu_usage > 90.0:
    print("ALERT: High CPU Usage")
else:
    print("CPU Usage is normal.")

The elif statement

Chain multiple checks; the first true block runs.

http_status = 503

if http_status == 200:
    print("Status OK")
elif http_status == 404:
    print("Resource not found")
elif http_status >= 500:
    print("Server error (5xx)")
else:
    print("Another status:", http_status)

Guard Clauses

Handle edge cases at the top of functions to avoid deep nesting of if conditions.

def process_data_guarded(data):
    if not data:
        print("No data provided")
    elif not isinstance(data, list):
        print(f"Invalid value type for 'data'. Provided {type(data)}; Required: list")
    else:
        print(f"Processing {len(data)} items...")
        print("Processed")

process_data_guarded(None)
process_data_guarded([])
process_data_guarded("abc")
process_data_guarded(10)
process_data_guarded([1, 2, 3])
3 months ago Permalien
cluster icon
  • Lambda Functions : Lambda Functions Python functions defined with def allow multiple statements, clear naming, and support for docstrings, making them ideal for complex...
  • Fixtures in Pytest : Fixtures in Pytest As tests grow more complex, repeating setup and cleanup steps makes tests harder to read and maintain. Pytest fixtures allow centr...
  • Functions: return vs yield : Functions: return vs yield Regular functions execute immediately, run to completion, and return a single value (or None). Generator functions retur...
  • Adding Tests to a Multi-File Project : Adding Tests to a Multi-File Project Standard Project Layout with Tests To maintain a clean and organized codebase, it is standard practice to separat...
  • Running Python modules : Running Scripts: python -m vs. python file.py The Core Difference: What is "Entry Point Zero"? The key to understanding the difference lies in the fir...

Dictionaries/shaare/xQcXjQ

  • python
  • python

Dictionaries (dict)

Dictionaries are mutable, insertion-ordered collections of key-value pairs. Keys must be unique and immutable; values can be of any type.

Characteristics and Use Cases

  • Insertion-ordered (Python 3.7+)
  • Mutable: add, remove, or change key-value pairs
  • Fast lookups by key
  • Ideal for configuration data, JSON-like structures, and lookups

Dictionary Operations Overview

Dictionaries in Python support a variety of operations for efficient data manipulation:

  • Length: Use len(my_dictionary) to get the number of key-value pairs.
  • Accessing Keys, Values, and Items: Use my_dictionary.keys(), my_dictionary.values(), and my_dictionary.items() to retrieve keys, values, or key-value pairs.
  • Membership Test: Check if a key exists using 'key' in my_dictionary.
  • Get with Default: Use my_dictionary.get('key', default) to safely retrieve a value with a fallback.
  • Setdefault: Add a key with a default value if it doesn't exist using my_dictionary.setdefault(key, default).
  • Pop and Popitem: Remove a specific key with my_dictionary.pop(key) or remove an arbitrary key-value pair with my_dictionary.popitem().
  • Merging: Combine dictionaries using the | operator (Python 3.9+) or update() method.
  • Fromkeys: Create a new dictionary with specified keys and a default value using dict.fromkeys(keys, value).
  • Clear: Remove all items from the dictionary with my_dictionary.clear().
my_dictionary = {'a': 1, 'b': 2, 'c': 3}
print(my_dictionary)

print(f"Length: {len(my_dictionary)}")

# Keys, Values, and Items
print(f"Keys: {my_dictionary.keys()}")
print(f"Values: {my_dictionary.values()}")
print(f"Items: {my_dictionary.items()}")

for item in my_dictionary.items():
    print(type(item))

for key, value in my_dictionary.items():
    print(f"- {key}: {value}")

# Membership test
print(f"'b' is in my_dictionary? {"b" in my_dictionary}")
print(f"'d' is in my_dictionary? {"d" in my_dictionary}")
print(f"1 is in my_dictionary? {1 in my_dictionary}")
print(f"1 is in values of my_dictionary? {1 in set(my_dictionary.values())}")

# Accessing elements
print("'b':", my_dictionary["b"]) # Will raise KeyError if key is not present in the dictionary
print("'b':", my_dictionary.get("b")) # Will not raise KeyError
print("'e' without default:", my_dictionary.get("e"))
print("'e' with default:", my_dictionary.get("e", -1))

my_dictionary.setdefault("d", 4)
print(my_dictionary)

# Removing elements
removed = my_dictionary.pop("a")
print(f"Removed value: {removed}")
removed = my_dictionary.popitem()
print(f"Removed value: {removed}")
removed = my_dictionary.popitem()
print(f"Removed value: {removed}")
# Merging of dictionaries
default_tags = {
    "Environment": "Production",
    "Owner": "Finance",
    "CostCenter": "10000"
}

custom_tags = {
    "CostCenter": "12345"
}

merged_tags = default_tags | custom_tags
print(merged_tags)
default_tags.update(custom_tags)
print(default_tags)

# Creating new dictionary based on a set of keys
new_dict = dict.fromkeys(['one', 'two', 'one'], 0)
print(new_dict)

new_dict.clear()
print(new_dict)

Adding and Updating Items

  • server_config['port'] = 8080 # Update existing key
  • server_config['environment'] = 'production' # Add new key-value pair
tags = {
    "Environment": "Production",
    "Owner": "Finance",
    "CostCenter": "10000"
}

tags["CostCenter"] = "12345"
tags["Project"] = "Python for DevOps"

print(tags)
3 months ago Permalien
cluster icon
  • Handling Errors and Status Codes : Handling Errors and Status Codes HTTP status codes communicate the outcome of an API request, and handling them correctly is key to robust automation...
  • Logging Anatomy : Python Logging Anatomy Python’s logging module has five core components: Loggers, Log Records, Handlers, Formatters and Filters. Loggers are hierar...
  • Classes and Objects : Classes and Objects Beyond Built-ins: Python lets you define your own data types using class. Class: A blueprint or template for creating objects. De...
  • Tuples, sets : Tuples (tuple) Tuples are ordered, immutable sequences defined with parentheses (). Once created, their contents cannot be changed. Characteristics an...
  • Implementing Retries and Timeouts : Implementing Retries and Timeouts External services can be slow or unreliable, causing scripts to hang or fail unexpectedly. Timeouts and retries hel...

Tuples, sets/shaare/2QdV2w

  • python
  • python

Tuples (tuple)

Tuples are ordered, immutable sequences defined with parentheses (). Once created, their contents cannot be changed.

Characteristics and Use Cases

  • Ordered: items maintain position
  • Immutable: cannot add, remove, or change after creation
  • Useful for fixed records like coordinates, version numbers, or as dictionary keys
host_port = ("127.0.0.1", 3000)
red_rgb = (255, 0, 0)
tuple_single_value = ("only-value",) # To create a single-item tuple, add a trailing comma
print(type(host_port))
print(type(tuple_single_value))

print(f"Host: {host_port[0]}")
print(red_rgb[-2:])
print(type(red_rgb[-2:]))

# host_port[0] = "192.168.1.1" # Uncommenting will raise a TypeError because tuples are immutable

Sets (set)

  • Characteristics: Unordered, Mutable, Unique items only (duplicates removed)
    • The items of a set must be immutable.
  • Use Cases: Membership testing, removing duplicates, set operations (union, intersection, difference).

Set Operations

  • Membership Testing: Check if an item exists in a set using the in keyword.
  • Adding Items: Use add() to add an item to a set.
  • Removing Items: Use remove() to remove an item (raises an error if the item doesn't exist) or discard() to remove an item (doesn't raise an error if the item doesn't exist).
  • Set Operations:
    • Union: Combine all unique items from two sets using union() or |.
    • Intersection: Find common items between two sets using intersection() or &.
    • Difference: Find items in one set but not in another using difference() or -.
unique_ports = set([80, 443, 22, 80, 8080, 443])
server_names = {"web01", "web02"}

print(unique_ports)
print(22 in unique_ports)
print(22 in server_names)

unique_ports.add(3000)
print(unique_ports)
unique_ports.remove(22)
print(unique_ports)
# unique_ports.remove(22) # Will raise KeyError because item 22 is not in the set anymore
unique_ports.discard(22)
print(unique_ports)
# set_of_lists = set([[1, 2], [3, 4]]) # Will throw a TypeError, since lists are mutable
# set_of_sets = {{1, 2}, {3, 4}} # Will throw a TypeError, since sets are mutable
set_of_tuples = {(1, 2), (3, 4)}
print(set_of_tuples)
print((1, 2) in set_of_tuples)
print((1, 3) in set_of_tuples)
3 months ago Permalien
cluster icon
  • Making HTTP Requests : Making HTTP Requests The requests library simplifies HTTP interactions by abstracting raw HTTP details, making it ideal for DevOps automation tasks. ...
  • Functions: return vs yield : Functions: return vs yield Regular functions execute immediately, run to completion, and return a single value (or None). Generator functions retur...
  • Regex : Regex Essentials: Overview Regular expressions (regex) are a language for defining text search patterns. Python’s re module provides functions like...
  • Generators : Generators Writing a class-based iterator requires __iter__() and __next__(), plus manual state management and StopIteration handling. Generator fu...
  • Mocking : Mocking Fundamentals Introduction When unit testing DevOps scripts that interact with external systems, tests can become slow, unreliable, difficult ...

List/shaare/pR6E2Q

  • python
  • python

Lists (list)

Lists are ordered, mutable sequences defined with square brackets []. You can add, remove, or change items after creation.

Characteristics and Use Cases

  • Ordered: items maintain position
  • Mutable: .append(), .insert(), .pop(), .remove()
  • Ideal for storing sequences where order matters and contents change (e.g., list of servers, deployment steps)

Accessing Items and Slicing

  • Access single elements with my_list[index] (0-based). Use negative indices like my_list[-1] for the last item.
  • Slice with my_list[start:stop] to get a sub-list from start up to (but not including) stop.
  • Use three-parameter slicing my_list[start:stop:step] for stepping, e.g., my_list[::2] selects every other element.
  • Omitting start or stop defaults to the beginning or end of the list respectively, and slicing returns a new list without modifying the original.
servers = ["web01", "web02", "web03"]
mixed_list = ["config.yaml", 8080, True]

for item in mixed_list:
    print(type(item))

print(servers[0])
# print(servers[3]) # Commenting this out will raise an IndexError Exception
print(servers[-1])
print(servers[-2])

# Slicing
print(servers[:2]) # Will print only elements at indexes 0 and 1
print(servers[1:]) # Will print only elements at indexes 1 and 2
print(servers[-2:]) # Will print only the second to last and last elements
# Slicing does not alter the original list
print(servers)
# Mutating lists
ports = [80, 443, 8080]
ports.append(5000)
print(ports)
ports.insert(1, 3000)
print(ports)
ports.remove(80)
print(ports)
removed_value = ports.pop(2)
print(ports)
print(removed_value)

# Example to show how mutating lists can lead to side-effects outside of
# the scope of the code that modifies the list.
def mutate_list(l):
    l.pop()

new_list = ["a", "b", "c"]
mutate_list(new_list)
print(new_list)
3 months ago Permalien
cluster icon
  • Implementing Retries and Timeouts : Implementing Retries and Timeouts External services can be slow or unreliable, causing scripts to hang or fail unexpectedly. Timeouts and retries hel...
  • Editable Installs with pyproject.toml : Editable Installs with pyproject.toml The Python interpreter doesn't automatically know about our project's structure. The modern and most robust solu...
  • Python package and subpackage : Introduction to Packages (__init__.py) What is a Package? A Python package provides a way to structure a project's module namespace by using directori...
  • Filesystem Paths : Working with Filesystem Paths in Python Manipulating paths as plain strings is error-prone and OS-specific. pathlib provides an object-oriented, cr...
  • Fixtures in Pytest : Fixtures in Pytest As tests grow more complex, repeating setup and cleanup steps makes tests harder to read and maintain. Pytest fixtures allow centr...

Numbers, strings/shaare/R2PqPQ

  • python
  • python

Numbers (int and float)

  • int: Whole numbers (e.g., 10, 1024). No overflow due to arbitrary precision.
  • float: Numbers with decimals (e.g., 3.14159). Uses IEEE 754 representation; small precision differences possible.
import math

print(type(1.0))

print("When comparing floats directly, we may run into precision issues:")
print("0.1 * 3 == 0.3: ", 0.1 * 3 == 0.3)
print("To tackle this, we can use the math.isclose() function:")
print("math.isclose(0.1 * 3, 0.3): ", math.isclose(0.1 * 3, 0.3))

Arithmetic Operations

  • +, -, *, /
  • / true division → float
  • // floor division → integer or float
  • % modulo → remainder
  • ** → power
print(8/2)
print(type(8/2))
print(5/3)
print(8//2)
print(type(8//2))
print(5//3)
print(5//3.0)
print(5%3) # 1 as the result, and 2 remaining

Strings

String Manipulation

  • Strings are ordered, immutable sequences of characters.
  • Use single or double quotes consistently; triple quotes for multi-line strings or docstrings.
single_line_str = "Double quoted"
single_line_str2 = 'Single quoted'

command_template = """
I will not be indented
    I will be indented
"""

print(command_template)

Format Output using f-string

Tip: f-strings allow inline expression evaluation and formatting, making string construction concise and readable.

math_division = 7/2
print(f"Result: {math_division}")
print(f"Result: {7/2}")

Common Operations and Essential String Methods

  • Concatenation (+): Joins strings.
  • Length (len()): Gets the number of characters.
  • Indexing ([]): Access a character by position (0-based).
  • Slicing ([:]): Extract substrings.
  • .lower() / .upper()
  • .strip() / .lstrip() / .rstrip()
  • .startswith() / .endswith()
  • .split() / .join()
  • .replace()
course_title = "     Python strings    "
print(course_title)
print(f"Result of .strip(): {course_title.strip()}")
print(f"Result of .lstrip(): {course_title.lstrip()}")
print(f"Result of .rstrip(): {course_title.rstrip()}")
print(f"Result of .upper(): {course_title.upper()}")
print(f"Result of .lower(): {course_title.lower()}")

filename = "file.yaml"
print(filename.startswith("file"))
print(filename.endswith("yaml"))

path = "/usr/local/bin"
path_parts = path.split("/")
print(f"path parts: {path_parts}")
print(f"joined path paths: {"\\".join(path_parts)}")

print(path + "/python")
print(len(path))
print(path[3])
print(path[3:10])
print(path[3:])
print(path[:10])

String Immutability

Strings are immutable, meaning you cannot change a string in place; operations that seem to modify a string actually create and return a new string object.

course_title = "     Python strings    "
print(course_title)
print(f"Result of .strip(): {course_title.strip()}")
print(f"Result of .lstrip(): {course_title.lstrip()}")
print(f"Result of .rstrip(): {course_title.rstrip()}")
print(f"Result of .upper(): {course_title.upper()}")
print(f"Result of .lower(): {course_title.lower()}")
print(course_title)
3 months ago Permalien
cluster icon
  • *args and **kwargs : Flexible Functions: *args and **kwargs We can use the syntax *args and **kwargs to accept a variable number of both positional and keyword arguments....
  • Adding Type Hints to Decorators and Generators : Adding Type Hints to Decorators and Generators Decorators and generators are advanced constructs that require specialized type hints to make their tr...
  • Typing classes : Introduction As our Python automation projects grow, defining custom classes helps model complex objects and should be reflected in type hints for cl...
  • Python Modules and the import System : Python Modules and the import System What is a Module? A module in Python corresponds directly to a single file containing Python code. The module's ...
  • Dictionaries : Dictionaries (dict) Dictionaries are mutable, insertion-ordered collections of key-value pairs. Keys must be unique and immutable; values can be of an...

Variables, comments/shaare/x85Yzw

  • python
  • python

Variables: Naming Values

  • Naming Guidelines:
    • Must start with a letter or underscore (_) and can contain letters, numbers, and underscores.
    • Use snake_case for readability (e.g., max_retries).
  • Purpose: Store data like file paths, server counts, status messages, API keys, configurations.
  • Typing: Python uses dynamic typing, which means we don't need to explicitly declare the variable type, and we can assign values with different types to the same variable (not recommended!)
var1 = "hello"
item = 101
print(type(item))
# Never do that! Don't assign a value of a different type to the same variable!
item = "Code 101"
print(type(item))

Comments

Python code may be readable, but comments and docstrings explain intent, rationale, and usage. Comments (#) are ignored by the interpreter; docstrings ("""...""") are accessible via __doc__ (we'll come back to docstrings later, when we discuss functions).

Single-Line Comments (#)

Use # to comment single lines or inline code. Best for explaining why, adding TODO/FIXME markers, or temporarily disabling code.

# Example of a single-line comment
error_code = 0

# TODO: handle case when argument is None 

Multi-Line / Block Comments

Prefix each line with # to comment out blocks of code. Useful for disabling sections or annotating complex logic. It's also possible to wrap multiline comments between triple single-quotes ('''...''') or between triple double-quotes ("""..."""), but this is not their original intended usage.

# if True:
#     print("I will execute")
3 months ago Permalien
cluster icon
  • Regex : Regex Essentials: Overview Regular expressions (regex) are a language for defining text search patterns. Python’s re module provides functions like...
  • Logging Anatomy : Python Logging Anatomy Python’s logging module has five core components: Loggers, Log Records, Handlers, Formatters and Filters. Loggers are hierar...
  • Pytest Markers : Pytest Markers Markers are decorators (@pytest.mark.) applied to tests to attach metadata. Built-in markers like skip, skipif, xfail, and parametrize...
  • Handling Authentication : Handling Authentication APIs often require authentication to control access, rate limits, and auditing. Without authentication, requests to protected...
  • Temporary Files and Directories : Temporary Files and Directories Automation scripts often need scratch space for intermediate data without cluttering the filesystem or risking name c...

GIT/shaare/kW0W5g

  • git
  • git

  • git init → initialize a new Git repo
  • git clone → clone remote repo
  • git add → stage change for next commit
  • git commit → create new commit with staged changes
  • git push → upload local commits to a remote repo
  • git pull → download change from remote to local
  • git checkout → switch between branches or restore
  • git merge → combine change from branches

Installing Git

  • git config --global
    • user.name → "your name"
    • user.email → "my@email.com"
    • preferred text editor
    • core.editor → "code --wait"
    • color output
    • color.ui → auto

  • git status → check the current state
  • git log → commit history

Branching and Merging

  • git branch → create a new branch

  • git checkout main

  • git merge mybranch


git init → create new repository

  • git init

  • git init --bare → no working directory

    • stocker l'historique Git et synchronise l'équipe
  • git init --template=/path/to/template/dir

    • use configuration in directory
    • for new Git folder

git add

  • git add → stage changes for next commit

    • staging area → git status
  • git add [tag]

  • git add .

  • git add *.txt


  • git reset [tag] → reset

.gitignore

  • Create a .gitignore in root of repo
  • Specify pattern of file you want to ignore
    • *.log
    • .DS-Store

git commit → snapshot → commit hash

1st:

  • git add .
  • git status

2nd:

  • git commit -m "my message"

3rd:

  • git log → show commit history

Amend Commit → modify the most recent commit

  • Change the commit message

  • Add or remove file from the commit

  • Correct mistake or typos

  • git commit --amend -m "typo fix"


Undo Commit

  • git reset HEAD~1 → most recent commit

  • git reset HEAD~3 → last 3 commit

  • git revert → new commit that undo the change


Cloning a Repository

  • git clone https://www..../repository.git

  • git status → check current branch

  • git push origin master

    • push change to remote repo
  • git pull → git fetch + git merge

  • git fetch → retrieve the latest change from remote

  • git merge → merge the changes from the remote directory


Creating Branches

  • git branch feature/new-login-page

    • create branch
  • git checkout feature/new-login-page

    • switch to new branch
  • git checkout -b feature/new-login-page

    • create new branch & switch to it
  • git checkout master

    • switch back to master branch

Merging Branches

  • git merge feature/new-login-page

Best Practices

  • Use consistent branch naming

    • feature/
    • bugfix/
    • hotfix
  • Keep branches small and focused on a single feature

  • Regularly merge branch

  • Delete merged branch

  • Synchronize local branch with the remote repo


Git Flow Workflow

Main Branches

  • master → production ready code
  • develop → dev code with new features

Supporting Branches

  • feature → dev new feature
  • release → use to prepare a new release
  • hotfix → quick fix of critical bug
  1. Start new feature

    • branch from develop
  2. Work on new branch & commit

  3. Merge feature back to develop

  4. Create a release

    • branch off develop
    • release/1.2.0
  5. Finalize release

    • make change
    • merge release into master
    • develop
  6. Fix a bug

    • branch off master
    • hotfix/1.2.1
    • merge

Pull Request Workflow

  1. Branch out → create new branch from master
  2. Commit change → commit your work to new branch
  3. Push the branch → push branch to the remote
  4. Open a pull request → go to web interface, create pull request
  5. Add a description → describe change you've made
  6. Review → assign team members to review the pull request

Rebasing

  • Rewrite the commit history

Example

  • master → A --- B --- C

  • feature → D --- E

  • Faire partir la branche feature de C

  • git checkout feature

  • git rebase master

  • git push --force

    • pousse avec force l'historique
  • Resulting feature branch:

    • A --- B --- C --- D --- E
  • git rebase --interactive

    • customize rebase concept

Stashing Changes

  • Save local change without committing them

  • Create a snapshot of your current working dir

  • git stash -m "WIP: implementing new feature"

  • git stash list

    • list of all stash changes
  • git stash show stash@{0}

    • show change of a specific stash
  • git stash apply

    • apply most recent stash
  • git stash drop

    • remove stash
  • git stash clear

    • remove all stash
  • git stash pop

    • apply & drop

Stash Branching

  • git stash branch new-feature stash@{0}
    • create new branch "new-feature"
    • checkout the new branch
    • apply change from the stash
    • drop the applied stash

Git Hooks

  • Script that run automatically every time an event is made in repo

  • Automate task

  • pre-commit hook → run before commit

  • commit-msg hook → run after commit

  • pre-push hook → run before push

  • post-push hook → run after push

  • post-checkout hook → run after a branch is checked out

  • post-merge hook → run after a merge

  • Local hooks → stored in .git/hooks

  • Global hooks → stored in ~/.git-templates/hooks


Create a New Hook

  • touch .git/hooks/pre-commit

  • chmod +x .git/hooks/pre-commit

    • local
  • touch ~/.git-templates/hooks/pre-commit

  • chmod +x ~/.git-templates/hooks/pre-commit

  • Bash scripting


  • git checkout HEAD^ → go to previous commit
  • git diff → see difference
  • git checkout <filename> → undo the change
  • git restore → restore file
  • git restore --source HEAD~N <file-name>
  • git restore --staged <filename>
  • git revert <hash> → create new commit reverting the change

Move Commit from One Branch to Another

  • Switch to branch
    • git switch mybranch
    • git log → get commit hash
    • git switch master
    • git cherry-pick <commit-hash>

Tag ≃ Bookmarks

  • Lightweight tag → name

    • git tag v1.0
  • Annotated tag → name + desc

    • git tag v1.0 -m

Git Tags / Rebase / Reflog

  • git tag → list of the tag

  • git push origin <tag name> → push tag to remote

  • git tag -d <tag name> → delete

  • git push origin --delete <tag>

  • git diff main origin/main

    • comparer les différences entre local et remote

  • git rebase -i HEAD~N

    • combien de commit en arrière
    • réécrire, fusionner, réorganiser
    • corriger l'historique de plusieurs commits en une seule session
  • pick → garder tel quel

  • reword → changer le message

  • squash → fusionner plusieurs commit

  • edit → modifier un commit

  • drop → supprimer un commit


  • git reflog → voir l'historique pour revert

  • git reset --hard HEAD@{?}

    • ou git checkout <commit-id>
  • git merge --continue → to follow git merge after diff

4 months ago Permalien
cluster icon
  • Aucun lien connexe

Journalctl/shaare/QpdyUg

  • linux
  • linux

journalctl

  • journalctl -u sshd

    • show only log for sshd
  • logger hello

    • write in syslog
  • journalctl -f

    • log is live
  • nano /etc/systemd/journald.conf

    • Storage=auto
  • mkdir /var/log/journal

    • [unclear note in image]
4 months ago Permalien
cluster icon
  • IP Subnet Calculator :
  • File Display Commands / Filters / Text Processing Input : File Display Commands cat → Show entire content cat -A → Show non-printable characters more → Paginate output less → Same as more but allows navigatio...
  • Stratis : Stratis → advanced storage management Extend filesystem automatically when needed dnf install stratis-cli stratisd dnf makecache --refresh Upda...
  • Special Permissions (SetUID, SetGID, Sticky Bit) : chmod u+s xyz.sh → add SetUID (user-level special permission) chmod g+s xyz.sh → add SetGID (group-level special permission) chmod u-s xyz.sh → remov...
  • Navigating File System / Linux filetypes : ls -l → List pwd → Print working directory dr-xr-xr-x | directories -rw-r--r-- | file Linux File Types Symbol Type - Regular file d Dire...

Share folder (NFS and Samba)/shaare/0MCBLg

  • linux
  • linux

NFS → Network File System

  • Share folder

NFS Server

  • dnf install nfs-utils libnfsidmap

  • systemctl enable rpcbind

  • systemctl enable nfs-server

  • systemctl start rpcbind

  • systemctl start nfs-server

  • systemctl start rpc-statd

  • systemctl start nfs-idmapd

  • mkdir /myshare

  • chmod a+rwx /myshare

  • nano /etc/exports

/myshare *(rw,sync,no_root_squash)
  • exportfs -rv → export NFS file system

NFS Client

  • dnf install nfs-utils rpcbind

  • service rpcbind start

    • Start package
  • ps -ef | egrep "firewall|iptable"

    • Disable firewall in case on server
  • showmount -e 192.168.0.100

    • Show mount from NFS server
    • 192.168.0.100 = NFS server IP
  • mkdir /mnt/app → create mount point

  • mount 192.168.0.100:/myshare /mnt/app

    • Mount NFS file system
  • df -h → verify mounted system

  • umount /mnt/app


SAMBA

  • SMB → Server Message Block

  • CIF → Common Internet File System

  • Samba protocol

  • dnf install samba samba-client samba-common

  • firewall-cmd --permanent --zone=public --add-service=samba

  • firewall-cmd --reload

    • Add rule to firewall
  • mkdir -p /samba/myshare

  • chmod a+rwx /samba/myshare

  • chown -R nobody:nobody /samba

    • Create Samba share directory
  • chcon -t samba_share_t /samba/myshare

    • Change SELinux security context
  • nano /etc/samba/smb.conf

    • Add new filesystem shared
[anonymous]
path = /samba/myshare
browsable = yes
writable = yes
guest ok = yes
guest only = yes
read only = no
  • testparm

    • Test SMB configuration
  • systemctl enable smb

  • systemctl start smb

    • Start Samba service

Mount on Linux Client

  • dnf -y install cifs-utils samba-client

  • mkdir -p /mnt/sambashare

  • mount -t cifs //192.168.0.35/anonymous /mnt/sambashare

    • Mount Samba share without password

Secure Samba Server

  • useradd larry

  • groupadd smbgrp

  • usermod -a -G smbgrp larry

  • smbpasswd -a larry

    • Set Samba password for larry
  • mkdir /samba/secureshare

  • chown -R larry:smbgrp /samba/secureshare

  • chmod -R 0770 /samba/secureshare

  • chcon -t samba_share_t /samba/secureshare


Samba Secure Share

  • nano /etc/samba/smb.conf
[secure]
path = /samba/secureshare
valid users = @smbgrp
guest ok = no
writable = yes
browsable = yes
  • systemctl restart smb
4 months ago Permalien
cluster icon
  • Crontab / at : Crontab Basics crontab -e → edit crontab crontab -l → list crontab entries crontab -r → remove crontab entries systemctl status crond → check crond s...
  • SadServers: Linux, DevOps & SRE Labs | Interview & Hiring Assessment : Hands-On Linux & DevOps Real Challenges. Real Infra. Real Skills. Master Linux & DevOps troubleshooting on live servers. Fun, real-world challenges fo...
  • Disk usage, logging, shutdown, hostname : Disk and Memory Info df → disk partition info df -h → human-readable format du → size of file/directory free → memory info cat /proc/cpuinfo → CPU in...
  • Special Permissions (SetUID, SetGID, Sticky Bit) : chmod u+s xyz.sh → add SetUID (user-level special permission) chmod g+s xyz.sh → add SetGID (group-level special permission) chmod u-s xyz.sh → remov...
  • Filesystem check and disk cloning : fsck & xfs_repair → filesystem check fsck → ext2, ext3, ext4 xfs_repair → xfs only Made each boot & fix it df -T → check Unmount before fsck ...

Filesystem check and disk cloning/shaare/Utyg1Q

  • linux
  • linux

fsck & xfs_repair → filesystem check

  • fsck → ext2, ext3, ext4

  • xfs_repair → xfs only

  • Made each boot & fix it

  • df -T → check

    • Unmount before fsck

fsck error code

  • 0 → no error

  • 1 → error corrected

  • 2 → reboot needed

  • 4 → some error left uncorrected

  • 8 → operational error

  • -f → force

  • -y → repair automatically

  • fsck /dev/sdb

  • umount /bigdata

  • xfs_repair /dev/mapper/stratis...


dd → disk cloning

  • dd if=<sourcefilename> of=<targetfilename>

  • dd if=/dev/sda1 of=/root/sda1.img

    • Backup copy disk partition
  • dd if=/root/sda1.img of=/dev/sdb3

    • Restore
4 months ago Permalien
cluster icon
  • Crontab / at : Crontab Basics crontab -e → edit crontab crontab -l → list crontab entries crontab -r → remove crontab entries systemctl status crond → check crond s...
  • Share folder (NFS and Samba) : NFS → Network File System Share folder NFS Server dnf install nfs-utils libnfsidmap systemctl enable rpcbind systemctl enable nfs-server ...
  • Ldap Secure Ssh : Check on listening port netstat -tunlp → check open/listening port Securing SSH config more /etc/ssh/sshd_config change port 22 PermitRootLogin ...
  • NTP and Mail : NTP / Chronyd → Time Synchronisation nano /etc/chrony.conf → edit conf systemctl start chronyd systemctl enable chronyd chronyc → interactive cmd t...
  • Linux filesystem : Directory Description /boot Grub.cfg /root home of root /dev system device (mouse, keyboard) /etc configuration files /bin → /usr/bin e...

Stratis/shaare/XNOa6Q

  • linux
  • linux

Stratis → advanced storage management

  • Extend filesystem automatically when needed

  • dnf install stratis-cli stratisd

  • dnf makecache --refresh

    • Update latest package list for DNF
  • systemctl start stratisd

  • systemctl enable stratisd

    • Start daemon
  • lsblk

    • List block device
  • stratis pool create pool1 /dev/sdb

    • Create pool with /dev/sdb
  • stratis pool list

    • Show pool list
  • stratis pool add-data pool1 /dev/sdc

    • Extend pool1 with /dev/sdc

Stratis Filesystem

  • stratis filesystem create pool1 fs1

    • Create filesystem using Stratis
  • stratis filesystem list → get UUID

    • Verify filesystem creation
  • mkdir /bigdata

  • mount /stratis/pool1/fs1 /bigdata

    • Mount Stratis disk to /bigdata
  • stratis filesystem snapshot pool1 fs1 fs-snap

    • Make a snapshot
  • nano fstab

UUID="a313..."   /bigdata   xfs   defaults,x-systemd.requires=stratis.service   0 0
  • Startup mount

RAID (Redundant Array of Independent Disks)

  • RAID0 → add physical disk to make big one
  • RAID1 → mirror (slow)
  • RAID5 → 3 or more disks
    • Read & write a little on every disk
4 months ago Permalien
cluster icon
  • User management : Essential Commands: useradd groupadd userdel groupdel usermod Modify 3 files: /etc/passwd, /etc/group, /etc/shadow (passwd info) Cre...
  • Networking : Interface configuration files: /etc/nsswitch.conf → where resolve hostname to IP address /etc/hosts → add new IP to resolve /etc/resolv.conf → r...
  • Systemctl, process management : Basic System Info Commands uptime → time now, up since, numbers of users, load average hostname → ip hostname uname -a → current OS and kernel info w...
  • Journalctl : journalctl journalctl -u sshd show only log for sshd logger hello write in syslog journalctl -f log is live nano /etc/systemd/journald.c...
  • Special Permissions (SetUID, SetGID, Sticky Bit) : chmod u+s xyz.sh → add SetUID (user-level special permission) chmod g+s xyz.sh → add SetGID (group-level special permission) chmod u-s xyz.sh → remov...

Logical Volume Management (LVM)/shaare/PaqQSg

  • linux
  • linux

LVM (Logical Volume Management)

  • Combine disk together by software

  • Add new HDD on the fly to extend disk space

  • Set LVM in Linux install

  • Desired capacity: all → set last partition to space left

  • /boot → 500 MB LVM ⚠ always

  • fdisk /dev/sdc

    • n → new partition
    • t → change partition system ID
    • 8e → change to Linux LVM
  • pvcreate /dev/sdc1 → create physical volume

  • pvdisplay → infos

  • vgcreate vg /dev/sdc1 → create volume group

  • vgdisplay → show info

  • lvcreate -n lv --size 500 vg

  • lvdisplay → show info

  • mkfs.xfs /dev/vg/lv → format logical volume

Extend LVM → create LVM partition

  • pvcreate /dev/sd01
  • vgextend vg /dev/sd01
  • lvextend -L +100M /dev/mapper/vg-vg
  • xfs_growfs /dev/mapper/vg-vg

Add / Extend Swap Space

  • System will not boot if set incorrect

  • Recommended: 2x size of RAM

  • dd if=/dev/zero of=/newswap bs=1M count=1024

    • Extract disk space from HDD to swap
  • chmod go-r /newswap

    • Make file non-readable for others
  • mkswap /newswap

    • Make swap file
  • swapon /newswap

    • Activate swap (add with the rest)
  • nano fstab

/newswap swap swap defaults 0 0

xfs_info → display detailed information

  • xfs_info /dev/mapper/cs-root
    • Debug info about main XFS partition
4 months ago Permalien
cluster icon
  • Networking : Interface configuration files: /etc/nsswitch.conf → where resolve hostname to IP address /etc/hosts → add new IP to resolve /etc/resolv.conf → r...
  • Linux File Ownership, ACLs, and I/O Redirects : File Ownership chown → Change ownership chgrp → Change group Access Control List (ACL) setfacl → Set file ACL getfacl → Get file ACL Add Permissi...
  • Vi Editor : Vi Editor Commands i → insert mode ESC → escape to command mode r → replace mode x → delete character dd → delete line yy → copy line p → paste v → v...
  • Firewall : Enable firewall firewall-config → GUI for options add ports firewall-cmd → CLI cat /etc/sysconfig/iptables-config cat /etc/firewalld/firewalld.co...
  • Linux filesystem : Directory Description /boot Grub.cfg /root home of root /dev system device (mouse, keyboard) /etc configuration files /bin → /usr/bin e...

Computer Storage / Disk Partition/shaare/aqHzuQ

  • linux
  • linux

Computer Storage

  • Local → RAM / HDD / SSD

  • DAS (Direct Attached Storage)

    • USB HDD / DVD
  • SAN (Storage Area Network)

    • through iSCSI cable or fiber cable
    • PCI SAN cards / HBA cards
  • NAS (Network Attached Storage)

    • through network (TCP/IP) Samba, NFS

Disk Partition

  • df → disk info

  • fdisk → total & partition

  • fdisk -l → get info about partition

  • fdisk /etc/sdb → mount partition

    • n → new partition
    • w → write
  • mkfs.xfs /dev/sdb1

    • create file system
  • mkdir /data → create folder to mount partition

  • mount /dev/sdb1 /data

    • mount disk
  • nano /etc/fstab

    • mount new disk at startup
/dev/sdb1    /data    xfs    defaults    0    0
  • unmount /data

    • unmount disk
  • mount -a → read fstab and remount disk

4 months ago Permalien
cluster icon
  • Ldap Secure Ssh : Check on listening port netstat -tunlp → check open/listening port Securing SSH config more /etc/ssh/sshd_config change port 22 PermitRootLogin ...
  • System information, root recovery, environment variables, shortcuts : Finding System Information cat /etc/redhat-release → Red Hat version uname -a → Linux hostname, kernel, architecture dmidecode → hardware, BIOS, syst...
  • Bash : First Line of Script #!/bin/bash → defines the shell interpreter Comments Use # for commenting Common Elements Commands: echo, cp, etc. Statement...
  • Screen & Tmux : Screen (Terminal Multiplexer) Multi-terminal sessions in one window Alt+a | → split vertical Alt+a Shift+s → split horizontal Alt+a Tab → switch wind...
  • Linux filesystem : Directory Description /boot Grub.cfg /root home of root /dev system device (mouse, keyboard) /etc configuration files /bin → /usr/bin e...

Linux Boot Process / Optimizing Boot Performance/shaare/F8MFtg

  • linux
  • linux

Linux Boot Process → Older Version

  • BIOS → Basic Input / Output System

    • executes MBR
  • MBR → Master Boot Record

    • executes GRUB
  • GRUB → Grand Unified Bootloader

    • executes kernel
  • KERNEL

    • kernel executes /sbin/init
    • mount the root file system
  • INIT

    • init executes run level programs
    • initial RAM disk → contains drivers
  • RUN LEVEL

    • run level programs executed from /etc/rc.d/rc*.d/

Linux Boot Process → CentOS 7, 8, 9

  • BIOS → MBR → GRUB 2

  • /boot/grub2/grub.cfg

  • KERNEL

    • load drivers from initrd.img
    • start the first OS process (systemd)
  • SYSTEMD = system daemon

    • read /etc/systemd/system/default.target

systemd-analyze → Optimizing Boot Performance

  • Understand how long the Linux system takes to boot by:

    • kernel
    • service
    • initrd
  • initrd → initialize system hardware

  • systemd-analyze blame

    • sort by time all service

Message of the Day → Message at Login

  • nano /etc/motd

  • Customize:

    • touch /etc/profile.d/motd.sh
  • In /etc/ssh/sshd_config

    • PrintMotd no
  • systemctl restart sshd.service

4 months ago Permalien
cluster icon
  • Bash : First Line of Script #!/bin/bash → defines the shell interpreter Comments Use # for commenting Common Elements Commands: echo, cp, etc. Statement...
  • Filesystem check and disk cloning : fsck & xfs_repair → filesystem check fsck → ext2, ext3, ext4 xfs_repair → xfs only Made each boot & fix it df -T → check Unmount before fsck ...
  • Screen & Tmux : Screen (Terminal Multiplexer) Multi-terminal sessions in one window Alt+a | → split vertical Alt+a Shift+s → split horizontal Alt+a Tab → switch wind...
  • Tuned : Tweaks with Tuned / Podman Tweaks with Tuned systemctl enable tuned tuned-adm active check which profile is active tuned-adm list list all prof...
  • SSH and DNS : SSH ps -ef | grep sshd → check ssh systemctl status sshd DNS PTR record → IP to hostname A record → hostname to IP CNAME record → hostname to...

System Run Level and Boot Process/shaare/CcSV1A

  • linux
  • linux

System Run Level

  • init 0 → shutdown
  • init 1 → single user mode → troubleshoot
  • init 6 → reboot the system
  • init 2 → multiuser without networking
  • init 3 → multiuser with networking
  • init 5 → multiuser with networking with GUI
who -r
  • To know which level

Boot Process

  • CPU -> BIOS -> CMOS
  • BIOS = Basic Input / Output System
  • CMOS = Complementary Metal-Oxide Semiconductor
  • ROM = Read Only Memory
  • POST = Power On Self Test
  • HDD → 1st sector = MBR
  • MBR = Master Boot Record
  • OS in RAM
  • App goes to CPU

Bootstrap

  • How the computer is going to power on
4 months ago Permalien
cluster icon
  • SED: Stream Editor for Text Manipulation : Basic Replace Syntax: sed -i 's/KENNY/LENNY/g' filename Substitute all occurrences of "KENNY" with "LENNY" Delete Line Containing String: s...
  • Logical Volume Management (LVM) : LVM (Logical Volume Management) Combine disk together by software Add new HDD on the fly to extend disk space Set LVM in Linux install Desired...
  • Share folder (NFS and Samba) : NFS → Network File System Share folder NFS Server dnf install nfs-utils libnfsidmap systemctl enable rpcbind systemctl enable nfs-server ...
  • Computer Storage / Disk Partition : Computer Storage Local → RAM / HDD / SSD DAS (Direct Attached Storage) USB HDD / DVD SAN (Storage Area Network) through iSCSI cable or fiber ...
  • Linux filesystem : Directory Description /boot Grub.cfg /root home of root /dev system device (mouse, keyboard) /etc configuration files /bin → /usr/bin e...

Ansible/shaare/xvI12A

  • ansible
  • ansible

  • Control node → server which runs Ansible
  • Modules → command executed on client side (found pre-made modules on Ansible website)
  • Task → multiple procedures to be completed
  • Playbook → automation file (YAML) with step-by-step execution of multiple tasks
  • Inventory → hosts file, remote clients where tasks are executed
  • Tag → reference to a specific task
  • Variable → value reused across tasks
  • Role → split playbook into smaller sub-playbooks

Install Ansible

  • dnf install epel-release
  • dnf install ansible ansible-doc
  • ansible --version
  • ansible localhost -m ping

Config Files

  • /etc/ansible
  • /etc/ansible/ansible.cfg
  • /etc/ansible/hosts → IP of remote
  • /etc/ansible/roles → sub-task

YAML File Syntax

  • Sequential → process one at a time
  • Indentation is extremely important → use spaces, no tabs
  • Empty lines have no value
  • Extension: .yml or .yaml
  • Execute YAML with absolute path if not in /etc/ansible/
  • No need to modify file permission

Example of YAML Playbook

- name: sampleplaybook
  hosts: all or localhost
  become: yes
  become_user: root

  tasks:
    - name: install apache http
      yum:
        name: httpd
        state: present

    - name: 2nd task
      service:
        name: httpd
        state: started

→ More modules at: docs.ansible.com

Ansible Playbook Basics

  • ansible-playbook --syntax-check my.yml
  • ansible-playbook --check my.yml

Run a Playbook

ansible-playbook /root/ansible/first.yml

Example Output

  • Output playbook → debug: msg="hello"

Remote Client Inventory

  • Remote client file → /etc/ansible/hosts
[appservers]
app1.example.com
app2.example.com

[webserver]
web1.example.com
web2.example.com
  • Header = group client

IP Range Example

192.168.0.[110:119]

Custom Inventory Path

ansible-playbook -i /home/user/ansible/hosts

Inventory Examples

[server]
server1 ansible-ssh-host=192.168.0.20
server2 ansible-ssh-host=192.168.0.21

[appserver]
server1

[webserver]
server2

List Inventory

ansible-inventory --list
  • Listing host file

Connect to Remote Host

  • Edit inventory:
nano /etc/ansible/hosts
[labclients]
192.168.0.57
  • ssh-keygen
  • ssh-copy-id 192.168.0.57 → automatic login
  • ansible all -m ping → check connection
  • ansible -a "uptime" all → check uptime on remote

Playbook Copy File

tasks:
  - name: copy file
    become: true
    copy:
      src: /home/sterne/file
      dest: /tmp
      owner: sterne
      group: sterne
      mode: 0644
  • become: true → available for other user

Playbook Change Permission

tasks:
  - name: file perm
    file:
      path: /home/sterne/backup.tar
      mode: a+w

Playbook Install Apache Server / Open Port

  • ansible-galaxy collection install ansible.posix

Run Shell Script

tasks:
  - name: run shell script
    shell: "/home/sterne/myscript.sh"

Set Cronjob

tasks:
  - name: "schedule cron"
    cron:
      name: comment for crontab
      minute: "0"
      hour: "10"
      day: "*"
      month: "*"
      weekday: "4"
      user: root
      job: "/home/sterne/myscript"

Create User

tasks:
  - name: create user
    user:
      name: sterne
      home: /home/sterne
      shell: /bin/bash

Change Password

tasks:
  - name: "change pass"
    user:
      name: george
      update_password: always
      password: "{{ newpassword | password_hash('sha512') }}"

Download Permission

tasks:
  - name: download tomcat
    hosts: localhost
    tasks:
      - name: create a directory
        file:
          path: /opt/tomcat
          state: directory
          mode: 0755
          owner: root
          group: root

      - name: get package from url
        url: https://...
        dest: /opt/tomcat
        mode: 0755
        group: sterne
        owner: sterne

Start at a Specific Task

ansible-playbook multiple.yml --start-at-task "task name"
  • Pick and choose a step

Ansible Ad-hoc Commands

ansible [target] -m [module] -a "[options]"

Ping Localhost

ansible localhost -m ping

Ansible Ad-hoc File / Package / Service Commands

  • ansible all -m file -a "path=/home/... state=touch"

  • ansible all -m file -a "path=/home/... state=absent"

  • Write / delete a file

  • ansible all -m copy -a "src=/... dest=/..."

  • Copy a file

  • ansible all -m dnf -a "name=telnet state=present"

  • Install package

  • ansible all -m service -a "name=httpd state=started enabled=yes"

  • Start service

  • enabled=yes → at startup

  • ansible all -m shell -a "systemctl status httpd"

  • Check status with shell

  • ansible all -m setup

  • Get information from remote client

  • Example: ansible_os_family == "Ubuntu"

  • ansible client1 -a "/sbin/reboot"

  • Run command directly


Roles → Grouping Tasks into Smaller Playbook

  • Separate long playbook in smaller parts
  • /etc/ansible/roles
  • Example groups mentioned:
    • fullinstall
    • basicinstall
- name: full install
  hosts: east-webservers
  roles:
    - fullinstall

- name: basic install
  hosts: west-webservers
  roles:
    - basicinstall

Create Roles Structure

cd /etc/ansible/roles
  • mkdir [rolenames] → make directory for each role
  • Example:
mkdir basicinstall
  • Create subdirectory tasks
  • Example:
mkdir basicinstall/tasks
  • Create yml files in tasks dir
touch basicinstall/tasks/main.yml

Ansible Galaxy

  • galaxy.ansible.com → many roles
  • ansible-galaxy role install [unclear-role-name]
  • Downloaded in [unclear path ending with /ansible/roles]

Tags

  • Reference or alias to a task
- name: start httpd
  service:
    name: httpd
    state: started
  tags: s-httpd
  • ansible-playbook myplay.yml -t s-httpd

    • Run only a certain part of playbook
  • ansible-playbook myplay.yml --list-tag

    • List all tag in a playbook
  • ansible-playbook myplay.yml --skip-tags s-httpd

    • Skip a task using a tag

Variables

  • Container that hold a defined value repetitively
  • Can be defined in inventory files as well
- name: "install some package"
  hosts: all
  vars:
    myvariable: mypackagename
  tasks:
    - name: package install
      dnf:
        name: "{{ myvariable }}"
        state: started

Variable in Hosts

[abc:vars]
myserver=192.168.0.1000

server1 ansible-host=192.168.0.57

Handlers

  • Execute at the end of the play
  • Use to start, reload, stop service
  • Tasks that only run when notified
tasks:
  - name: ensure apache is running
    service:
      name: httpd
      state: started
    notify: restart apache

handlers:
  - name: restart apache
    service:
      name: httpd
      state: restarted
  • Activate handlers at the end

Conditions

  • Playbook take action on it's own → when
tasks:
  - name: start a service
    when: A == "B"
    service:
      name: servicename
      state: started

Loops

tasks:
  - name: create users
    user:
      name: "{{ item }}"
    loop:
      - jerry
      - kramer
      - george

- name: create users
  hosts: localhost
  vars:
    users: [jerry, kramer, george]

  tasks:
    - name: create user
      user:
        name: "{{ item }}"
      with_items: "{{ users }}"

Ansible Vault → Secure YAML

  • ansible-vault create myplayinvault.yml

    • Create a YAML file in the vault
    • Launch vi editor
  • ansible-playbook myplayinvault.yml --ask-vault-pass

    • To launch encrypted YAML
  • ansible-vault view httpdvault.yml

    • Edit in vi editor
  • ansible-vault --help

    • List of options
  • ansible-vault encrypt myplay.yml


Encrypt Strings in a Playbook

  • ansible-playbook myplay.yml --ask-vault-pass
  • ansible-vault encrypt_string httpd
    • Result copied into playbook
- name: test encrypted
  hosts: localhost
  vars:
    secret: !vault |
      $ANSIBLE_VAULT...
      3u33...

  tasks:
    - name: test
      debug:
        var: secret

Ansible AWX

  • GUI to manage Ansible
  • Node.js in Docker

Ansible Tower

  • Commercial / Red Hat

  • ansible-config → show configuration

  • ansible-connection → connect to client

  • ansible-console → launch console

    • help for module
cp /tmp/myfile /home/remoteuser
  • Copy file from local to remote

  • ansible-doc → manual of plugin / module

ansible-inventory -i hosts --graph
  • See a graph of all inventory
9 months ago Permalien
cluster icon
  • Enroll - Reverse-engineering servers into Ansible : Get an existing Linux host into Ansible in seconds. Enroll inspects a Debian-like or RedHat-like system, harvests the state that matters, and generate...

Kickstart – Automate Linux Install/shaare/MZrKcg

  • linux
  • linux

  1. Kickstart server

  2. Make Kickstart available on the network

  3. Make installation source available

  4. Make boot media available

  5. Start Kickstart installation

    cd /root
    anaconda-ks.cfg   # create for 1st install in root folder
  6. Steps:

    • cp /root/anaconda-ks.cfg /var/www/html/
    • chmod a+r /var/www/html/anaconda-ks.cfg
    • systemctl stop|disable firewalld
    • Create new VM with CentOS DVD ISO

      • Start VM → set boot to DVD
      • Change network adapter to bridged adapter
    • Boot: linux ks=https://192.168.1.x/anaconda-ks.cfg
    • Start automated installation

Example network config:

ksdevice=eth0 ip=192.168.0.50 \
netmask=255.255.255.0 \
gateway=192.168.0.1
9 months ago Permalien
cluster icon
  • File Display Commands / Filters / Text Processing Input : File Display Commands cat → Show entire content cat -A → Show non-printable characters more → Paginate output less → Same as more but allows navigatio...
  • Filesystem check and disk cloning : fsck & xfs_repair → filesystem check fsck → ext2, ext3, ext4 xfs_repair → xfs only Made each boot & fix it df -T → check Unmount before fsck ...
  • Vi Editor : Vi Editor Commands i → insert mode ESC → escape to command mode r → replace mode x → delete character dd → delete line yy → copy line p → paste v → v...
  • Podman and Docker : Containers → Podman Podman → manage pods and container images Buildah → building/pushing/signing container images Skopeo → copy/inspect/delete/signin...
  • Package Management : System Updates & Software Install dnf (yum) → RedHat → /etc/yum.repos.d apt-get → Debian rpm → RedHat package management standalone package to ...


(110)
4 / 6
Liens par page
  • 20
  • 50
  • 100
Filtrer par liens sans tag
Replier Replier tout Déplier Déplier tout Êtes-vous sûr de vouloir supprimer ce lien ? Êtes-vous sûr de vouloir supprimer ce tag ? Le gestionnaire de marque-pages personnel, minimaliste, et sans base de données par la communauté Shaarli