def test_can_add_connection_key_file(self):
     with HostVarsFile(self.file_system, 'host1', Mock()) as f:
         f.add_conn_file('mycert.pem')
     self.assertEquals(
         os.linesep.join(
             ['---', 'ansible_ssh_private_key_file: "mycert.pem"']),
         self.file_system.read_all_lines('host_vars', 'host1'))
    def _add_host_vars_files(self, ansi_conf, logger):
        """
        :type ansi_conf: AnsibleConfiguration
        :type logger: Logger
        """
        for host_conf in ansi_conf.hosts_conf:
            with HostVarsFile(self.file_system, host_conf.ip, logger) as file:
                file.add_vars(host_conf.parameters)
                file.add_connection_type(host_conf.connection_method)
                ansible_port = self.ansible_connection_helper.get_ansible_port(
                    host_conf)
                file.add_port(ansible_port)

                if host_conf.connection_method == AnsibleConnectionHelper.CONNECTION_METHOD_WIN_RM:
                    if host_conf.connection_secured:
                        file.add_ignore_winrm_cert_validation()

                file.add_username(host_conf.username)
                if host_conf.password:
                    file.add_password(host_conf.password)
                else:
                    file_name = host_conf.ip + '_access_key.pem'
                    with self.file_system.create_file(file_name,
                                                      0400) as file_stream:
                        file_stream.write(host_conf.access_key)
                    file.add_conn_file(file_name)
 def test_can_add_custom_vars(self):
     with HostVarsFile(self.file_system, 'host1', Mock()) as f:
         f.add_vars({'param1': 'abc', 'param2': '123'})
         f.add_vars({'param3': 'W'})
     self.assertEquals(
         os.linesep.join(
             ['---', 'param1: "abc"', 'param2: "123"', 'param3: "W"']),
         self.file_system.read_all_lines('host_vars', 'host1'))
 def test_can_add_port(self):
     with HostVarsFile(self.file_system, 'host1', Mock()) as f:
         f.add_port('1234')
     self.assertEquals(
         os.linesep.join(['---', 'ansible_port: "1234"']),
         self.file_system.read_all_lines('host_vars', 'host1'))
 def test_can_add_connection_type(self):
     with HostVarsFile(self.file_system, 'host1', Mock()) as f:
         f.add_password('1234')
     self.assertEquals(
         os.linesep.join(['---', 'ansible_ssh_pass: "******"']),
         self.file_system.read_all_lines('host_vars', 'host1'))
 def test_can_add_connection_type(self):
     with HostVarsFile(self.file_system, 'host1', Mock()) as f:
         f.add_username('admin')
     self.assertEquals(
         os.linesep.join(['---', 'ansible_user: "******"']),
         self.file_system.read_all_lines('host_vars', 'host1'))
 def test_first_call_creates_the_folder(self):
     with HostVarsFile(self.file_system, 'host1', Mock()) as f:
         pass
     self.assertIn('host_vars', self.file_system.folders)