0

I am trying to execute this Playbook:

---
- name: Configure System for Oracle 19
  vars_prompt:
    - name: setup_test
      prompt: "Configure System for Test or Production? [Test/Prod]: "
      default: Test
      private: no

  vars_files:
    - /etc/ansible/roles/oracle_19_test/defaults/main.yml


- hosts: "{{ Appserver_STBY }}"
  tasks:
  - name: tnsnames auf Appserver anpassen
    template:
      src: tnsnames_Appserver.ora.j2
      dest: /opt/oracle/app/oracle/product/11.2.0/client_1/network/admin/tnsnames.ora
      owner: oracle
      group: oinstall
      mode: 0644
      backup: yes

- hosts: "{{ Floorserver }}"
  tasks:
  - name: tnsnames auf Floorserver anpassen
    template:
      src: /etc/ansible/roles/oracle_19_test/templates/tnsnames_Floor.ora.j2
      dest: /u01/app/oracle/product/11.2.0/xe/network/admin/tnsnames.ora
      owner: oracle
      group: oinstall
      mode: 0644
      backup: yes
    tags: floortest

No matter what I try to do, when I execute the playbook I get the Prompt for the Variable (although it is not of use now) and immediately after I get ERROR! the field 'hosts' is required but was not set

The hosts should be taken from variables which are defined in that var file. At first I created a Role, but then I realized that roles cannot run on multiple host sections. Can someone help me? According to YAML Checker this is valid Syntax, and I provided the hosts.

EDIT: So I changed my Playbook to this:

---
- hosts: "{{ Appserver_STBY }}"
  vars_files:
    - /etc/ansible/group_vars/all

  tasks:
  - name: tnsnames auf Appserver anpassen
    template:
      src: tnsnames_Appserver.ora.j2
      dest: /opt/oracle/app/oracle/product/11.2.0/client_1/network/admin/tnsnames.ora
      owner: oracle
      group: oinstall
      mode: 0644
      backup: yes

- hosts: "{{ Floorserver }}"
#  vars_files:
#    - /etc/ansible/group_vars/all

  tasks:
  - name: tnsnames auf Floorserver anpassen
    template:
      src: /etc/ansible/roles/oracle_19_test/templates/tnsnames_floor.ora.j2
      dest: /u01/app/oracle/product/11.2.0/xe/network/admin/tnsnames.ora
      owner: oracle
      group: oinstall
      mode: 0644
      backup: yes
    tags: floortest

If I include the variables for every hosts section it works. So now I tried to create a group_vars/all file relative to inventory. If i load the File manually it works. If i dont load it manually , the File will not be loaded and the Section with the Variables in Hostname will fail with:

ERROR! The field 'hosts' has an invalid value, which includes an undefined variable. The error was: 'Floorserver' is undefined

But inside my group_vars/all file the variable is declared:

# Floorserver Hosts
Floorserver: hostxyz

I still dont know what I am doing wrong..

Lucas W
  • 1
  • 1
  • 1
    The error message says it all: your playbook contains 3 plays and the first one does not have a `hosts` field which is required. Since we don't know what your are trying to do exactly with your `vars_prompt`, it's hard to give a solution. But as a first quick guess, removing the dash in front of the first `hosts:` would make this a valid playbook – Zeitounator Aug 03 '23 at 14:29
  • Overread this at first glance: if you are trying to load a `vars_files` once in a play to use variables as `hosts:` in different following plays, this wont work. You will need to target localhost for the first play ans use `add_host` or `set_fact`. But once again, hard to give a precise solution without further information. – Zeitounator Aug 03 '23 at 14:35
  • all the hosts are defined in ansible hosts file. I have a vars_file with all needed variables for the run for all the different host sections involved (like different template variables etc). The prompt will be used to ask the user if he wants to setup the servers for production or test usage. the result shall be used for later purposes. So there is no possibility in setting global variables for a certain playbook? – Lucas W Aug 03 '23 at 15:19
  • 1
    Does [Ansible: How to declare global variable within playbook?](https://stackoverflow.com/a/47393178/6771046) answer your question? – U880D Aug 03 '23 at 18:43
  • I edited my Post with more details – Lucas W Aug 04 '23 at 09:15

2 Answers2

0

You can't exclude host-tags. Try adding hosts: [localhost]. It will accept that to be the target of your task. You can specify connection: local, to be more precise. Every task inside your playbook must have a target to execute it from, even if that task is empty.

If you want, you can create an issue or feature request to Ansible.

Amparo Walter
  • 61
  • 1
  • 8
0

So there is no possibility in setting global variables for a certain playbook?

From within a play and with ansible-playbook, right, that would be the case. The CLI commands do not have the option to Create a Survey like in an Ansible Tower Job Template

Surveys set extra variables for the playbook similar to 'Prompt for Extra Variables' does, but in a user-friendly question and answer way.

except of specifying --extra-vars.

Since it is not possible to "define a variable accessible on a playbook level (global variable) from within a play", it might be feasible to use a workaround under certain circumstances.

A minimal example playbook

---
- name: Playbook Survey on Control Node
  hosts: localhost
  become: false
  gather_facts: false

  vars_prompt:

    - name: setup_test
      prompt: "Configure system for [test/prod]? "
      default: test
      private: no

  tasks:

  - copy:
      dest: group_vars/all
      content: "GLOBAL_VAR: {{ setup_test }}"

- hosts: test
  become: false
  gather_facts: false

  vars_files:

    - group_vars/all

  tasks:

  - debug:
      msg: "{{ GLOBAL_VAR }}"

will result into the output of

Configure system for [test/prod]?  [test]: prod

PLAY [Playbook Survey on Control Node] ********

TASK [copy] ***********************************
ok: [localhost]

PLAY [test] ***********************************

TASK [debug] **********************************
ok: [test1.example.com] =>
  msg: prod
ok: [test2.example.com] =>
  msg: prod

by saving a registered variable into file.

Documentation Links

U880D
  • 8,601
  • 6
  • 24
  • 40