---
# ansible-playbook -e "hosts_group=127.0.0.1 hosts_group2=192.168.3.20 hosts_group3=192.168.3.21 "
- hosts: "{{ hosts_group }}"
remote_user: root
vars:
name1: robin
hosts_group: "localhost"
current_date_time: "{{ ansible_date_time.date }}_{{ ansible_date_time.time }}"
tasks:
- name: show current_date_time
debug:
msg: "{{ current_date_time }}"
- name: add a host var
add_host:
name: var_of_play1
date_time: "{{ current_date_time }}"
- name: use local_action to run add_host
local_action:
module: add_host
hostname: "{{ item }}"
with_items:
- "{{ hosts_group2 }}"
- "{{ hosts_group3 }}"
- hosts: "{{ hosts_group2 }}"
remote_user: root
vars:
hosts_group2: "localhost"
current_date_time: "{{ hostvars['var_of_play1']['date_time'] }}"
tasks:
- name: show currect_date_time
debug:
msg: "{{ current_date_time }}"
- hosts: "{{ hosts_group3 }}"
remote_user: root
vars:
currect_date_time: "{{ hostvars['var_of_play1']['date_time'] }}"
tasks:
- name: show the vars of hosts_group3
debug:
msg: "{{ hostvars['var_of_play1']['date_time'] }}"