<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Ansible - How to check if a group exist. If it exist, then create Host Objects to that group in Ansible</title>
    <link>https://community.checkpoint.com/t5/Ansible/Ansible-How-to-check-if-a-group-exist-If-it-exist-then-create/m-p/116990#M528</link>
    <description>&lt;P&gt;Seems like you'd use fail in the playbook.&lt;BR /&gt;But you should check the group first, then create the host object.&lt;BR /&gt;My guess is you'd have a task like the following before your create host task:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;   - name: check-if-group-exists-if-not-create
     check_point.mgmt.cp_mgmt_group_facts
       name: groupTest
   - fail:
       check_point.mgmt.cp_mgmt_host:
         name: groupTest
         state: present
         members: Host1
         auto_publish_session: yes
       delegate_to: Global&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;However: I have not tested this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 27 Apr 2021 04:33:25 GMT</pubDate>
    <dc:creator>PhoneBoy</dc:creator>
    <dc:date>2021-04-27T04:33:25Z</dc:date>
    <item>
      <title>Ansible - How to check if a group exist. If it exist, then create Host Objects to that group</title>
      <link>https://community.checkpoint.com/t5/Ansible/Ansible-How-to-check-if-a-group-exist-If-it-exist-then-create/m-p/116723#M525</link>
      <description>&lt;P&gt;How do I check if a Group Exists in a domain using Ansible, and if it exists add a host to the group, if it doesn't exist, create a new group and then add a host to that Group.&lt;BR /&gt;&amp;nbsp;&lt;STRONG&gt;&lt;BR /&gt;playbook.yml&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;---
- hosts: check_point
  connection: httpapi
  gather_facts: False
  vars_files:
    - 'my_var.yml'
    - 'login.yml'
   
   tasks:
   - name: add-host-if-group-exist
     check_point.mgmt.cp_mgmt_host:
       name: Host1
       ip_address: 10.1.2.3
       comments: 'FWP'
       state: present
       groups: groupTest
       auto_publish_session: yes
     delegate_to: Global

   - name: add-group-if-group-doesn't exist-and-create-host
     check_point.mgmt.cp_mgmt_host:
       name: groupTest
       state: present
       members: Host1
       auto_publish_session: yes
     delegate_to: Global

# let's check if the group exists, if it doesn't create a new group and add a host. If a group exists add new host to that group&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Apr 2021 12:11:46 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/Ansible/Ansible-How-to-check-if-a-group-exist-If-it-exist-then-create/m-p/116723#M525</guid>
      <dc:creator>ukohae</dc:creator>
      <dc:date>2021-04-23T12:11:46Z</dc:date>
    </item>
    <item>
      <title>Re: Ansible - How to check if a group exist. If it exist, then create Host Objects to that group</title>
      <link>https://community.checkpoint.com/t5/Ansible/Ansible-How-to-check-if-a-group-exist-If-it-exist-then-create/m-p/116839#M526</link>
      <description>&lt;P&gt;I'm by no means an expert in Ansible.&lt;BR /&gt;But I assume you could execute check_point.mgmt.cp_mgmt_group_facts and either add to the existing group or create the group based on the result.&lt;BR /&gt;Tagging&amp;nbsp;&lt;a href="https://community.checkpoint.com/t5/user/viewprofilepage/user-id/41271"&gt;@Or_Soffer&lt;/a&gt;&amp;nbsp;in case he has a more precise idea.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Apr 2021 04:23:22 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/Ansible/Ansible-How-to-check-if-a-group-exist-If-it-exist-then-create/m-p/116839#M526</guid>
      <dc:creator>PhoneBoy</dc:creator>
      <dc:date>2021-04-26T04:23:22Z</dc:date>
    </item>
    <item>
      <title>Re: Ansible - How to check if a group exist. If it exist, then create Host Objects to that group</title>
      <link>https://community.checkpoint.com/t5/Ansible/Ansible-How-to-check-if-a-group-exist-If-it-exist-then-create/m-p/116988#M527</link>
      <description>&lt;P&gt;Hi, can I get a working code example?&lt;/P&gt;</description>
      <pubDate>Tue, 27 Apr 2021 04:18:09 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/Ansible/Ansible-How-to-check-if-a-group-exist-If-it-exist-then-create/m-p/116988#M527</guid>
      <dc:creator>ukohae</dc:creator>
      <dc:date>2021-04-27T04:18:09Z</dc:date>
    </item>
    <item>
      <title>Re: Ansible - How to check if a group exist. If it exist, then create Host Objects to that group</title>
      <link>https://community.checkpoint.com/t5/Ansible/Ansible-How-to-check-if-a-group-exist-If-it-exist-then-create/m-p/116990#M528</link>
      <description>&lt;P&gt;Seems like you'd use fail in the playbook.&lt;BR /&gt;But you should check the group first, then create the host object.&lt;BR /&gt;My guess is you'd have a task like the following before your create host task:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;   - name: check-if-group-exists-if-not-create
     check_point.mgmt.cp_mgmt_group_facts
       name: groupTest
   - fail:
       check_point.mgmt.cp_mgmt_host:
         name: groupTest
         state: present
         members: Host1
         auto_publish_session: yes
       delegate_to: Global&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;However: I have not tested this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Apr 2021 04:33:25 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/Ansible/Ansible-How-to-check-if-a-group-exist-If-it-exist-then-create/m-p/116990#M528</guid>
      <dc:creator>PhoneBoy</dc:creator>
      <dc:date>2021-04-27T04:33:25Z</dc:date>
    </item>
    <item>
      <title>Re: Ansible - How to check if a group exist. If it exist, then create Host Objects to that group</title>
      <link>https://community.checkpoint.com/t5/Ansible/Ansible-How-to-check-if-a-group-exist-If-it-exist-then-create/m-p/117049#M529</link>
      <description>&lt;P&gt;I ran the sample code. I get a console error message&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;---
- name: Global Objects
  hosts: Global
  connection: httpapi
  gather_facts: False
  vars_files:
    - 'credentials/my_var.yml'
    - 'credentials/login.yml'

  tasks:
  - name: check-if-group-exists-if-not-create
    check_point.mgmt.cp_mgmt_group_facts:
      name: gTest1A

  - name: fail
    fail:
      check_point.mgmt.cp_mgmt_host:
        name: Host1
        state: present
        #members: Host1
        auto_publish_session: yes
      #delegate_to: Global&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Error Message&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;TASK [fail] ********************************************************************************************************************************************************************************
fatal: [Global]: FAILED! =&amp;gt; {"changed": false, "msg": "Invalid options for fail: check_point.mgmt.cp_mgmt_host"}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;a href="https://community.checkpoint.com/t5/user/viewprofilepage/user-id/7"&gt;@PhoneBoy&lt;/a&gt;&amp;nbsp;&lt;a href="https://community.checkpoint.com/t5/user/viewprofilepage/user-id/14683"&gt;@Jonas_Rosenboom&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Apr 2021 13:50:43 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/Ansible/Ansible-How-to-check-if-a-group-exist-If-it-exist-then-create/m-p/117049#M529</guid>
      <dc:creator>ukohae</dc:creator>
      <dc:date>2021-04-27T13:50:43Z</dc:date>
    </item>
    <item>
      <title>Re: Ansible - How to check if a group exist. If it exist, then create Host Objects to that group</title>
      <link>https://community.checkpoint.com/t5/Ansible/Ansible-How-to-check-if-a-group-exist-If-it-exist-then-create/m-p/117068#M530</link>
      <description>&lt;P&gt;The fail should be associated with checking the existence of the group, which it's not in your code sample.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Apr 2021 15:15:35 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/Ansible/Ansible-How-to-check-if-a-group-exist-If-it-exist-then-create/m-p/117068#M530</guid>
      <dc:creator>PhoneBoy</dc:creator>
      <dc:date>2021-04-27T15:15:35Z</dc:date>
    </item>
    <item>
      <title>Re: Ansible - How to check if a group exist. If it exist, then create Host Objects to that group</title>
      <link>https://community.checkpoint.com/t5/Ansible/Ansible-How-to-check-if-a-group-exist-If-it-exist-then-create/m-p/117072#M531</link>
      <description>&lt;P&gt;If you can't use Ansible according to it's philosophy due to your environment, it might be easier to find a better tool for the job.&lt;/P&gt;
&lt;P&gt;via&amp;nbsp;&lt;A href="https://www.ansible.com/blog/ansible-best-practices-essentials" target="_blank" rel="noopener"&gt;https://www.ansible.com/blog/ansible-best-practices-essentials&lt;/A&gt;:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;gt; &lt;EM&gt;Ansible is a desired state engine by design. If you’re trying to “write code” in your plays and roles, you’re setting yourself up for failure. Our YAML-based playbooks were never meant to be for programming.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Check out our&amp;nbsp;&lt;A href="https://github.com/CheckPointSW/cp_mgmt_api_python_sdk" target="_blank" rel="noopener"&gt;Check Point API Python Development Kit&lt;/A&gt;&amp;nbsp;and &lt;A href="https://sc1.checkpoint.com/documents/latest/api_reference/index.html" target="_blank" rel="noopener"&gt;API documentation&lt;/A&gt; instead. A short Python script might cover all your cases and give you quick results.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Apr 2021 15:47:38 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/Ansible/Ansible-How-to-check-if-a-group-exist-If-it-exist-then-create/m-p/117072#M531</guid>
      <dc:creator>Jonas_Rosenboom</dc:creator>
      <dc:date>2021-04-27T15:47:38Z</dc:date>
    </item>
  </channel>
</rss>

