Skip to content

Instantly share code, notes, and snippets.

@BruceZu
Last active December 27, 2016 19:30
Show Gist options
  • Select an option

  • Save BruceZu/af591f42da5d0b83a974e5884721d685 to your computer and use it in GitHub Desktop.

Select an option

Save BruceZu/af591f42da5d0b83a974e5884721d685 to your computer and use it in GitHub Desktop.
Ansible debug module
Return
loop
block
error handling
task name and variable
@BruceZu
Copy link
Author

BruceZu commented Dec 23, 2016

block:

 - block:
   when:
   rescue:
   always:

with_first_found
loop control

# main.yml
- include: inner.yml
  with_items:
    - 1
    - 2
    - 3
  loop_control:
    loop_var: outer_item

# inner.yml
- debug: msg="outer item={{ outer_item }} inner item={{ item }}"
  with_items:
    - a
    - b
    - c

loop_control:
label: "{{item.name}}"

- name: create servers
  digital_ocean: name={{item.name}} state=present ....
  with_items:
    - name: server1
      disks: 3gb
      ram: 15Gb
      network:
        nic01: 100Gb
        nic02: 10Gb
        ...
  loop_control:
    label: "{{item.name}}"

loop_control:
pause: 3

Do-Until Loops link

 - action: shell /usr/bin/foo
  register: result
  until: result.stdout.find("all systems go") != -1
  retries: 5
  delay: 10

loop: loop

  • one command with different variable
  • more command
  with_items:
  with_dict:   # elements of a hash
  with_nested:
  with_file:  #  the content of each file
  with_fileglob  # match a pattern
  with_together # Parallel Sets  
  with_subelements: 
  with_sequence:
  with_random_choice:
  with_indexed_items:
  with_ini: #use regexp to retrieve a set of keys
  with_flattened: # iterate over every item in  a really crazy  datastructure
  with_items: "{{ groups['all'] }}"
  with_items: "{{ play_hosts }}"
  with_inventory_hostnames:
  when: 

work with register debug

   -- name
      debug
         var:
         verbosity
         msg:
         when:

Return: common return values

warnings: list of strings
exception:   -vvv
stdout_lines
stdout
stderr_lines
stderr
changed: boolean
failed: boolean
skipped: boolean

the registered variable’s string contents are accessible with the ‘stdout’ value

      - shell: cat /etc/motd
        register: motd_contents

      - shell: echo "motd contains the word hi"
        when: motd_contents.stdout.find('hi') != -1

The registered result can be used in the “with_items” of a task if it is converted into a list (or already is a list)
“stdout_lines” is already available on the object
could also call “home_dirs.stdout.split()”

      - name: retrieve the list of home directories
        command: ls /home
        register: home_dirs

      - name: add home dirs to the backup spooler
        file: path=/mnt/bkspool/{{ item }} src=/home/{{ item }} state=link
        with_items: "{{ home_dirs.stdout_lines }}"
        # same as with_items: "{{ home_dirs.stdout.split() }}"

check the registered variable’s string contents

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment