2

I have a script that will execute in two parts. First it will execute on localhost and query a database table to get a hostname. second part of the script should run on the host which was registered in the query before. I am not able to set the host with the set_fact I did in the first part of the code.

this is what iam trying to do:

- hosts: localhost
  gather_facts: false
  become: yes
  become_user: oracle

  vars_files:
    - vars/main.yml

  tasks:
    - name: Get new hostname
      tempfile:
        state: file
      register: tf

    - name: create sql file
      template:
        src: get_hostname.sql.j2
        dest:"{{ tf.path }}"
        mode: 0775

    - name: login
      command:
        argv:
        - "sqlplus"
        - -s
        - "@{{ tf.path }}"
      environment: 
        ORACLE_HOME: "oracle/home"
      register: command_out

    - set_fact:
        NEW_HOST: "{{ command_out.stdout }}"

- hosts: "{{ NEW_HOST }}"
  gather_facts: false
  become: yes
  become_user: oracle

  vars_file:
    - vars/main.yml

  tasks:
    - name: debug
      command: hostname
      register: new_host_out

    - debug:
        msg: "new host is {{ new_host_out.stdout }}"

Everything works fine in the first part of the code, but errors out at the second part saying it cannot find the NEW_HOST.

1 Answers1

1

Use hostvars to reference such a variable. Create a dummy host to keep this variable. For example, given the inventory

shell> cat hosts
dummy

[test]
test_11
test_12
test_13

The playbook creates the variable. See Delegated facts

shell> cat pb.yml
- hosts: localhost
  tasks:
    - set_fact:
        NEW_HOST: test_12
      delegate_to: dummy
      delegate_facts: true
    - debug:
        var: hostvars.dummy.NEW_HOST

- hosts: "{{ hostvars.dummy.NEW_HOST }}"
  gather_facts: false
  tasks:
    - debug:
        var: inventory_hostname

gives

shell> ansible-playbook pb.yml

PLAY [localhost] ****************************************************************************

TASK [set_fact] *****************************************************************************
ok: [localhost -> dummy]

TASK [debug] ********************************************************************************
ok: [localhost] => 
  hostvars.dummy.NEW_HOST: test_12

PLAY [test_12] ******************************************************************************

TASK [debug] ********************************************************************************
ok: [test_12] => 
  inventory_hostname: test_12

PLAY RECAP **********************************************************************************
localhost: ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test_12  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

You can use localhost for this purpose as well. The playbook below works as expected

- hosts: localhost
  tasks:
    - set_fact:
        NEW_HOST: test_12

- hosts: "{{ hostvars.localhost.NEW_HOST }}"
  gather_facts: false
  tasks:
    - debug:
        var: inventory_hostname
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63