1

Currently I'm searching for a solution, where I can create a Variable and then import the Playbook with the variable.

Here is my code:

- name: test
  hosts: localhost
  vars:
    ansible_python_interpreter: /usr/bin/python3
    playbook1: server_provision
    playbook2: server_provision_oracle
  gather_facts: no
  tasks:
  - name: define Variable with when condition
    set_fact:
      server_provision: "{{ playbook1 }}"
    when: OS == "RHEL"

  - name: store variable to file
    copy:
      content: "server_provision: {{ server_provision }}"
      dest: /tmp/server_provision.yml
    when: OS == "RHEL"

  - name: define Variable with when condition
    set_fact:
      server_provision: "{{ playbook2 }}"
    when: OS == "Oracle"

  - name: store variable to file
    copy:
      content: "server_provision: {{ server_provision }}"
      dest: /tmp/server_provision.yml
    when: OS == "Oracle"


- name: test
  vars_files:
    - /tmp/server_provision.yml
  import_playbook: "{{ server_provision }}.yaml"

But when I execute the playbook, i'm getting the error, that the variable server_provision has no value.

The Problem is that my unimportet playbook doesn't get executet, it starts with the importet one.

Do you have a solution for this?

VallingSki
  • 133
  • 4

1 Answers1

1

You can use pre_tasks. Pre Taks are executed before the tasks section. Ansible Docs

Something like this:

---
  hosts: localhost
  vars:
    ansible_python_interpreter: /usr/bin/python3
    playbook1: server_provision
    playbook2: server_provision_oracle
  gather_facts: no

  vars_files:
    - /tmp/server_provision.yml

  pre_tasks:
  - name: define Variable with when condition
    set_fact:
      server_provision: "{{ playbook1 }}"
    when: OS == "RHEL"

  - name: store variable to file
    copy:
      content: "server_provision: {{ server_provision }}"
      dest: /tmp/server_provision.yml
    when: OS == "RHEL"

  - name: define Variable with when condition
    set_fact:
      server_provision: "{{ playbook2 }}"
    when: OS == "Oracle"

  - name: store variable to file
    copy:
      content: "server_provision: {{ server_provision }}"
      dest: /tmp/server_provision.yml
    when: OS == "Oracle"

  tasks:
    - name: test
      import_playbook: "{{ server_provision }}.yaml"
Spirit
  • 631
  • 7
  • 11
  • At which point, did you get this error? After the include or at the pre_tasks? – Spirit Dec 15 '20 at 16:17
  • After the pre_tasks. In the tasks where he should include the playbook – VallingSki Dec 15 '20 at 16:24
  • You can get more information with the `-v` command line switch. More `-vvvv` = more detailed output – Spirit Dec 15 '20 at 16:29
  • I get this;ESTABLISH SSH CONNECTION FOR USER SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o -o ConnectTimeout=10 -o ControlPath=/tmp/awx_1428_h8t3shy7/cp/16f8e91cd -tt '/bin/sh -c /usr/bin/python3 /tmp/ansible-tmp-1608049946.7190742-16657-173060652088279/AnsiballZ_import_playbook.py /home//.ansible/tmp/ansible-tmp-1608049946.7190742-16657-173060652088279/args && sleep 0'"'"'' – VallingSki Dec 15 '20 at 16:34
  • Shared connection to XXXX closed. – VallingSki Dec 15 '20 at 16:35
  • I don't know what your imported playbook is doing. And maybe it is a problem with this. Furthermore I think maybe this is a topic for a new question. Sry that I'm not able to help you here. – Spirit Dec 15 '20 at 16:41