Show Menu

Ansible Playbook Cheat Sheet by

Ansible Playbook reminder


- name: Create FW rule
  hosts: all
  gather_facts: no
  - role: robertdebock.mysql
    become: yes
      mysql_user_name: tony
  - include-tasks: tasks/useful.yml
  - firewalld:
      var: ansible_facts`
      port: "{{ http_port | default('88') }}/tcp"
      service: http
      zone: public
      state: enabled
      permanent: yes #(persist reboot)
      immediate: yes #(enable now)

  - name: Generate password
    shell: pwgen -N 1 -s 30
    register: myPass

  - name: Print the generated password
      msg: "The password is {{ myPass }}";
- When the option expects a variable as parameter, you don't need double curly breaths.
- When line start with a variable like on the
line above, you must use double quote " around the variable.
- Boolean value accept: yes, true, True TRUE. Same for false


- hosts: server1
  - name: Install git and restart nginx
    - name: install Git
      yum: name=git state=present
    - name: Restart nginx
      service: name=nginx state=restarted
    become: yes
    when: ansible_facts['distribution']=='CentOS'
    rescue: # Do this if the task fail
     - mail:
         subject: Houston, We've Got a Problem
         body: task {{ }}
    always: # Always do this
    - mail:
        subject: "{{ ansible_play_name }} status"
        body: "{{ ansible_failed_result }}"

Variables preced­ences

Magic Variables

Get vars value defined for another host
Get facts value from another host
Same as above writen diffrently
Get all groups the current host is part of
Get the current host name define in inventiry not the FQDN

Perfor­mances tuning

gather­_facts: False
Disable facts gathering
forks=50 (in ansibl­e.cfg)
or --forks 50 or -f 50 on CLI (default is 5)
[ssh_c­onn­ection] (in ansible.cfg)
ssh_args = -o Contro­lMa­ste­r=auto -o Contro­lPe­rsi­st=60s
pipelining = True (in ansibl­e.cfg)
Reduce the number of SSH connec­tions
strategy: free
Execute tasks without waiting for other hosts to finish their tasks




 ­ ­ ­mai­l.e­xam­


 ­ ­ ­web­ser­vers:

 ­ ­ ­ ­ ­hosts:

 ­ ­ ­ ­ ­ ­ ­frc­llw­eb001:

 ­ ­ ­ ­ ­ ­ ­ ­ ­ans­ibl­e_host: 192.16­8.1.231

ansibl­e.cfg precedence

2. ansibl­e.cfg in playbook dir
3. ~/.ans­ibl­e.cfg
4. /etc/a­nsi­ble­/an­sib­le.cfg

Error Handling

any_er­ror­s_f­atal: true
Will stop & exit playbook if any error
ignore­_er­rors: true
Execute next tasks on failed hosts
failed­_when: <co­ndi­tio­n>
Task fail when condition is met
Avoid using shell or command modules, they will simply execute the command without any valida­tions, use the approp­riate module.


No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Ansible Cheat Sheet