register:
- Used to register variables defined in a module when it is invoked in a task
- The registered data is stored in JSON format
- The value of the variables set using the register clause is always a dictionary, but the specific keys of the dictionary are different, depending on the module that was invoked.
- Every module will have a different dictionary of variables that it will try to gather from the host.
- Below example shows the three dictionary values that were registered when using backup parameter in ios_config module.
ok: [coresw03] => {
"result": {
"backup_path": "/etc/ansible/roles/cisco_ios/backup/coresw03_config.2019-04-28@13:40:06",
"changed": false,
"failed": false
}
}
debug:
msg:
- Used to display the variables registered using the register clause.
- The registered variable is referenced within {{ }} when using the msg parameter.
- This can be used along with a regular message string to provide more context to the variable’s specific dictionary key that was registered.
- To reference only a specific key in the dictionary append the registered variable with the dictionary key that you want to display;
{{ result.backup_path }}
- The msg paramter supports array, so you can reference multiple keys in the same task;
msg:
- "Backup path is {{ result.backup_path }}"
- "Changed: {{ result.changed }}"
debug:
var:
- Used to display the variables registered using the register clause.
- Outputs the variable in raw format and cannot provide any context to the variable’s specific dictionary key that was registered.
- To reference only a specific key in the dictionary append the registered value with the dictionary key that you want to display;
result.backup_path
- The var parameter doesn’t support array. So you cannot reference multiple keys in the same task.
Note:
debug: var and debug: msg are mutually exclusive and cannot be used together in the same task. But they can be used in separate tasks under the same play.
Playbook1 using msg parameter:
- name: Running task_backup_cisco_ios_v3.yaml from /etc/ansible/roles/cisco_ios/tasks directory
ios_config:
backup: yes
register: result
- name: Capture directory where backup file is stored
debug:
msg: "The backup file is saved in {{ result.backup_path }}"
Playbook1 output:
shoaib@ubndsk01:~/Desktop/ansible/projects/project_backup$ ansible-playbook pb_backup_cisco_ios_v1.yml
PLAY [Running pb_backup_cisco_ios_v1.yml from ~/Desktop/ansible/projects/project_backup directory] ************************************************************************************************************
TASK [cisco_ios : Running task_backup_cisco_ios_v3.yaml from /etc/ansible/roles/cisco_ios/tasks directory] ****************************************************************************************************
ok: [coresw03]
TASK [cisco_ios : Capture directory where backup file is stored] **********************************************************************************************************************************************
ok: [coresw03] => {
"msg": "The backup file is saved in /etc/ansible/roles/cisco_ios/backup/coresw03_config.2019-04-28@13:26:18"
}
PLAY RECAP ****************************************************************************************************************************************************************************************************
coresw03 : ok=2 changed=0 unreachable=0 failed=0
Playbook 2 using var parameter:
- name: Running task_backup_cisco_ios_v3.yaml from /etc/ansible/roles/cisco_ios/tasks directory
ios_config:
backup: yes
register: result
- name: Capture directory where backup file is stored
debug:
var: result.backup_path
Playbook2 output:
shoaib@ubndsk01:~/Desktop/ansible/projects/project_backup$ ansible-playbook pb_backup_cisco_ios_v1.yml
PLAY [Running pb_backup_cisco_ios_v1.yml from ~/Desktop/ansible/projects/project_backup directory] ************************************************************************************************************
TASK [cisco_ios : Running task_backup_cisco_ios_v3.yaml from /etc/ansible/roles/cisco_ios/tasks directory] ****************************************************************************************************
ok: [coresw03]
TASK [cisco_ios : Capture directory where backup file is stored] **********************************************************************************************************************************************
ok: [coresw03] => {
"result.backup_path": "/etc/ansible/roles/cisco_ios/backup/coresw03_config.2019-04-28@13:27:48"
}
PLAY RECAP ****************************************************************************************************************************************************************************************************
coresw03 : ok=2 changed=0 unreachable=0 failed=0
Playbook 3 using ‘msg’ and ‘var’ in the same play but under different tasks:
- name: Running task_backup_cisco_ios_v3.yaml from /etc/ansible/roles/cisco_ios/tasks directory
ios_config:
backup: yes
register: result
- name: Capture directory where backup file is stored
debug:
var: result.backup_path
- name: Capture directory where backup file is stored
debug:
msg: "The backup file is saved in {{ result.backup_path }}"
Playbook 3 output:
shoaib@ubndsk01:~/Desktop/ansible/projects/project_backup$ ansible-playbook pb_backup_cisco_ios_v1.yml
PLAY [Running pb_backup_cisco_ios_v1.yml from ~/Desktop/ansible/projects/project_backup directory] ************************************************************************************************************
TASK [cisco_ios : Running task_backup_cisco_ios_v3.yaml from /etc/ansible/roles/cisco_ios/tasks directory] ****************************************************************************************************
ok: [coresw03]
TASK [cisco_ios : Capture directory where backup file is stored] **********************************************************************************************************************************************
ok: [coresw03] => {
"result.backup_path": "/etc/ansible/roles/cisco_ios/backup/coresw03_config.2019-04-28@13:29:06"
}
TASK [cisco_ios : Capture directory where backup file is stored] **********************************************************************************************************************************************
ok: [coresw03] => {
"msg": "The backup file is saved in /etc/ansible/roles/cisco_ios/backup/coresw03_config.2019-04-28@13:29:06"
}
PLAY RECAP ****************************************************************************************************************************************************************************************************
coresw03 : ok=3 changed=0 unreachable=0 failed=0
How to access dictionary values using debug module?
register: output
– debug:
msg: “{{ output.key.key }}”