Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Documentation as Code

June 21, 2017
Jose Angel Munoz
Related topics:
DevOps

Share:

    Introduction

    With nowadays virtualization technologies, low latency communications, CPU Power and The Cloud, the Infrastructure paradigm is being changed from the static old-fashion way of managing servers to a new standard automation way of deploying services.

    Sysadmins are becoming SysDevs and the widely famous DevOps term is starting to be defined by itself. Of course, the old-fashion way is still there though - and always from my humble point of view - any company with growth objectives will need to start changing the way of doing things in less than five years and start to automate their infrastructure.

    Definition - Still a new concept?

    When typing "Infrastructure as Code" in any Search Provider, about 350K results will be returned including a Wikipedia entry. New books are being published about this subject and it is clear that there is a new tendency when deploying new servers and services.

    There are several approaches when talking about code development techniques and documentation like Wikis, Git Markdown, documentation along with the source, etc.

    But what about doing it altogether? What about doing Documentation as Code?

    I was surprised when at the time of writing this post; there is almost nothing on the Internet when searching for "Documentation as Code". Search providers return almost 14K useless results. You will find that there is not even an entry in the Wikipedia. The Cherryleaf.com blog includes a couple of interesting links and describes it as being able to store the documentation with the code. To me, it is correct but I think we can go one step forward. Let's hold the reins of this and let's complete the definition.

    Documentation as Code is the way of automating documentation along with the written code. It can be when deploying new Infrastructure elements, new services or just a new piece of software or script. Automated Documentation will be able to be exported and shared using external tools like Wikis, Content Management, Git or others.

    The Idea

    Bash Example

    Let's suppose that we want to create a bash script. Our documentation will be together with our script code. The difference is that it will be able to be exported with a flag given. This flag will also allow the exporting of the document in markdown and even in HTML or PDF.

    #!/usr/bin/env bash
    # Example of Documentation as Code
    
    if [ $# -eq 0 ]; then
     echo "$(basename $0) [run]"
    fi
    
    case $1 in
     run)
    
    echo "Here goes the code to run"
     exit
     ;;
    
    doc)
    
    sed '/^:/,/^DAC/!d;s/^:/cat/' "$0" | bash -s "$@"
     exit
     ;;
    
    *) exit
     ;;
    esac
    
    
    : <<DACv1
    ## Documentation Starts
    
    Here you can add all the needed markdown documentation. You can split it as shown in this script.
    DACv1
    
    : <<\DACv2
    ### Documentation Continues
    Here goes the documentation example with a table:
    
    1 | 2 | 3 | 4 | 5
    ---|---|---|---
    T | H | I | S |
    I | S |   | A |
    T | A | B | L | E
    DACv2

    Python Example

    Going on with the same approach, we can do the same with other scripting languages. Here it is my idea with Python.

    #!/usr/bin/env python
    # Example of Documentation as Code
    import optparse
    
    def main():
        parser = optparse.OptionParser(usage='Usage: %prog -o [run]')
        dac_choices = ['run', 'doc', 'Run', 'Doc', 'RUN', 'DOC'] 
        parser.add_option("-o", dest="runordoc",
                          type="choice",
                          choices=dac_choices,
                          help="Please specify if you want Run or Doc")
    
        (opts, args) = parser.parse_args()
    
        if opts.runordoc is None:
            print "A mandatory option is missing\n"
            parser.print_help()
            exit(-1)
    
        if opts.runordoc == "Run" or opts.runordoc == "run" or opts.runordoc =="RUN":
            print "Here goes the code to run"
            exit(0)
    
        elif opts.runordoc == "Doc" or opts.runordoc == "doc" or opts.runordoc =="DOC":
            DACv1 = """## Documentation Starts
    
    Here you can add all the needed markdown documentation. You can split it as shown in this script.
    
    """
    
            DACv2 = """### Documentation Continues
    
    Here goes the documentation example with a table:
    
     1 | 2 | 3 | 4 | 5
    ---|---|---|---
     T | H | I | S |
     I | S |   | A |
     T | A | B | L | E
    """
    
            print DACv1
            print DACv2.strip()
    
    if __name__ == '__main__':
        main()
    

    PowerShell Example

    The "Here-Strings" in PowerShell will do the job. Let's see how it works.

     # Example of Documentation as Code
    
    [CmdletBinding(SupportsShouldProcess=$True)]
    Param(
      [Parameter(Mandatory=$true,Position=0, HelpMessage="Please specify if you want Run or Doc")]
      [ValidateSet("Run","Doc")]
      [string]$RunorDoc = $null
    )
    
    if ($PSCmdlet.ShouldProcess("$RunorDoc","Return Options"))
    
    {
    switch ($RunorDoc)
    {
        "Run" {Write-Output "Here goes the code to run `$RunorDoc = $RunorDoc"}
    
        "Doc" {
    
    # DACv1    
    
    @"
    ## Documentation Starts
    
    Here you can add all the needed markdown documentation. You can split it as shown in this script.
    
    "@
    
    # DACv2
    
    @"
    
    ### Documentation Continues
    
    Here goes the documentation example with a table:
    
    1 | 2 | 3 | 4 | 5
    ---|---|---|---
    T | H | I | S |
    I | S |   | A |
    T | A | B | L | E
    "@
                    }
            }
    }

    Final Step

    From here, we can use tools like Pandoc or AsciiDoctor inside our scripts to automatically convert and post our documentation to a Wiki, GitHub, Content Management, etc. Another approach is keeping the markdown format and using Jekyll to automatically creating amazing documentation portals.

    Conclusion

    Documentation As Code is still a raw concept and I'm sure that it will start to emerge in a few months. To me, it is a procedure together with tools to get the best documentation and revision control along with the code. The way of creating documentation must be simple and the result must be useful.  The scripts shown here are just ideas and I'm sure that once you read this, a lot of new ones will strike your head.


    Download the Eclipse Vert.x cheat sheet, this cheat sheet provides step by step details to let you create your apps the way you want to.

    Last updated: June 20, 2017

    Recent Posts

    • AI meets containers: My first step into Podman AI Lab

    • Live migrating VMs with OpenShift Virtualization

    • Storage considerations for OpenShift Virtualization

    • Upgrade from OpenShift Service Mesh 2.6 to 3.0 with Kiali

    • EE Builder with Ansible Automation Platform on OpenShift

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue