I'am trying to define the jvmroute in server.xml of tomcat by naming it node0x. To do that i find i playbook with increment_number with anisble_facts
` - name: set number node
become: yes
become_method: sudo
become_user: "{{env}}adm"
set_fact:
node_number: 1
- name: Nommage des nodes
become: yes
become_method: sudo
become_user: "{{env}}adm"
community.general.xml:
path: /data/{{env}}/web/tomcat/conf/server.xml
xpath: /Server/Service/Engine
attribute: jvmroute
value: "node0{{node_number}}"
backup: true
when: load_balancing == "yes"
- name: increment number node
become: yes
become_method: sudo
become_user: "{{env}}adm"
set_fact:
node_number={{ node_number | int + 1 }}`
and calling this task like this
`- name: set count
set_fact:
node_number: 1
- name: Nommages des nodes tomcat
include_tasks: nommage_nodes.yml
with_items: "{{hostlb}}"
when: not resultchrono.stat.exists`
What i want is on server1 define node01, then server2 define node02 etc..
Can you please help me ?
Q: "On server1 define node01, then server2 define node02 etc."
A: For example, given the inventory
shell> cat hosts
test_01
test_02
test_03
Get indexes and create the names of the variables
my_index: "{{ ansible_play_hosts_all.index(inventory_hostname) + 1 }}"
my_node_var: my_node_{{ my_index }}
gives (abridged)
ok: [test_01] =>
my_node_var: my_node_1
ok: [test_02] =>
my_node_var: my_node_2
ok: [test_03] =>
my_node_var: my_node_3
Set the values
- set_fact:
my_node_{{ my_index }}: 1
gives (abridged)
ok: [test_01] =>
lookup('vars', my_node_var): '1'
ok: [test_02] =>
lookup('vars', my_node_var): '1'
ok: [test_03] =>
lookup('vars', my_node_var): '1'
Increment the variable if you want to
- set_fact:
my_node_{{ my_index }}: "{{ lookup('vars', my_node_var) + 1 }}"
gives (abridged)
ok: [test_01] =>
lookup('vars', my_node_var): '1'
ok: [test_02] =>
lookup('vars', my_node_var): '1'
ok: [test_03] =>
lookup('vars', my_node_var): '1'
Reference the variable
- debug:
var: my_node_1
when: inventory_hostname == 'test_01'
gives (abridged)
ok: [test_01] =>
my_node_1: '2'
skipping: [test_02]
skipping: [test_03]
, or use hostvars to reference any of the variables
- debug:
msg: |
my_node_1: "{{ hostvars.test_01.my_node_1 }}"
my_node_2: "{{ hostvars.test_02.my_node_2 }}"
my_node_3: "{{ hostvars.test_03.my_node_3 }}"
run_once: true
gives
msg: |-
my_node_1: "2"
my_node_2: "2"
my_node_3: "2"
Example of a complete playbook for testing
- hosts: all
vars:
my_index: "{{ ansible_play_hosts_all.index(inventory_hostname) + 1 }}"
my_node_var: my_node_{{ my_index }}
tasks:
- debug:
var: my_index
- debug:
var: my_node_var
- set_fact:
my_node_{{ my_index }}: 1
- debug:
var: lookup('vars', my_node_var)
- set_fact:
my_node_{{ my_index }}: "{{ lookup('vars', my_node_var) + 1 }}"
- debug:
var: lookup('vars', my_node_var)
- debug:
var: my_node_1
when: inventory_hostname == 'test_01'
- debug:
msg: |
my_node_1: "{{ hostvars.test_01.my_node_1 }}"
my_node_2: "{{ hostvars.test_02.my_node_2 }}"
my_node_3: "{{ hostvars.test_03.my_node_3 }}"
run_once: true