<?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 - Add members to a group in Ansible</title>
    <link>https://community.checkpoint.com/t5/Ansible/Ansible-Add-members-to-a-group/m-p/116200#M522</link>
    <description>&lt;P&gt;If you&amp;nbsp;&lt;STRONG&gt;ignore_warnings&lt;/STRONG&gt; as discussed before it will create the additional host objects, and you can use the original list you've created from the CSV.&lt;/P&gt;
&lt;P&gt;For the other options, you can register the result in a variable (e.g. &lt;STRONG&gt;result&lt;/STRONG&gt;).&amp;nbsp;&lt;SPAN&gt;Starting in Ansible 1.6.1, the results registered with multiple items are stored in &lt;STRONG&gt;result.results&lt;/STRONG&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;as an array. You then need to create a filter to weed out the bad ones and use the result.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;See:&amp;nbsp;&lt;A href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html" target="_blank"&gt;https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;However, I advise against this approach for anything but a temporary solution, as it goes against the Ansible concept of Idempotency, and will make your future life much harder.&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 16 Apr 2021 15:24:43 GMT</pubDate>
    <dc:creator>Jonas_Rosenboom</dc:creator>
    <dc:date>2021-04-16T15:24:43Z</dc:date>
    <item>
      <title>Ansible - Add members to a group</title>
      <link>https://community.checkpoint.com/t5/Ansible/Ansible-Add-members-to-a-group/m-p/116071#M515</link>
      <description>&lt;P&gt;Is there a way to &lt;EM&gt;&lt;STRONG&gt;read all host objects&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp; in a &lt;STRONG&gt;&lt;EM&gt;csv file&lt;/EM&gt;&lt;/STRONG&gt; and add them to a member of a group using&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&lt;STRONG&gt;cp_mgmt_group module&lt;/STRONG&gt;?&lt;BR /&gt;&lt;BR /&gt;my playbook only adds the last host ( &lt;STRONG&gt;gTest103, 10.1.2.5, FWP - Applying changes&lt;/STRONG&gt; ) to a member,&amp;nbsp; &lt;EM&gt;&lt;STRONG&gt;gTest1A,&lt;/STRONG&gt;&lt;/EM&gt; in my playbook. Is there a reason why it adds only the last host gTest103? and then it removes the other hosts&lt;/P&gt;&lt;P&gt;Network.csv&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Name,IP,Comments
gTest101,10.1.2.3,FWP - Testing this
gTest102,10.1.2.4,FWP - Applying this
gTest103,10.1.2.5,FWP - Applying changes&lt;/LI-CODE&gt;&lt;LI-CODE lang="markup"&gt;---
- name: Global Objects
  hosts: check_point
  connection: httpapi
  gather_facts: False
  vars_files:
    - 'credentials/my_var.yml'
    - 'credentials/login.yml'

  tasks:
  - name:  read-csv-file
    read_csv:
      path: file_reader/Networks.csv
      key: Name
    register: user

  - name: add-host-object
    check_point.mgmt.cp_mgmt_host:                    
      name: "{{ item.value.Name | quote }}"           
      ip_address: "{{ item.value.IP | quote }}"       
      comments: "{{ item.value.Comments }}"           
      state: present
      auto_publish_session: yes
    loop: "{{ user.dict | dict2items }}"               
    ignore_errors: yes
    delegate_to: Global

  - name: add-network-group
    check_point.mgmt.cp_mgmt_group:
      name: gTest1A
      comments: "anything"
      state: present
      members:
        - "{{ item.value.Name }}"
      auto_publish_session: yes
    loop: "{{ user.dict|dict2items }}"
    ignore_errors: 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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Apr 2021 12:22:53 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/Ansible/Ansible-Add-members-to-a-group/m-p/116071#M515</guid>
      <dc:creator>ukohae</dc:creator>
      <dc:date>2021-04-15T12:22:53Z</dc:date>
    </item>
    <item>
      <title>Re: Ansible - Add members to a group</title>
      <link>https://community.checkpoint.com/t5/Ansible/Ansible-Add-members-to-a-group/m-p/116118#M516</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;you are using &lt;STRONG&gt;loop&lt;/STRONG&gt; here, which causes multiple calls to the &lt;EM&gt;&lt;STRONG&gt;cp_mgmt_group&lt;/STRONG&gt; &lt;/EM&gt;module, each specifying the group to have ONE member (and no others). That's why you end up with the last host from your CSV, because the last call changes the group to contain exactly that host (and no other objects).&lt;/P&gt;
&lt;P&gt;With the CSV parsed like in your example, the following would be a valid approach.&lt;BR /&gt;It creates a list from all the dictionary keys present (which in your example is the name for the object), and provides the full list as the member parameter to the group module.&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;  - set_fact:
      hosts_in_group: "{{ user.dict | dict2items | map(attribute='key') | list }}"

  - name: add-network-group
    check_point.mgmt.cp_mgmt_group:
      name: ExampleGroup
      state: present
      members: "{{ hosts_in_group }}"
      auto_publish_session: yes&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;This requires the CSV to contain a complete list of all the objects you want to be present in the group.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Apr 2021 12:48:12 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/Ansible/Ansible-Add-members-to-a-group/m-p/116118#M516</guid>
      <dc:creator>Jonas_Rosenboom</dc:creator>
      <dc:date>2021-04-15T12:48:12Z</dc:date>
    </item>
    <item>
      <title>Re: Ansible - Add members to a group</title>
      <link>https://community.checkpoint.com/t5/Ansible/Ansible-Add-members-to-a-group/m-p/116138#M518</link>
      <description>&lt;P&gt;hi, in the code below, if the host already exist in gTest1A or shows any error of multiple objects with the same IPs exist if a new host is added, I am trying to tell it to ignore existing host or objects with multiple IPs and create the new host with a unique IP into the group gTest2A (&lt;EM&gt;&lt;STRONG&gt;gTest105,&amp;nbsp;10.1.2.5, FWP -Ansible&lt;/STRONG&gt;&lt;/EM&gt;) , it doesn't create the new host &lt;STRONG&gt;gTest105, 10.1.2.5, FWP -Ansible&lt;BR /&gt;&lt;BR /&gt;Note: gTest1A already exist, but gTest2A needs to be created&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;gTest1A.csv&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Name,IP,Comments
gTest101,10.1.2.3,FWP - Test,
gTest102,10.1.2.4, FWP - Fix,
&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;gTest2A.csv&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Name,IP,Comments
gTest103,10.1.2.3,FWP - New Comments,
gTest104,10.1.2.4, FWP - New Comments by user,
gTest105,10.1.2.5,FWp - Ansible,
&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;playbook.yml&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;---
- name: Global Objects
  hosts: check_point
  connection: httpapi
  gather_facts: False
  vars_files:
    - 'credentials/my_var.yml'
    - 'credentials/login.yml'

  tasks:
  - name:  read-csv-file
    read_csv:
      path: file_reader/gTest2A.csv
      key: Name
    register: user


  - name: add-host-object
    check_point.mgmt.cp_mgmt_host:                  
      name: "{{ item.value.Name | quote }}"           
      ip_address: "{{ item.value.IP | quote }}"       
      comments: "{{ item.value.Comments }}"           
      state: present
      auto_publish_session: yes
    loop: "{{ user.dict | dict2items }}"               
    ignore_errors: yes
    delegate_to: Global

  - name: wait for session to be refreshed
    wait_for:
      timeout: 30

  - set_fact:
      hosts_in_group: "{{ user.dict | dict2items | map(attribute='key') | list }}"


  - name: add-network-group
    check_point.mgmt.cp_mgmt_group:
      name: gTest2A
      comments: "something different"
      state: present
      members: "{{ hosts_in_group }}"
      auto_publish_session: yes
    ignore_errors: yes
    delegate_to: Global &lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Apr 2021 18:44:42 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/Ansible/Ansible-Add-members-to-a-group/m-p/116138#M518</guid>
      <dc:creator>ukohae</dc:creator>
      <dc:date>2021-04-15T18:44:42Z</dc:date>
    </item>
    <item>
      <title>Re: Ansible - Add members to a group</title>
      <link>https://community.checkpoint.com/t5/Ansible/Ansible-Add-members-to-a-group/m-p/116140#M519</link>
      <description>&lt;P&gt;Based on your playbook, &lt;EM&gt;gTest105&lt;/EM&gt; should always be created, while &lt;EM&gt;gTest103&lt;/EM&gt; and &lt;EM&gt;gTest104&amp;nbsp;&lt;/EM&gt;will fail validation due to another object with the same IP being present.&lt;/P&gt;
&lt;P&gt;This will however cause the &lt;STRONG&gt;cp_mgmt_group&lt;/STRONG&gt; module to fail, because it expects &lt;EM&gt;gTest103&lt;/EM&gt; and &lt;EM&gt;gTest104&amp;nbsp;&lt;/EM&gt;to be present.&lt;BR /&gt;If you want to create &lt;EM&gt;gTest103&lt;/EM&gt; and &lt;EM&gt;gTest104&lt;/EM&gt; even if other objects with the same IP address exist, you can add "&lt;STRONG&gt;ignore_warnings: true&lt;/STRONG&gt;" to the &lt;STRONG&gt;&lt;U&gt;module&lt;/U&gt; parameters&lt;/STRONG&gt;. Be aware that this may suppress other validation warnings too.&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;  - name: add-host-object
    check_point.mgmt.cp_mgmt_host:
      name: "{{ item.value.Name | quote }}"
      ip_address: "{{ item.value.IP | quote }}"
      comments: "{{ item.value.Comments }}"
      state: present
      ignore_warnings: yes
    loop: "{{ user.dict | dict2items }}"
    ignore_errors: yes
&lt;/LI-CODE&gt;
&lt;P&gt;Note that &lt;STRONG&gt;ignore_warnings&lt;/STRONG&gt; and&amp;nbsp;&lt;FONT color="#000000"&gt;&lt;STRONG&gt;ignore_errors&lt;/STRONG&gt;&lt;/FONT&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;apply at different levels. At the module level we ignore the validation warning, and at the task level we ignore any errors during module execution. It might be a good idea to remove the &lt;STRONG&gt;ignore_errors&amp;nbsp;&lt;/STRONG&gt;to fail EARLY in case we are facing an actual problem (instead of failing later on when creating the group).&lt;/P&gt;
&lt;P&gt;If you wanted to skip creating any hosts that already exist with the same IP and a different name, you would have to append the output of each successful call of &lt;STRONG&gt;cp_host_mgmt&lt;/STRONG&gt; to a separate list and set the group members based on that.&lt;/P&gt;
&lt;P&gt;If you are certain that creation of &lt;EM&gt;gTest105&lt;/EM&gt; fails from your playbook (instead of it just missing from the group), please check the error message for clues and/or share it here.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Apr 2021 19:26:40 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/Ansible/Ansible-Add-members-to-a-group/m-p/116140#M519</guid>
      <dc:creator>Jonas_Rosenboom</dc:creator>
      <dc:date>2021-04-15T19:26:40Z</dc:date>
    </item>
    <item>
      <title>Re: Ansible - Add members to a group</title>
      <link>https://community.checkpoint.com/t5/Ansible/Ansible-Add-members-to-a-group/m-p/116147#M520</link>
      <description>&lt;P&gt;Thanks very much,&amp;nbsp; I am still a notice at this. Referring to this statement "&lt;EM&gt;&lt;STRONG&gt;I&lt;/STRONG&gt;&lt;/EM&gt;&lt;EM&gt;&lt;SPAN&gt;&lt;STRONG&gt;f you wanted to skip creating any hosts that already exist with the same IP and a different name, you would have to append the output of each successful call of&amp;nbsp;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;STRONG&gt;cp_host_mgmt&amp;nbsp;&lt;/STRONG&gt;&lt;SPAN&gt;&lt;STRONG&gt;to a separate list and set the group members based on that&lt;/STRONG&gt;."&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/EM&gt;How do I append each successful output to a list and pass it to a new group?&lt;/P&gt;</description>
      <pubDate>Fri, 16 Apr 2021 12:44:48 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/Ansible/Ansible-Add-members-to-a-group/m-p/116147#M520</guid>
      <dc:creator>ukohae</dc:creator>
      <dc:date>2021-04-16T12:44:48Z</dc:date>
    </item>
    <item>
      <title>Re: Ansible - Add members to a group</title>
      <link>https://community.checkpoint.com/t5/Ansible/Ansible-Add-members-to-a-group/m-p/116199#M521</link>
      <description>&lt;P&gt;How can I append the output of my result such that on successful call to &lt;STRONG&gt;cp_host_mgmt&lt;/STRONG&gt;, it still applies only the&amp;nbsp; newly added host (&lt;STRONG&gt;excluding existing host and duplicate IPs&lt;/STRONG&gt;)&amp;nbsp; to the new group &lt;STRONG&gt;gTest2A&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Apr 2021 15:16:37 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/Ansible/Ansible-Add-members-to-a-group/m-p/116199#M521</guid>
      <dc:creator>ukohae</dc:creator>
      <dc:date>2021-04-16T15:16:37Z</dc:date>
    </item>
    <item>
      <title>Re: Ansible - Add members to a group</title>
      <link>https://community.checkpoint.com/t5/Ansible/Ansible-Add-members-to-a-group/m-p/116200#M522</link>
      <description>&lt;P&gt;If you&amp;nbsp;&lt;STRONG&gt;ignore_warnings&lt;/STRONG&gt; as discussed before it will create the additional host objects, and you can use the original list you've created from the CSV.&lt;/P&gt;
&lt;P&gt;For the other options, you can register the result in a variable (e.g. &lt;STRONG&gt;result&lt;/STRONG&gt;).&amp;nbsp;&lt;SPAN&gt;Starting in Ansible 1.6.1, the results registered with multiple items are stored in &lt;STRONG&gt;result.results&lt;/STRONG&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;as an array. You then need to create a filter to weed out the bad ones and use the result.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;See:&amp;nbsp;&lt;A href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html" target="_blank"&gt;https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;However, I advise against this approach for anything but a temporary solution, as it goes against the Ansible concept of Idempotency, and will make your future life much harder.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Apr 2021 15:24:43 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/Ansible/Ansible-Add-members-to-a-group/m-p/116200#M522</guid>
      <dc:creator>Jonas_Rosenboom</dc:creator>
      <dc:date>2021-04-16T15:24:43Z</dc:date>
    </item>
    <item>
      <title>Re: Ansible - Add members to a group</title>
      <link>https://community.checkpoint.com/t5/Ansible/Ansible-Add-members-to-a-group/m-p/116226#M523</link>
      <description>&lt;P&gt;Thanks.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Group: gTest1A

Host: gTest101
IP: 10.1.2.3
Comment: FWP - Firewall&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;New Group to be published&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Group: gTest2A

Host: gTest101
IP: 10.1.2.3
Comment: FWP - Ansible Test&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;Another thing I was wondering about. What Happens if&amp;nbsp; Group: gTest1A already and it contains a Host [ gTest101, 10.1.2.3, FWP - Firewall]&lt;BR /&gt;&lt;BR /&gt;if I publish a task to a new Group called gTest2A and the Host has the same Name and IP, but different Comment [gTest101, 10.1.2.3, FWP - Ansible Test]&lt;BR /&gt;&lt;BR /&gt;Why does it edit the existing group's Comment in gTest1A ( from "FWP - Firewall"&amp;nbsp; to "FWP - Ansible Test"]&lt;BR /&gt;&lt;BR /&gt;Ansible automatically updates the existing group's Comment.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Before it gets changed automatically, ansible doesn't throw any error message that this object already exists. It just shows &lt;STRONG&gt;"changed"&lt;/STRONG&gt; in my command line.&lt;BR /&gt;&lt;BR /&gt;is there a way to create the new group gTest2A without editing existing Comments in gTest1A automatically?&lt;/P&gt;</description>
      <pubDate>Sat, 17 Apr 2021 05:47:32 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/Ansible/Ansible-Add-members-to-a-group/m-p/116226#M523</guid>
      <dc:creator>ukohae</dc:creator>
      <dc:date>2021-04-17T05:47:32Z</dc:date>
    </item>
    <item>
      <title>Re: Ansible - Add members to a group</title>
      <link>https://community.checkpoint.com/t5/Ansible/Ansible-Add-members-to-a-group/m-p/116230#M524</link>
      <description>&lt;P&gt;I assume you mean it changes the existing &lt;STRONG&gt;host&lt;/STRONG&gt; object's comment here.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That is what Ansible is designed for. It makes sure that after the playbook finishes, the actual state of your objects matches the values configured in Ansible.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There should be one "correct" comment for an object at any given time. Either you would need to update the configuration in Ansible, or change the comment in the object (the latter being done for you by Ansible in this case).&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 17 Apr 2021 13:37:25 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/Ansible/Ansible-Add-members-to-a-group/m-p/116230#M524</guid>
      <dc:creator>Jonas_Rosenboom</dc:creator>
      <dc:date>2021-04-17T13:37:25Z</dc:date>
    </item>
  </channel>
</rss>

