-3

I'm trying to create a playbook that will first modify another playbook, then run that modified version of the playbook. But import_playbook pre-processes, so the original playbook, not the modified, is run. Is there any way to tell Ansible to re-read this playbook? Note I am changing more that the hosts, but using this simple example.

- name: Change the playbook
  hosts: localhost
  connection: local
  gather_facts: False

  tasks:
    - name: Change the playbook
      lineinfile:
        path: "my_playbook.yml"
        regexp: "- hosts: some_group_name"
        line: "- hosts: some_other_group_name"

- name: Include the playbook that was modified
  import_playbook: my_playbook.yml

Zeitounator
  • 38,476
  • 7
  • 53
  • 66
Joe Friedeggs
  • 129
  • 1
  • 1
  • 8
  • 2
    This describes what you're trying to do, but not why you're trying to do it. It's quite possible that if you describe your actual problem, someone will be able to tell you a reasonable way to solve it. – flowerysong Mar 27 '22 at 18:03
  • 2
    You most probably don't want to do that. As specified in the comment above, please add more information about the exact problem you are trying to solve with this bad ansible practice. See [X/Y problem](https://xyproblem.info) – Zeitounator Mar 28 '22 at 07:42

1 Answers1

0

With proper organized inventory, group_vars, variables, templating and add_host or dynamic inventory, there is no need for self-modifying code (SMC).

Regarding

But import_playbook pre-processes, so the original playbook ...

you may have a look into What's the difference between include_tasks and import_tasks?.

Furthermore, there is no dynamic include_playbook.

U880D
  • 8,601
  • 6
  • 24
  • 40