0

During my playbook I create a variable for one set of machines the apps. Namely a password for a user db_password. It is within the group apps only at this point.

I am trying to recall that variable during a later part of the play for a different set of hosts that belong to a different group using hostvars. I have tried a number of things and it doesn't seem to work. Below is the main playbook as well as the main.yml for the 2 roles called.

My problem is it will not show the variable for the db_password when I try and debug it on the db servers. I think I am on the right track with the hostvars concept but open to new ideas. I know I could also write the output of the set_fact db_password to a file and then move it to the db server and have it be read from that but I am trying to keep this within this one playbook. Any thoughts and help is appreciated

- hosts: 127.0.0.1
  connection: local
  gather_facts: true

- hosts: apps
  roles:
- { role: test_app}

- hosts: db
  roles:
- { role: test_db }

main.yml for test_app

- name: "Setup TPMS {{ app_server_hostname }} | GENERATE password"
 set_fact: db_password={{lookup('password', '/dev/null 
 chars=ascii_letters,digits,hexdigits length=10')}}

 - name: user create
   user:
   name: testing
   update_password: always
   password: "{{ db_password}}"
   state: present

main.yml for test_db

- name: show var
  debug: "{{ hostvars[groups['apps'][0]]['db_password'] }}"

current error I am seeing I am expecting the password to return not hello world

 TASK [test_db : show var] 
 ok: [server104] => {
 "msg": "Hello world!"
  • What error do you get from that debug? BTW is that debug valid? Shouldn'it be debug: msg=...? – Zoredache Feb 20 '19 at 18:20
  • Added into my question the current error I am seeing as adding comments didn't give the error in a clean way – deathscythe272 Feb 20 '19 at 18:26
  • Right, and that output brings me to the second part of my comment. The debug module needs a msg or var argument. So - debug: msg='foo' – Zoredache Feb 20 '19 at 19:15
  • thanks for fixing that part of it, now that I am actually getting some return that I was expecting, ie the password for just one machine how would I get a line by line for each pair of app and db servers. Right now it "{{ hostvars[groups['apps'][0]]['db_password'] }}" returns only one password for both database machins. Instead of the 0 would I put a variable in the 0's place indicating the hostname of my app servers? – deathscythe272 Feb 20 '19 at 19:34
  • Well which server do you want? Just use hostvars['appserver1.name'] perhaps? Kinda depends on what the context of how you need to use it. – Zoredache Feb 20 '19 at 19:47
0

The indentation is wrong. Correct indentation is below

- hosts: apps
  roles:
    - { role: test_app}
- hosts: db
  roles:
    - { role: test_db }

.

- name: "Setup TPMS {{ app_server_hostname }} | GENERATE password"
  set_fact:
    db_password: ...
    chars: ...

.

- name: user create
  user:
    name: testing
    update_password: always
    password: "{{ db_password}}"
    state: present
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.