Pass -vvv to Ansible utilities:
ansible -vvv ... ansible-playbook -vvv ...
To debug connection use -vvvv:
ansible -vvvv ...
Print variable value with task debug:
- debug: var=ansible_host - debug: var: ansible_host - debug: msg: "host is {{ ansible_host }}"
To debug Python modules set ANSIBLE_KEEP_REMOTE_FILES to 1 (it causes Ansible to leave the exact copy of the Python scripts it executed on the target machine):
ANSIBLE_KEEP_REMOTE_FILES=1 ansible ...
There is debugger keyword that triggers debugger, see Ansible Debugger
To activate debugger on task:
- name: Execute a command command: false debugger: on_failed
To activate debugger in ansible.cfg:
[defaults] enable_task_debugger = True
To activate debugger via env var:
ANSIBLE_ENABLE_TASK_DEBUGGER=True ansible-playbook -i hosts site.yml
Use ansible-lint:
$ sudo apt install ansible-lint $ ansible-lint site.yml
Variables:
Inventory:
Collections:
Inclusion:
Filters:
Execution:
Files, templates, variables definitions are looked in files, templates, vars role / play directories first, then in the base directory for role / play.
-f option controls forking: how many executors are started to perform tasks.
The way tasks are processed depends on strategy and serial.
strategy * serial can be defined on each hosts in a play:
- hosts: web strategy: free serial: 50% - hosts: db strategy: linear serial: 1
You can dump any variable by debug task in a playbook or using mudule:
ansible $host -m debug -a var=groups
To avoid repetitive checks or to handle failures use block:
block: - shell: echo do 1 - shell: echo do 2 when: not db_started become: true become_user: dba rescue: - shell: echo rollback always: - shell: echo notify
To install specific version use a colon and conditions with operators: ==, !=, >, >=, <, <= separated by comma:
ansible-galaxy collection install 'my_namespace.my_collection:>=1.0.0,<2.0.0'
Only one version of collection can be installed, if you want to update with different version use --force:
ansible-galaxy collection install google.cloud:=0.0.1 ansible-galaxy collection install --force google.cloud:=1.0.1