Feedback

Chat Icon

AWX in Action

Ansible Orchestration at Scale

How to Get the Most Out of This Book
10%

Conventions

Heredoc

In this guide, we'll make extensive use of heredoc to create files. Heredoc is a redirection method in Bash that lets you pass multi-line input to a command. Here is an example:

cat <file.txt
Passwords are like underwear:
- You shouldn’t leave them out where people can see them.
- You should change them regularly.
- You shouldn’t loan them out to strangers.
EOF

Here, <is the heredoc delimiter. The heredoc ends when the delimiter is encountered at the beginning of a line. The > operator redirects the output to a file. As a result, when you copy and paste the whole block into your terminal, it will create a file named file.txt with the content:

Passwords are like underwear:
- You shouldnt leave them out where people can see them.
- You should change them regularly.
- You shouldnt loan them out to strangers.

When >> is used instead of >, the content will be appended to the file instead of overwriting it.

When a variable is used inside a heredoc, it will be expanded. For example:

# export an environment variable
export ARGUMENT="world"

# create a file with the variable expanded
cat <file.txt
Hello, $ARGUMENT!
EOF

AWX in Action

Ansible Orchestration at Scale

Enroll now to unlock all content and receive all future updates for free.