Monty_emma
Monty_emma

Reputation: 149

what is the cleanest way to run 4 commands 1 after the other using ansible

I am trying to run 4 commands, one after the other, using ansible.

sudo qmicli -d /dev/cdc-wdm0 -p --wds-set-autoconnect-settings=disabled,roaming-allowed
sudo qmicli -d /dev/cdc-wdm0 -p --wds-get-autoconnect-settings
sudo systemctl daemon-reload
sudo systemctl restart redbox-lte.service

- name: run first command
  shell: qmicli -d /dev/cdc-wdm0 -p --wds-set-autoconnect-settings=disabled,roaming-allowed
  become: true

etc. for every command

I was wondering what best practice is; I am sure that must be a better way.

Upvotes: 1

Views: 173

Answers (1)

F1ko
F1ko

Reputation: 4244

The following tasks accomplish what you want to do:

- name: Execute
  become: true
  shell: |
    qmicli -d /dev/cdc-wdm0 -p --wds-set-autoconnect-settings=disabled,roaming-allowed && \
    qmicli -d /dev/cdc-wdm0 -p --wds-get-autoconnect-settings

- name: Restart redbox-lte
  become: true
  systemd:
    state: restarted
    daemon_reload: yes
    name: redbox-lte

However, regarding best practice, you should always make sure to write your ansible scripts in an idempotent matter and print proper tasks statuses. For that reason (especially for the shell module) you should use options like changed_when. Once you ensured that your shell module is only being executed when needed you should go ahead and define the systemd restart as a handler, which will only execute if there was a change in the task that triggers the handler.

Upvotes: 1

Related Questions