# Playbooks - Tips and Tricks
[toc]
### Ansible Playbooks [Tips and Tricks]
#### Running part of a playbook (Typically for troubleshooting) [Syntax]
````yaml=1
List all tags in the specified playbook:
ansible-playbook <playbookname> --list-tags
Run only specified tags:
ansible-playbook <playbookname> --tags <tag1,tag2>
Run everything except specified tags:
ansible-playbook <playbookname> --skip-tags <tag1,tag2>
Run from a specific play/task:
ansible-playbook:<playbookname> --start-at-task
Step-by-step execution of the playbook:
ansible-playbook <playbookname> --step
## This will cause ansible to stop on each task, and ask if it should execute that task. (y/n/c)
````
#### Running part of a playbook (Typically for troubleshooting) [Examples]
````yaml=1
#### tags.yaml
--- ## Using tags to run selective tasks
- name: create user
hosts: nodes
become: yes
tasks:
- name: create multiple users listed
user:
name: "{{item}}"
state: present
loop:
- sk12k
- saif
- hari
- juarez
- dani
tags:
- users
- user
## Play to install multiple packages
- name: install packages
hosts: nodes
become: yes
tasks:
- name: install packages mentioned below
apt:
name: "{{item}}"
state: latest
loop:
- tree
- git
- elinks
- telnet
tags:
- pkgs
List all tags in playbook:
ansible-playbook tags.yaml --list-tags
run tasks with specific tags:
ansible-playbook tags.yaml --tags users,pkgs
skip specified tags:
ansible-playbook tags.yaml --skip-tags users,pkgs
start at a particular task:
ansible-playbook tags.yaml --start-at-task 'install packages mentioned below'
step by step execution of the playbook:
ansible-playbook tags.yaml --step
````
:mag: Additional Read:
- https://docs.ansible.com/ansible/latest/user_guide/playbooks_tags.html
- https://docs.ansible.com/ansible/latest/user_guide/playbooks_startnstep.html
#### Special tags: [always and never]
Ansible reserves two tag names for special behavior: always and never. If you assign the always tag to a task or play, Ansible will always run that task or play, unless you specifically skip it (--skip-tags always).
````yaml=1
--- ### Special tags example for always and never
tasks:
- name: Print a message
ansible.builtin.debug:
msg: "Always runs"
tags:
- always
- name: Print a message
ansible.builtin.debug:
msg: "runs when you use tag1"
tags:
- tag1
````
### ansible-playbook flags and other options
#### Checking the list of hosts for a playbook
:::warning
If you want to see a list of hosts that would be affected by your playbook before you actually run it, use --list-hosts:
:::
````yaml=
ansible-playbook playbook.yml --list-hosts
Running this should give output like:
playbook: playbook.yml
play #1 (all): host count=4
127.0.0.1
192.168.24.2
foo.example.com
bar.example.com”
````
:mag: Reference: From Ansible for DevOps
#### Setting remote user and Sudo options
````yaml=
Explicitly define a remote user:
ansible-playbook elinks.yml --remote-user=sk12k
Ask sudo password on remote host:
ansible-playbook <playbook-name> -b --ask-sudo-pass
````
#### Limiting playbooks to specific host / hostgroup
````yaml=
ansible-playbook <playbook-name> --limit <hostname/hostgroup-name >
````
#### Provide alternate hostfile or inventory
````yaml=!
--inventory=PATH (-i PATH): Define a custom inventory file (default is the default Ansible inventory file, usually located at /etc/ansible/hosts).
````
#### Verbose mode output
````yaml=!
--verbose (-v): Verbose mode (show all output, including output from successful options). You can pass in -vvvv to give every minute detail.
````
#### Initialize variables defined in playbook at runtime
````yaml=!
--extra-vars=VARS (-e VARS): Define variables to be used in the playbook, in "key=value,key=value" format.
````
#### Define number of forks (parallelism)
````yaml=!
--forks=NUM (-f NUM): Number for forks (integer). Set this to a number higher than 5 to increase the number of servers on which Ansible will run tasks concurrently.
````
#### Define local connection type (other than ssh) for localhost
````yaml=!
--connection=TYPE (-c TYPE): The type of connection which will be used (this defaults to ssh; you might sometimes want to use local to run a playbook on your local machine, or on a remote server via cron).
````
#### Dry run a playbook
````yaml=!
--check: Run the playbook in Check Mode (‘Dry Run’); all tasks defined in the playbook will be checked against all hosts, but none will actually be run.”
````
:mag: For full options checkout --> https://docs.ansible.com/ansible/latest/cli/ansible-playbook.html