0

I'm writing a playbook and this is what I'm trying to achieve:

Company has a banking application which has components spread on multiple servers. This service can be stopped from a single script or can be stopped component by component. Before applying security Patches (RHEL) I must stop the application.

It should read from the user whether user want to stop the application from AIO script or should stop component by component.

For Packages I wrote:

vars_prompt:                              
  - name: deploypackages                  
    prompt: Do you want to deploy Packages
    private: no

Now is there a way that I can achieve this for application and it should only run when the deploypackages is equal to y

Can we use the when clause in hosts section before tasks? I tried but it didn't do anything - didn't even throw an error so I searched on internet but couldn't find any answer.

1 Answer 1

1

Q: Only run when the 'deploypackages' is equal to 'y'.

A: For example, create an inventory

shell> cat hosts
dummy

[multiple_servers]
host1
host2
host3

and the playbook

shell> cat pb.yml
- hosts: "{{ (deploypackages == 'y')|
             ternary('multiple_servers', 'dummy') }}"
  gather_facts: false
  vars_prompt:
    - name: deploypackages
      prompt: Do you want to deploy Packages
      private: no
  tasks:
    - meta: end_play
      when: inventory_hostname == 'dummy'
    - debug:
        msg: Applying security Patches ...
  • If you don't answer 'y' the play will end
shell> ansible-playbook -i hosts pb.yml
Do you want to deploy Packages: no

PLAY [dummy] *********************************************************************************

TASK [meta] **********************************************************************************

PLAY RECAP ***********************************************************************************
  • If you answer 'y' the play will continue
shell> ansible-playbook -i hosts pb.yml
Do you want to deploy Packages: y

PLAY [multiple_servers] **********************************************************************

TASK [meta] **********************************************************************************
skipping: [host1]

TASK [debug] *********************************************************************************
ok: [host1] => 
  msg: Applying security Patches ...
ok: [host2] => 
  msg: Applying security Patches ...
ok: [host3] => 
  msg: Applying security Patches ...

PLAY RECAP ***********************************************************************************
host1                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
host2                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
host3                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
1
  • Yes, but what about the AIO restart or manual restart, because this is the part which i need to implement
    – OmiPenguin
    Jun 9, 2022 at 6:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .