Answers: <p>This looks like you are trying to solve a problem (how express a desired state using appropriate OS-specific strategies), but selecting the "wrong" approach.</p>
  
  <p>I would suggest that instead of maintaining separate playbooks (one for Windows, one for Debian, <em>etc</em>), you should write an OS-independent <a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html" rel="nofollow noreferrer">role</a>. The role will use facts from the target machines to apply the relevant configuration. This can be done in a few ways :</p>
  
  <ol>
  <li>Set OS-specific variables and include them in the tasks using a <code>include_vars: "{{ ansible_os_family }}.yml"</code> which will pick up a <code>Debian.yml</code> set of vars <em>e.g.</em>. You could then write <code>when</code> conditions on tasks to execute OS-specific tasks</li>
  <li>Write OS-specific tasks to be included in the <code>tasks/main.yml</code> <em>e.g.</em> </li>
  </ol>
  
  <pre><code># tasks/main.yml
  - import_tasks: "{{ ansible_os_family }}.yml"
  
  </code></pre>
  
  <p>This will afford you some benefits:</p>
  
  <ol>
  <li>You will be able to test the role independently for different scenarios (using <a href="https://molecule.readthedocs.org" rel="nofollow noreferrer">molecule</a> <em>e.g.</em></li>
  <li>You will be able to maintain the configuration (vars, tasks, etc) independently in separate files for respective OS's.</li>
  <li>You won't have to maintain two separate playbooks</li>
  <li>Your role will be easier to re-use</li>
  </ol>
  
  <p>See <a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse.html" rel="nofollow noreferrer">writing re-usable playbooks</a> on the Ansible documentation.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-06-30T10:18:35.037Z
Answers: 2019-06-30T10:18:35.037Z
Answers: Bruce Becker
Answers: <p>the best way I think is to create a var  yml file and include it in your ansible-books:</p>
  
  <pre><code>    - name: Include vars of stuff.yaml into the 'stuff' variable (2.2).
        include_vars:
          file: stuff.yaml
          name: stuff
  </code></pre>
  
  <p>see <a href="https://docs.ansible.com/ansible/latest/modules/include_vars_module.html" rel="nofollow noreferrer">include_vars_module</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2019-06-29T07:57:18.537Z
Answers: 2019-06-29T07:57:18.537Z
Answers: LinPy
Body: <p>I have an ansible playbook <code>add_dns_record.yml</code> that register the A record and relative PTR of an host to our MS DNS server.</p>
  
  <pre><code>- hosts: my_ad_dns
    remote_user: "{{ lookup('env', 'USER') }}@MYDOMAIN.COM"
    gather_facts: false
    tasks:
    - name: Create A record
      win_dns_record:
        computer_name: "{{ inventory_hostname }}"
        name: "{{ dns_a_name }}"
        type: "A"
        value: "{{ dns_ip_address }}"
        zone: "MYDOMAIN.COM"
        state: present
  
    - name: Create PTR
      ...
  </code></pre>
  
  <p>I have another playbook <code>debian_base.yml</code> used to prepare debian servers:</p>
  
  <pre><code>- hosts: debian
    tasks:
  
      - name: install python
        raw: test -e /usr/bin/python || (apt -y update &amp;&amp; apt install -y python-minimal)
  
      - name: Set hostname
        hostname:
          name: "{{ inventory_hostname }}"
  
  
      - name: Update repositories cache and install pre-requirements
        apt:
          name: "{{ packages}}"
          update_cache: yes
        vars:
          packages:
          - sudo
          - apt-transport-https
          - cloud-guest-utils
          - open-vm-tools
          - screen
          - dirmngr
          - openjdk-8-jdk-headless
          - dos2unix
          ....
  </code></pre>
  
  <p>I want that every debian server deployed using the playbook <code>debian_base.yml</code> get registered on the MS DNS.</p>
  
  <p>I had a look to <a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_includes.html#importing-playbooks" rel="nofollow noreferrer"><code>import_playbook</code></a> but it doesn't accept variables.</p>
  
  <p>What are the best practices to achieve that?</p>
  
Tags: <ansible>
Title: Ansible nested playbooks with variables
UserDisplayName: sgargel
ThreadText: # Ansible nested playbooks with variables
  
  (Asked by: username_0 on 2019-06-28)
  
  I have an ansible playbook `add_dns_record.yml` that register the A record and relative PTR of an host to our MS DNS server.
  
  ```
  - hosts: my_ad_dns
    remote_user: "{{ lookup('env', 'USER') }}@MYDOMAIN.COM"
    gather_facts: false
    tasks:
    - name: Create A record
      win_dns_record:
        computer_name: "{{ inventory_hostname }}"
        name: "{{ dns_a_name }}"
        type: "A"
        value: "{{ dns_ip_address }}"
        zone: "MYDOMAIN.COM"
        state: present
  
    - name: Create PTR
      ...
  
  ```
  
  I have another playbook `debian_base.yml` used to prepare debian servers:
  
  ```
  - hosts: debian
    tasks:
  
      - name: install python
        raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal)
  
      - name: Set hostname
        hostname:
          name: "{{ inventory_hostname }}"
  
      - name: Update repositories cache and install pre-requirements
        apt:
          name: "{{ packages}}"
          update_cache: yes
        vars:
          packages:
          - sudo
          - apt-transport-https
          - cloud-guest-utils
          - open-vm-tools
          - screen
          - dirmngr
          - openjdk-8-jdk-headless
          - dos2unix
          ....
  
  ```
  
  I want that every debian server deployed using the playbook `debian_base.yml` get registered on the MS DNS.
  
  I had a look to [`import_playbook`](https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_includes.html#importing-playbooks) but it doesn't accept variables.
  
  What are the best practices to achieve that?
  
  **Tags:** ansible
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2019-06-30. Score: 1 | Accepted answer
  
  This looks like you are trying to solve a problem (how express a desired state using appropriate OS-specific strategies), but selecting the "wrong" approach.
  
  I would suggest that instead of maintaining separate playbooks (one for Windows, one for Debian, *etc*), you should write an OS-independent [role](https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html). The role will use facts from the target machines to apply the relevant configuration. This can be done in a few ways :
  
  1. Set OS-specific variables and include them in the tasks using a `include_vars: "{{ ansible_os_family }}.yml"` which will pick up a `Debian.yml` set of vars *e.g.*. You could then write `when` conditions on tasks to execute OS-specific tasks
  2. Write OS-specific tasks to be included in the `tasks/main.yml` *e.g.*
  
  ```
  # tasks/main.yml
  - import_tasks: "{{ ansible_os_family }}.yml"
  
  ```
  
  This will afford you some benefits:
  
  1. You will be able to test the role independently for different scenarios (using [molecule](https://molecule.readthedocs.org) *e.g.*
  2. You will be able to maintain the configuration (vars, tasks, etc) independently in separate files for respective OS's.
  3. You won't have to maintain two separate playbooks
  4. Your role will be easier to re-use
  
  See [writing re-usable playbooks](https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse.html) on the Ansible documentation.
  
  
  ---
  
  ### Answer by username_2 on 2019-06-29. Score: 1
  
  the best way I think is to create a var yml file and include it in your ansible-books:
  
  ```
      - name: Include vars of stuff.yaml into the 'stuff' variable (2.2).
        include_vars:
          file: stuff.yaml
          name: stuff
  
  ```
  
  see [include\_vars\_module](https://docs.ansible.com/ansible/latest/modules/include_vars_module.html)
  
  
  ---
  
  