Server IP : 85.214.239.14 / Your IP : 3.135.193.70 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /lib/python3/dist-packages/ansible_collections/openstack/cloud/plugins/modules/ |
Upload File : |
#!/usr/bin/python # Copyright (c) 2015 Hewlett-Packard Development Company, L.P. # Copyright (c) 2013, Benno Joy <benno@ansible.com> # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) DOCUMENTATION = ''' --- module: security_group short_description: Add/Delete security groups from an OpenStack cloud. author: OpenStack Ansible SIG description: - Add or Remove security groups from an OpenStack cloud. options: name: description: - Name that has to be given to the security group. This module requires that security group names be unique. required: true type: str description: description: - Long description of the purpose of the security group type: str state: description: - Should the resource be present or absent. choices: [present, absent] default: present type: str project: description: - Unique name or ID of the project. required: false type: str requirements: - "python >= 3.6" - "openstacksdk" extends_documentation_fragment: - openstack.cloud.openstack ''' EXAMPLES = ''' # Create a security group - openstack.cloud.security_group: cloud: mordred state: present name: foo description: security group for foo servers # Update the existing 'foo' security group description - openstack.cloud.security_group: cloud: mordred state: present name: foo description: updated description for the foo security group # Create a security group for a given project - openstack.cloud.security_group: cloud: mordred state: present name: foo project: myproj ''' from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule class SecurityGroupModule(OpenStackModule): argument_spec = dict( name=dict(required=True), description=dict(default=''), state=dict(default='present', choices=['absent', 'present']), project=dict(default=None), ) def _needs_update(self, secgroup): """Check for differences in the updatable values. NOTE: We don't currently allow name updates. """ if secgroup['description'] != self.params['description']: return True return False def _system_state_change(self, secgroup): state = self.params['state'] if state == 'present': if not secgroup: return True return self._needs_update(secgroup) if state == 'absent' and secgroup: return True return False def run(self): name = self.params['name'] state = self.params['state'] description = self.params['description'] project = self.params['project'] if project is not None: proj = self.conn.get_project(project) if proj is None: self.fail_json(msg='Project %s could not be found' % project) project_id = proj['id'] else: project_id = self.conn.current_project_id if project_id: filters = {'tenant_id': project_id} else: filters = None secgroup = self.conn.get_security_group(name, filters=filters) if self.ansible.check_mode: self.exit(changed=self._system_state_change(secgroup)) changed = False if state == 'present': if not secgroup: kwargs = {} if project_id: kwargs['project_id'] = project_id secgroup = self.conn.create_security_group(name, description, **kwargs) changed = True else: if self._needs_update(secgroup): secgroup = self.conn.update_security_group( secgroup['id'], description=description) changed = True self.exit( changed=changed, id=secgroup['id'], secgroup=secgroup) if state == 'absent': if secgroup: self.conn.delete_security_group(secgroup['id']) changed = True self.exit(changed=changed) def main(): module = SecurityGroupModule() module() if __name__ == '__main__': main()