One playbook can load vars files which is updated by another playbook in runtime?

Hi, I have a main.yml which include 1.yml and 2.yml.
1.yml has one task to append a new variable in local vars file - “vars.yml”.
2.yml uses vars file - “vars.yml” and must use the new variable which is appended by 1.yml.

I did some test. 2.yml cannot find out the new variable and it always load the original vars file.

Do you have any solution to resolve my question? Thank you!

I am not sure if dynamically updating vars file, would take effect in the same playbook, probably you can use ‘register’ to add this dynamic variable, and later down access it using hostvars.

Benno

But how to use hostvars in playbook?

I wrote:

  • action: shell echo -n $hostvars[‘localhost.localdomain’].message
    register: msg
  • action: template src=template.conf dest=/root/out.conf owner=root group=root mode=0644

The out.conf content is not value of variable message.

If I wrote:

  • action: shell echo -n ${hostvars[‘localhost.localdomain’].message}

It showed syntax error: invalid arithmetic operator (error token is “.localdomain”)

action: shell echo -n ${hostvars.{localhost.localdomain}.message}
shows:

stderr: /bin/sh: ${hostvars.{localhost.localdomain}.message}: bad substitution

Playbooks should not be dynamically editing yaml variable files.

Do use register, we have also theorized including a “set_variable” action plugin for adding new variables if need be, but in most cases register is all you need.