Configuration
List all current configs:
ansible-config listDump current settings:
ansible-config dumpDump current settings different from the defaults:
ansible-config dump --only-changedDisply 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.ymlRun playbook in check-mode 3:
ansible-playbook site.yml --checkRun playbook in step-by-step 4:
ansible-playbook site.yml --stepRun playbook for a specific host:
ansible-playbook site.yml --limit=<hostname>Run playbook for our local host:
ansible-playbook site.yml --limit=<hostname> --connection=localRun 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
findto 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
*.shfiles 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 }}"
Links
setup - Gathers facts about remote hosts — Ansible Documentation↩︎
Ansible change default value according to a condition - Server Fault↩︎
ansible - Accessing a dictionary using another variable as key YAML - Stack Overflow↩︎
find - Return a list of files based on specific criteria — Ansible Documentation↩︎
How to filter, join, map and reduce lists in Ansible with examples.↩︎