2

Let assume following:

main-playbook.yml

- name: Play-1
  hosts: localhost
  connection: local
  gather_facts: no
  roles:
    - role: my-role
      vars:
        newhost: 192.168.1.1

generated_playbook.yml

- name: Play-1
  hosts: newhost
  gather_facts: yes
  
  tasks:
  - name: Task1
  - name: Task2
  - name: Task3

main task from the Role:

- name: "Role MAIN-1"
  add_host:
    name: newhost
    ansible_host: "{{newhost}}"

- include: generated_playbook.yml

Error:


ERROR! conflicting action statements: hosts, tasks

The error appears to be in 'generated_playbook.yml': line 1, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- name: Generated Playbook
  ^ here

I create new role and include this role in the main playbook.
In this role I add new host to in-memory inventory and than, I generate new playbook with j2 template example output generated_playbook.yml

Question:
Is there a way to run this new generated playbook only on the new added host but not adding anything else to main playbook?
I was trying to use import-playbook or include inside the role but this fails

0

1 Answer 1

2

It's not possible. Quoting from ansible.builtin.import_playbook

Files with a list of plays can only be included at the top level.

The example explicitly shows this

- name: This DOES NOT WORK
  hosts: all
  tasks:
    - debug:
        msg: task1

    - name: This fails because I'm inside a play already
      import_playbook: stuff.yaml

Notes

  • There is no include_playbook. See what include_* and import_* modules are available.
shell> ansible-doc -t module -l | grep include_

include_role    Load and ...  
include_tasks   Dynamically inclu...
include_vars    Load variables from files, dynamically...
shell> ansible-doc -t module -l | grep import_

import_playbook Imp...        
import_role     Import a ro...
import_tasks    Impo...
  • Quoting note from import_module:

This is a core feature of Ansible, rather than a module, and cannot be overridden like a module.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .