Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override Ansible playbook `serial` from command line

Tags:

ansible

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!

like image 630
jmvbxx Avatar asked Apr 25 '18 16:04

jmvbxx


1 Answers

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]# 
like image 94
ilias-sp Avatar answered Nov 09 '22 10:11

ilias-sp