Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Documentation as Code

June 21, 2017
Jose Angel Munoz
Related topics:
DevOps

    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

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    • Fun in the RUN instruction: Why container builds with distroless images can surprise you

    • Trusted software factory: Building trust in the agentic AI era

    • Build a zero trust AI pipeline with OpenShift and RHEL CVMs

    • Red Hat Hardened Images: Top 5 benefits for software developers

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    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
    © 2026 Red Hat

    Red Hat legal and privacy links

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

    Chat Support

    Please log in with your Red Hat account to access chat support.