\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Gael.langlais} \pdfinfo{ /Title (ansible-playbook.pdf) /Creator (Cheatography) /Author (Gael.langlais) /Subject (Ansible Playbook Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Ansible Playbook Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Gael.langlais} via \textcolor{DarkBackground}{\uline{cheatography.com/169063/cs/36547/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Gael.langlais \\ \uline{cheatography.com/gael-langlais} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 26th January, 2023.\\ Updated 12th July, 2024.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Playbook}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{-{}-{}- \newline - name: Create FW rule \newline hosts: all \newline gather\_facts: no \newline roles: \newline - role: robertdebock.mysql \newline become: yes \newline vars: \newline mysql\_user\_name: tony \newline tasks: \newline - include-tasks: tasks/useful.yml \newline - firewalld: \newline var: ansible\_facts` \newline port: "\{\{ http\_port | default('88') \}\}/tcp" \newline service: http \newline source: 192.0.0.0/24 \newline zone: public \newline state: enabled \newline permanent: yes \#(persist reboot) \newline immediate: yes \#(enable now) \newline \newline - name: Generate password \newline shell: pwgen -N 1 -s 30 \newline register: myPass \newline \newline - name: Print the generated password \newline debug: \newline msg: "The password is \{\{ myPass \}\}";} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{- When the option expects a variable as parameter, you don't need double curly breaths around variables. \newline - When line start with a variable like on the `{\emph{port}}` line above, you {\bf{must}} use double quote " around the variable. \newline - Boolean value accept: {\bf{{\emph{yes}}, {\bf{true}}, {\bf{True}}, {\bf{TRUE}}}} and {\bf{{\emph{no}}, {\bf{NO}}, {\bf{false}}, {\bf{FALSE}}}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{page jump}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Block}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{- hosts: server1 \newline tasks: \newline - name: Install git and restart nginx \newline block: \newline - name: install Git \newline yum: name=git state=present \newline - name: Restart nginx \newline service: name=nginx state=restarted \newline become: yes \newline when: ansible\_facts{[}'distribution'{]}=='CentOS' \newline rescue: \# Do this if the task fail \newline - mail: \newline to: admin@domain.com \newline subject: Houston, We've Got a Problem \newline body: task \{\{ \seqsplit{ansible\_failed\_task.name} \}\} \newline always: \# Always do this \newline - mail: \newline to: user@domain.com \newline subject: "\{\{ ansible\_play\_name \}\} status" \newline body: "\{\{ ansible\_failed\_result \}\}"} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Variables precedences}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Link to Ansible doc: \seqsplit{https://shorturl.at/HMZ05}% Row Count 1 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Magic Variables}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{hostvars{[}'srv1'{]}.ip\_address}} & Get vars value defined for another host \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{hostvars{[}'srv1'{]}.ansible\_facts.mounts}} & Get facts value from another host \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} {\bf{hostvars{[}'srv1'{]}{[}ansible\_facts{]}{[}mounts{]}}} & Same as above writen diffrently \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} {\bf{group\_names}} & Get all groups the current host is part of \tn % Row Count 11 (+ 3) % Row 4 \SetRowColor{LightBackground} {\bf{inventory\_hostname}} & Get the current host name define in inventiry not the FQDN \tn % Row Count 14 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.92 cm} x{4.08 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Performances tuning}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{gather\_facts: False}} & Disable facts gathering \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{forks=50}} (in ansible.cfg) & or {\bf{-{}-forks 50}} or {\bf{-f 50}} on CLI (default is {\bf{5}}) \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{{[}ssh\_connection{]}}} (in ansible.cfg)\{\{nl\}\}{\bf{ssh\_args = -o ControlMaster=auto -o ControlPersist=60s}}} \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} {\bf{pipelining = True}} (in ansible.cfg) & Reduce the number of SSH connections \tn % Row Count 10 (+ 2) % Row 4 \SetRowColor{LightBackground} {\bf{strategy: free}} & Execute tasks without waiting for other hosts to finish their tasks \tn % Row Count 14 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Use mitogen if you can: \newline https://mitogen.networkgenomics.com/ansible\_detailed.html} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Inventory}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{all: \newline hosts: \newline mail.example.com: \newline ansible\_host: 192.168.1.230 \newline ansible\_user: mailmin \newline children: \newline webservers: \newline hosts: \newline frcllweb001: \newline ansible\_host: 192.168.1.231} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{ansible.cfg precedence}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{1. {\bf{\$ANSIBLE\_CONFIG}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{2. {\bf{ansible.cfg in playbook dir}}} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{3. {\bf{\textasciitilde{}/.ansible.cfg}}} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{4. {\bf{/etc/ansible/ansible.cfg}}} \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.12 cm} x{4.88 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Error Handling}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{any\_errors\_fatal:} true & Will stop \& exit playbook if any error \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{ignore\_errors:} true & Execute next tasks on failed hosts \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} failed\_when: \textless{}condition\textgreater{} & Task fail when condition is met \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Avoid using {\bf{shell}} or {\bf{command}} modules, they will simply execute the command without any validations, use the appropriate module.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Ansible tips \& triks}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Make your playbook executable}} \newline % Row Count 1 (+ 1) Add the following sheebang at the top of your palybook: \newline % Row Count 3 (+ 2) {\bf{`\#!/usr/bin/ansible`}} \newline % Row Count 4 (+ 1) and add the executable bit to your playbook file \newline % Row Count 5 (+ 1) {\bf{chmod +x \textless{}my\_playbook.yml\textgreater{}}} \newline % Row Count 6 (+ 1) Now you can execute your playbook like a regular script {\bf{./my\_playbook.yml}}% Row Count 8 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}