Override Ansible playbook `serial` from command line
you can pass a variable from cli with -e flag, and use that to the serial
attribute. example (adding a default value of 3 in case you dont pass the variable value in cli):
- hosts: ping_test
serial: "{{ serial_number|default(3) }}"
gather_facts: true
vars:
tasks:
- name: task 1
debug:
var: serial_number
run as:
ansible-playbook <playbook file> -e serial_number=1
sample output (2 executions):
[root@optima-ansible ILIAS]# ansible-playbook -i hosts serial.yml -e serial_number=1
PLAY [ping_test] ****************************************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************************************
ok: [localhost]
TASK [task 1] *******************************************************************************************************************************************************************************************************
ok: [localhost] => {
"serial_number": "1"
}
PLAY [ping_test] ****************************************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************************************
ok: [greenhat]
TASK [task 1] *******************************************************************************************************************************************************************************************************
ok: [greenhat] => {
"serial_number": "1"
}
PLAY RECAP **********************************************************************************************************************************************************************************************************
greenhat : ok=2 changed=0 unreachable=0 failed=0
localhost : ok=2 changed=0 unreachable=0 failed=0
[root@optima-ansible ILIAS]# ansible-playbook -i hosts serial.yml -e serial_number=2
PLAY [ping_test] ****************************************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************************************
ok: [greenhat]
ok: [localhost]
TASK [task 1] *******************************************************************************************************************************************************************************************************
ok: [localhost] => {
"serial_number": "2"
}
ok: [greenhat] => {
"serial_number": "2"
}
PLAY RECAP **********************************************************************************************************************************************************************************************************
greenhat : ok=2 changed=0 unreachable=0 failed=0
localhost : ok=2 changed=0 unreachable=0 failed=0
[root@optima-ansible ILIAS]#