0

asking for Ansible guru to help figure out. is it possible or not by Ansible

  1. Jdk is downloaded and extracted to /apps/tools/jdk_1.8.121

  2. binaries - /apps/tools/jdk_1.8.121/bin

needed

each binary in /apps/tools/jdk_1.8.121/bin/java, java, javadoc,

... .... and so on approximately 40 items, push to loop to alternatives programm

alternatives --install /usr/bin/java java /apps/tools/jdk_1.8.121/bin/java 1000 alternatives --install /usr/bin/{{ var }} {{var}} /apps/tools/jdk_1.8.121/bin/{{var}} 1000 

1 Answer 1

1

That's pretty easy, you just have to register the result of the find module and loop over it with with_items:

- hosts: hosts tasks: - name: Find binaries find: paths: /apps/tools/jdk_1.8.121/bin register: bins - name: Install alternatives command: "update-alternatives --install /usr/bin/{{ item.path|basename }} {{ item.path|basename }} {{ item.path }} 1000" with_items: "{{ bins.files }}" 

Or, using the alternatives module:

 - name: Install alternatives alternatives: name: "{{ item.path|basename }}" link: "/usr/bin/{{ item.path|basename }}" path: "{{ item.path }}" priority: 1000 with_items: "{{ bins.files }}" 

With Ansible 2.5 or newer, it is recommended to use loop instead of with_items:

 - name: Install alternatives alternatives: name: "{{ item.path|basename }}" link: "/usr/bin/{{ item.path|basename }}" path: "{{ item.path }}" priority: 1000 loop: "{{ bins.files|flatten(levels=1) }}" 
9
  • if I need to remove everything .. is it possibe to re use the module again ? Commented Nov 4, 2019 at 22:49
  • i guess this is not best practice ... but if i will use this approach - name: Remove SAP JDK New approach command: 'update-alternatives --remove "{{ item.path|basename}}" "/apps/tools/jdk_1.8.121/bin/java8/bin/{{ item.path|basename }}"' with_items: "{{ bins.files }}"ice but what if i will use this approach Commented Nov 4, 2019 at 23:06
  • Why should it not be best practice? Of course you can use it. Commented Nov 5, 2019 at 5:17
  • i was told that this approach is not acceptable, we need to use loop approach. could you help to do that Commented Nov 6, 2019 at 9:29
  • I could, but that loops are explained in every basic shell scripting tutorial. And it wouldn't be ansible. Commented Nov 6, 2019 at 9:37

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.