Skip to content

Ansible

Configuration

  • List all current configs:

      ansible-config list
  • Dump current settings:

      ansible-config dump
  • Dump current settings different from the defaults:

      ansible-config dump --only-changed
  • Disply current config file:

      ansible-config view

Facts

  • Gather facts about remote host 1:

      ansible --module-name setup <hostname>

Commands

  • Run a command on a specific host 2:

      ansible --module-name shell --args 'ls -l /etc' <hostname>

Playbook

  • Run playbook on all hosts:

      ansible-playbook site.yml
  • Run playbook in check-mode 3:

      ansible-playbook site.yml --check
  • Run playbook in step-by-step 4:

      ansible-playbook site.yml --step
  • Run playbook for a specific host:

      ansible-playbook site.yml --limit=<hostname>
  • Run playbook for our local host:

      ansible-playbook site.yml --limit=<hostname> --connection=local
  • Run only specific tags from playbook 5:

      ansible-playbook site.yml --tags 'configuration,packages'
  • Skip specific tags from playbook:

      ansible-playbook site.yml --skip-tags 'install,notification'

Examples

  • Set a variable based on a condition 6:

      - name: Set DPKG architecture
        set_fact:
          dpkg_arch: "{{ 'amd64' if ansible_architecture == 'x86_64' else ansible_architecture }}"
  • Use a variable as the name of another variable 7:

      - name: Install package for architecture
        apt:
          deb: '{{ item }}'
          state: present
        with_items: '{{ packages[ansible_architecture] }}'
  • Use find to list files and use them on another task 8:

      - name: Find currently installed docs
        find:
          path: /usr/share/doc
          recurse: True
          excludes: 'copyright'
        register: doc
      - name: Remove found docs
        file:
          path: '{{ item.path }}'
          state: absent
        with_items: '{{ doc.files }}'
        when: 'doc.files is defined and doc.files|length > 0'
  • Extract downloaded package and symlink *.sh files 9:

      - name: Download scripts
        unarchive:
          src: https://github.com/ruanyf/simple-bash-scripts/archive/master.tar.gz
          dest: /opt/scripts/
          extra_opts:
            - --strip=1
          remote_src: True
          list_files: True
        register: unpack
      - name: Create symlinks to scripts
        file:
          src: '/opt/scripts/{{ item }}'
          dest: '/usr/local/bin/{{ item }}'
          state: link
        with_items: "{{ unpack.files | select('match', '^.*sh$') | list }}"

  1. setup - Gathers facts about remote hosts — Ansible Documentation↩︎

  2. Introduction To Ad-Hoc Commands — Ansible Documentation↩︎

  3. Check Mode (“Dry Run”) — Ansible Documentation↩︎

  4. Start and Step — Ansible Documentation↩︎

  5. Tags — Ansible Documentation↩︎

  6. Ansible change default value according to a condition - Server Fault↩︎

  7. ansible - Accessing a dictionary using another variable as key YAML - Stack Overflow↩︎

  8. find - Return a list of files based on specific criteria — Ansible Documentation↩︎

  9. How to filter, join, map and reduce lists in Ansible with examples.↩︎