How to pass values to an ansible target

59 Views Asked by At

I have this inventory file

[app:children]
CL
#BA
#EA
#EB
#EI

[BA]
TEST-cl1 ansible_user=ec2-user modulename=BA

[CL]
TEST-cl1 ansible_user=ec2-user modulename=CL
TEST-cl1 ansible_user=ec2-user modulename=CL
TEST-cl1 ansible_user=ec2-user modulename=CL

[EA]
TEST-cl1 ansible_user=ec2-user modulename=EA

[EB]
TEST-cl1 ansible_user=ec2-user modulename=EB

[EI]
TEST-cl1 ansible_user=ec2-user modulename=EI

Assume my playbook is like this:

---
- name: RUNNING DEPLOYMENT TASKS
  hosts: app
  connection: ssh
  gather_facts: no
  remote_user: ec2-user
  tasks:
    - name: print visit particulars
      shell: echo {{ modulename }} >> f

What will the file "f" contain?

I would like to see "CL" as all but CL is commented off in that app group. However I see EI which I find quite disappointing (do you get why?)

Since I specify hosts: app in my playbook I thought that the children under app will be selected. Why isn't this so, and how should I code it so that I control where the task is run?

1

There are 1 best solutions below

1
U880D On

What will the file f contain?

Just test with debug module – Print statements during execution.

Since I specify hosts: app in my playbook I thought that the children under app will be selected.

Right, that's the case.

A minimal example with an inventory file of example.com

[BA]
ba.example.com modulename=BA

[CL]
cl.example.com modulename=CL

[app:children]
CL

and a playbook modulename.yml

---
- hosts: app
  become: false
  gather_facts: false

  tasks:

  - debug:
      msg: |
        "modulename: {{ modulename }}"
        "group_names: {{ group_names }}"

called via

ansible-playbook --inventory example.com modulename.yml

will result into an output of

TASK [debug] *********************
ok: [cl.example.com] =>
  msg: |-
    "modulename: CL"
    "group_names: [u'CL', u'app']"

How should I code it so that I control where the task is run?

You may have another look under How to build your inventory, especially Inventory basics: formats, hosts, and groups.