We use serial
in nearly all of our playbooks but there are occasions where we need to make a quick change and it's unnecessary for the Ansible to abide by the serial
restriction.
Is there a way to override serial
from the command line with a flag as part of the ansible-playbook
command?
Code example:
- hosts: database
serial: 1
become: yes
Many thanks in advance!
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]#
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With