Code execution depending on Tag Values

14 views
Skip to first unread message

Ameya Agashe

unread,
May 20, 2021, 11:43:17 AM5/20/21
to ansible...@googlegroups.com, Ansible Development
Hello Anyone here can guide me on Ansible ?
I have created a role for AEM(Adobe Experience Manager) Author and Publisher. They both have exact same instructions however as per role their variables do change.  One server per role. One server cannot have two roles.

Variable values are to be dictated by tag. We have two or more VM's but Ansible code should grab the value of the variable depending on tag. Has anyone done this before? If yes a code snippet or article would be appreciated.

Example code snippet is below:

- name: Create mnt directory structure
  file:
    path: /mnt/{{ item }}
    state: directory
    owner: cq5
    group: cq5
    mode: 775
  with_items:
    - crx
    - crx/author
    - crx/publish
  tags:
    - aem_author
    - aem_publish

- name: Copy License file on Author
  copy:
    src: license.properties
    dest: "{{ item }}"
    owner: cq5
    group: cq5
    mode: 755
    force: yes
  loop:
    - "{{ author_folder }}"
    - "{{ publishe_folder }}"
  tags:
    - aem_author
    - aem_publish

So as you see in the above snippet, looping variables should be fed as per server. If no tag is given then it should feed in the variable and in fact skip it.

Is this possible at all?
Thanks and Regards,
Ameya Agashe

Roland Müller

unread,
May 21, 2021, 1:05:52 AM5/21/21
to Ansible Project
Hello,

not sure whether I understand your goal right. I assume you want to have given group of tasks for each kind of server/host inside the same playbook.

One possibility would be to organize the playbook tasks inside different blocks and have a hosts definition for each of these blocks. A, B can be single hosts or groups from inventory.

---
- name: block A
  hosts: A
  tasks:
  - name: T1 on a
    debug:
      msg: T1

- name: block B
  hosts: B
  tasks:
  - name: T2 on b
    debug:
      msg: T2

- name: block common
  hosts: all
  tasks:
  - name: T3
    debug:
      msg: T3

BR,
Roland

André Oswald

unread,
May 21, 2021, 1:45:57 AM5/21/21
to Ansible Project
Hi,

the way I understand it you want your Playbook to only copy the license.properties file to the location in `author_folder` if the tag `aem_author` is specified und to the location `publishe_folder` when the playbook is invoked with the `aem_publish` tag?

If this is the case then I think the most common way would be to always copy to the location specified in a variable `dest_folder` which is set per host or group in your inventory. 
If for some reason you have to be able to set it via tags, than I would probably would use `set_fact` in combination with tags to specify the variable content accordingly:

- name: Set destination for Author
  set_fact:
    dest_folder:  "{{ author_folder }}"
  tags:
    - aem_author

- name: Set destination for Publisher
  set_fact:
    dest_folder:  "{{ publishe_folder }}"
  tags:
    - aem_publish

- name: Copy License file on Author
  copy:
    src: license.properties
    dest: "{{ dest_folder }}"

    owner: cq5
    group: cq5
    mode: 755
    force: yes
  tags:
    - aem_author
    - aem_publish

This would probably work but I feel that this really the wrong way to do it unless you have a really specific need.

Cheers,
André
Reply all
Reply to author
Forward
0 new messages