passing "var" from one playbook to another playbook

116 views
Skip to first unread message

maulik patel

unread,
Aug 22, 2022, 11:35:17 PM8/22/22
to Ansible Project
hi ,

i'm trying to call one playbook1.yaml into master-playbook.yaml and i want to use variable that defined through set_facts into playbook1 into master playbook. but i dont know what is correct way to do...please help me here with better example..

playbook1.yaml: 

      - name: local_ip_fact
        set_fact:
               local_ip: "{{ sites[site]['peer_ip'] | ipaddr('address') }}"
        when: "site in sites.keys()"

master-playbook.yaml: 

---
  - import_playbook: playbook1.yaml:
  - hosts: '{{ localhost | default("PALO_HOST") }}'
    connection: local
    become: yes
    gather_facts: true

.........................................................................

thanks..!!

maulik patel

unread,
Aug 22, 2022, 11:36:07 PM8/22/22
to Ansible Project
here i want to use local_ip , into master playbook...

Rowe, Walter P. (Fed)

unread,
Aug 23, 2022, 7:13:30 AM8/23/22
to ansible...@googlegroups.com
You need to set set_stats to pass "artifacts" back from a playbook to a workflow. Artifacts from upstream playbooks are add to extra_vars in downstream playbooks.

set_stats:
  data:
    local_ip: "{{ sites[site]['peer_ip'] | ipaddr('address') }}"

You can set as many artifacts as you want in a single set_stats task. The artifacts of set_stats CANNOT be used within the playbook they originate. You have to use set_fact for that. So it is conceivable that you would  set_fact AND set_stats in a single playbook. The set_fact task would create a var you can use later within the playbook. The set_stats task would create artifacts for use in downstream playbooks.

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM


-- 
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/2ebd3959-c061-4e01-9315-151a1fb04798n%40googlegroups.com.

maulik patel

unread,
Aug 24, 2022, 2:20:15 AM8/24/22
to Ansible Project

seems like not working for me, not sure if i'm missing anything here...

cat playbook1.yaml
---

- hosts: localhost

  connection: local
  become: yes
  gather_facts: true
  tasks:
    - name: load var
      include_vars: sites.yaml

    - name: site_facts
      set_fact:
           sites: "{{ item }}"
      loop: "{{ site_int }}"

    - name: local_ip_fact

      set_stats:
              data:      
                 local_ip: "{{ sites[site]['peer_ip'] | ipaddr('address') }}"
      when: "site in sites.keys()"

cat master.yaml:

---
- import_playbook: slave.yaml
- hosts: localhost
  gather_facts: false
  tasks:
    - debug:
        msg: "{{ local_ip }}"
  
so basically i need to pass this local_ip, variable from palybook1 to master....but it gives me error about undefined variable...

Rowe, Walter P. (Fed)

unread,
Aug 24, 2022, 7:12:09 AM8/24/22
to ansible...@googlegroups.com
Are you running this in Ansible Tower? This only works in Ansible Tower. It won't work from the command line.


Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM



Vladimir Botka

unread,
Aug 24, 2022, 7:58:38 AM8/24/22
to 'Rowe, Walter P. (Fed)' via Ansible Project
On Wed, 24 Aug 2022 11:11:56 +0000
"'Rowe, Walter P. (Fed)' via Ansible Project"
<ansible...@googlegroups.com> wrote:

> ... This only works in Ansible Tower. It won't work from the command line.

What exactly is the functionality provided by Tower only?

--
Vladimir Botka

Rowe, Walter P. (Fed)

unread,
Aug 24, 2022, 7:59:31 AM8/24/22
to ansible...@googlegroups.com
Because Ansible Tower lets your craft "workflows" that chain together playbooks. Command Line ansible does not do that.

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/20220824135818.54779017%40gmail.com.

maulik patel

unread,
Aug 24, 2022, 9:19:31 AM8/24/22
to ansible...@googlegroups.com
No I’m not using ansible tower. I’m Just making umbrella type of structure that being said I need to call out one play book which has few variable that needs to be also used in master play book.. kind of importing one playbook within another playbook and all variable in that playbook needs to also call out too…

Hope this makes clear..!!

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/5Wk865ez6RY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/20220824135818.54779017%40gmail.com.

Rowe, Walter P. (Fed)

unread,
Aug 24, 2022, 10:10:33 AM8/24/22
to ansible...@googlegroups.com
With one monolithic playbook you can use task files for specific tasks just to break up the "long" file.

- name: task 1 tasks
  include_tasks: taskfile1.yml

- name: task 2 tasks
  include_tasks: taskfile2.yml

- name: task 3 tasks 
  include_tasks: taskfile3.yml

- name: task 4 tasks
  include_tasks: taskfile4.yml

We do this for our server provisioning. The task files can be short or long depending on what they do. In some cases we use roles because they are used also in other playbooks. You main playbook looks cleaner this way and it lets you add items as you mature your service by simply developing the role or task file and adding another stanza like these.

- name: install packages
  include_tasks: ./common/packages.yml

- name: create local users and groups
  include_tasks: ./common/users_groups.yml

- name: join to active directory
  include_role:
    name: nist-sssd-role

- name: configure volumes and filesystems
  include_tasks: ./common/volumes.yml

Hope that helps.

Vladimir Botka

unread,
Aug 24, 2022, 10:31:29 AM8/24/22
to 'Rowe, Walter P. (Fed)' via Ansible Project
On Wed, 24 Aug 2022 11:11:56 +0000
"'Rowe, Walter P. (Fed)' via Ansible Project"
<ansible...@googlegroups.com> wrote:

> ... This only works in Ansible Tower. It won't work from the command line.

Walter,

what exactly is the functionality provided by Tower only?

Thank you,

Best regards

--
Vladimir Botka

Rowe, Walter P. (Fed)

unread,
Aug 24, 2022, 10:34:57 AM8/24/22
to ansible...@googlegroups.com
Tower lets you manage inventories, create "smart inventories", run chains of playbooks together in workflows, have a centralized "controller" that runs your jobs, centralizes logging, lets you authenticate users against LDAP, AD, etc, lets you create "surveys" that fill in vars used in your jobs, integrates with your version control system, lets you store secrets (credentials) that are passed to your playbooks. Centralizing all this on a single host makes your IT security team feel better too. All the security controls around your playbooks and their privileged execution are maintained in a single place they can more easily audit and assess.

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/20220824163109.41d12c68%40gmail.com.

Reply all
Reply to author
Forward
0 new messages