-2

I have 3 applications hosted on three separate hosts and mentioned in the my.hosts file like below:

[app1_webapp]
host3.mybank.com

[app2_webapp]
host5.mybank.com

[app3_webapp]
host8.mybank.com

My requirement is to run two types of raw module commands.

  1. First raw task which should run on all three hosts for all three applications, i.e uptime

  2. Second raw task is ps command that should run only on the respective host, i.e ps -ef | grep app1 should only run on host3.mybank.com

Below is how I call

ansible-playbook -i my.hosts main.yml -e appname=app1,app2,app3

and my main.yml

- hosts: "{{ product(appname.split(',')) | product(['webapp'])|map('flatten')|map('join', '_') }}"

  user: user1
  gather_facts: no

  tasks:

    - name: Check Running Process
      raw: "ps -ef | grep {{ item }}"
      register: psout
      with_items: "{{ appname.split(',') }}"

    - name: DUMP Running Process
      debug: 
        msg: "{{ psout.stdout }}"

The above raw fails as it tried ps for each app on each hosts which is what I wish to skip (correct).

How do I put a when condition so that the ps command for the respective app should run on the respective host only and not on all three hosts?

U880D
  • 8,601
  • 6
  • 24
  • 40
Ashar
  • 2,942
  • 10
  • 58
  • 122
  • 2
    I would say you are facing a [XY problem](https://xyproblem.info), where you want us to solve your trial at a problem that should not be solved this way. Rather, [group your inventory by function](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#example-group-by-function), and either assign some variable to each group defining what the correct `raw` command should be or run multiple plays (that can live in the same playbook) each targeted at a specific function. – β.εηοιτ.βε Jul 11 '22 at 18:35
  • @β.εηοιτ.βε my problem is regarding standard practice in infrastructure management. Each app runs on its own host as specified in the `my.hosts` file. I wish to find if the app process `ps -ef | grep ` is running on its own host(desired) and not on all `inventory-hosts`. – Ashar Jul 11 '22 at 21:39
  • 1
    Then you should run all the `ps -ef` on all hosts, and have an `assert`to see if the correct process is running. The condition fro the `assert` would come from a group variable because you sorted the hosts by functions. So basically, the answer is the same: make yourself a proper inventory and your issue won't be one anymore. – β.εηοιτ.βε Jul 12 '22 at 11:29

1 Answers1

1

As already mentioned in the comments the proper approach would be to group the inventory by function ...


Given an inventory of

[app1_test]
test1.example.com

[app2_test]
test2.example.com

a small test playbook

---
- hosts: app1_test,app2_test
  become: false
  gather_facts: false

  vars:

    APPNAME: "app1,app2"

  tasks:

  - name: Check on
    debug:
      msg: "{{ item }} on {{ group_names }}"
    when: group_names is search(item)
    with_items: "{{ APPNAME.split(',') }}"

is resulting into the requested output of

TASK [Check on] *************************
ok: [test1.example.com] => (item=app1) =>
  msg: app1 on [u'app1_test']
ok: [test2.example.com] => (item=app2) =>
  msg: app2 on [u'app2_test']

How do I put a when condition so that ... the respective app should run on the respective host only

This means, you need to check (only) if the "app" is contained within the respective group name(s). One host can belong to multiple groups.

Further Q&A

Documentation

U880D
  • 8,601
  • 6
  • 24
  • 40